-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_bxbooks.py
67 lines (61 loc) · 2.2 KB
/
read_bxbooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from elasticsearch import Elasticsearch, helpers
import csv
import json
import time
matching_query = { "query_string": {
"query" : None
}
}
class mBook:
isbn = None
book_title = None
book_author = None
year_pub = None
pub = None
summ = None
metric = 0
def __init__(self,isbn,book_title,book_author,year_pub,pub,summ):
self.isbn = isbn
self.book_title = book_title
self.book_author = book_author
self.year_pub = year_pub
self.pub = pub
self.summ = summ
#olh h plhroforia se mia lista
def get_info(self):
return [self.isbn,self.book_title,self.book_author,self.year_pub,self.pub,self.summ,self.metric]
#to metriko vasei tou user pou exw id
def set_metric(self,metric):
self.metric = metric
def main():
global matching_query
#sundesh
es = Elasticsearch(host = "localhost", port = 9200)
#results["hits"]["hits"][0]["_score"] to score ka8e document
#results[0]["_source"] periexontai ta kleidia ths plhroforias
while 1:
#pairnw eisodo
mvar = str(input("Give a string : "))
matching_query["query_string"]["query"] = str(mvar)
#searching ...
results = es.search(index="bx_books_2",query=matching_query,size = 10000)
mcounter = 0 #gia na apari8mhsw to plh8os
results = results["hits"]["hits"]#ta apotelesmata moy
#pairnw ta kleidia
try :
lst = list(results[0]["_source"].keys())
except IndexError : #an paizei index error den exw parei apotelesmata, afou prospa8w na parw to 0 ke den to vriskw eimai se empty list
print("No results.\nSearch again.")
#emfanish apotelesmatwn +fwna me metrikh?
for i in results:
print("Score : "+str(i["_score"]))
ln = "Number : " +str(mcounter) + " \n"
for k in lst:
ln = ln + " " + str(k)+ " : "+str(i["_source"][str(k)]) + "\n"
#endfor
print(ln)#emfanish apotelesmatos
mcounter+=1
#endfor
#endwhile
if __name__ == "__main__":
main()