-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_ElastiSearch.py
39 lines (31 loc) · 1.07 KB
/
query_ElastiSearch.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
import csv
from os import listdir
from os.path import isfile, join
from elasticsearch import Elasticsearch, helpers
import requests
import argparse
import ElastiSearch_build_index
es = Elasticsearch([{'host':'localhost', 'port': 9200}])
FOLDER_PATH = 'data'
parser = argparse.ArgumentParser(description="Ranking and retrieval")
parser.add_argument('--query', help="Enter query", default="", type=str)
def elastic_search(search_string, number_results):
global es
search_param = {
'query': {
'match': {
'snippet': '{}'.format(search_string)
}
}
}
res = es.search(index="news", body=search_param, size=number_results)
# print("Got %d Hits:" % res['hits']['total']['value'])
return_list = []
for hit in res['hits']['hits']:
return_list.append(hit['_source'])
return return_list
if __name__=="__main__":
# Sample usage. ElastiSearch sorted top 20 results for query 'cathedral climate'.
args = parser.parse_args()
search_results = elastic_search(args.query, 20)
print(search_results)