-
Notifications
You must be signed in to change notification settings - Fork 2
/
6-elastic.py
54 lines (41 loc) · 1.47 KB
/
6-elastic.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
47
48
49
50
51
52
53
54
import time
from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer
from common import read_verses
es_client = Elasticsearch(
"http://localhost:9200",
basic_auth=("elastic", "adminadmin")
)
def elastic_inserts(chunk):
start_time = time.perf_counter()
for id, text, meta, embedding in chunk:
es_client.index(index="verses", document={
"text": text,
"meta": meta,
"embedding": embedding
})
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"batch insert: {elapsed_time} sec")
return elapsed_time
def elastic_search(embedding):
results = es_client.search(index="verses", knn={
"field": "embedding",
"query_vector": embedding,
"k": 10,
"num_candidates": 10
}, source_includes=["text"])
for row in results['hits']['hits']:
print(f"Text: {row['_source']['text']}; Similarity: {row['_score']}")
read_verses(elastic_inserts, max_items=1400000, minibatch_size=1000)
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2')
embeddings = model.encode("воскресил из мертвых")
start_time = time.perf_counter()
elastic_search(embeddings)
elastic_search(embeddings)
elastic_search(embeddings)
elastic_search(embeddings)
elastic_search(embeddings)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"Search time: {elapsed_time/5} sec")