-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesManager.py
43 lines (34 loc) · 1.31 KB
/
esManager.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
import logging
from elasticsearch import Elasticsearch
logger = logging.getLogger(__name__)
logger.level = logging.DEBUG
class ElasticSearchManager:
def __init__(self, url="http://localhost:9200"):
self.es = Elasticsearch(url, timeout=60)
logger.info("Connected to Elasticsearch!")
def delete_index(self, index_name):
self.es.indices.delete(index=index_name, ignore_unavailable=True)
def create_index(self, index_name):
self.es.indices.create(
index=index_name,
mappings={
"properties": {
"embedding": {
"type": "dense_vector",
"index": True,
"similarity": "cosine",
"dims": 512
}
}
},
)
def search(self, index_name, **query_args):
return self.es.search(index=index_name, **query_args)
def index_item(self, index_name, document):
return self.es.index(index=index_name, document=document)
def bulk_insert(self, index_name, documents):
operations = []
for document in documents:
operations.append({'index': {'_index': index_name}})
operations.append(document)
return self.es.bulk(operations=operations)