-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic_search.py
34 lines (29 loc) · 1020 Bytes
/
elastic_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
34
from elasticsearch import Elasticsearch
class ElasticSearchResult:
def __init__(self, cloud_id, elastic_user, elastic_password):
self._INDEX = "docs"
self.client = Elasticsearch(
cloud_id=cloud_id, basic_auth=(elastic_user, elastic_password))
def indexing(self, document_list):
i = 0
for doc in document_list:
ans = self.client.index(index=self._INDEX, id=i, document=doc)
i += 1
def get_index(self):
print(self.client.indices.get(index=self._INDEX))
print('done')
def delete_doc(self):
self.client.indices.delete(index=self._INDEX, ignore=[400, 404])
def search(self, query, k: int = 10, _from: int = 0):
body = {
'from': 0,
'size': k,
'query': {
'match': {
'paragraphs': query
}
}
}
print(body)
out = self.client.search(index=self._INDEX, body=body)
return out