-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_search.py
33 lines (23 loc) · 989 Bytes
/
simple_search.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
import json
from time import sleep
from elasticsearch import Elasticsearch, TransportError
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
INDEX_NAME = "wikipedia_pages"
DOC_TYPE = "Wikipage"
if es.indices.exists(INDEX_NAME):
es.indices.delete(index=INDEX_NAME)
if not es.indices.exists(INDEX_NAME):
es.indices.create(index=INDEX_NAME)
with open("data/mapping.json", "r") as file:
es.indices.put_mapping(doc_type=DOC_TYPE, body=json.load(file), index=INDEX_NAME)
with open("data/web_crawler.json", "r") as file:
json_object: dict = json.load(file)[0]
try:
es.index(index=INDEX_NAME, body=json_object, doc_type=DOC_TYPE)
except TransportError as e:
print(e.info)
sleep(1)
with open("data/simple_query.json", "r") as query_file:
query = json.load(query_file)
results = es.search(INDEX_NAME, DOC_TYPE, query, size=10)
print('Title: %s' % (results["hits"]["hits"][0]['_source']['title']))