-
Notifications
You must be signed in to change notification settings - Fork 1
/
elasticsearch.py
35 lines (31 loc) · 1.23 KB
/
elasticsearch.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
import requests
N_RESULTS = 10
def get_ids_from_es(domain, query):
url = 'http://%s/freebase/label/_search' % domain
response = requests.get(url, params={'q': query, 'size': N_RESULTS})
results = []
if response:
response = response.json()
for hit in response.get('hits', {}).get('hits', []):
freebase_label = hit.get('_source', {}).get('label')
freebase_id = hit.get('_source', {}).get('resource')
score = hit.get('_score')
results.append([freebase_id, freebase_label, score])
return results
if __name__ == '__main__':
import sys
try:
_, DOMAIN = sys.argv
except Exception as e:
print('Usage: python elasticsearch.py DOMAIN')
sys.exit(0)
with open('entries_single_page.txt') as f:
with open('labels_one_page.txt', 'w') as out:
# write page id
out.write(f.readline())
# process queries
for query in f:
results = get_ids_from_es(DOMAIN, query.strip())
out.write('ES-Response-For-Entry: ' + query.strip() + '\n')
for id, label, score in results:
out.write(str(id) + ' ' + str(label) + ' ' + str(score) + '\n')