-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_map.py
46 lines (40 loc) · 1.24 KB
/
index_map.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
44
45
46
from config import settings
def create_df_index(es):
"""
If the index doesn't exist, create it.
"""
index_body = {
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1,
},
"mappings": {
"properties": {
"movie_name": {
"type": "text",
},
"embedding": {"type": "dense_vector", "dims": 384},
"genre": {
"type": "text",
},
"movie_id": {"type": "text"},
}
},
}
try:
# Ignore 400 means to ignore "Index Already Exist" error.
es.indices.create(index=settings.INDEX_NAME, body=index_body) # ignore=[400, 404]
print(f"Created Index -> {settings.INDEX_NAME}")
# else:
# print(f"Index {settings.INDEX_NAME} exists...")
except Exception as ex:
print(str(ex))
def insert_df_row(doc, es):
"""
It checks if the index exists, if not, it creates it, then it indexes the document.
:param doc: The document to be inserted
"""
if not es.indices.exists(settings.INDEX_NAME):
create_df_index(es)
# try:
es.index(index=settings.INDEX_NAME, body=doc)