-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_examples.py
88 lines (75 loc) · 2.64 KB
/
query_examples.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
from elasticsearch import Elasticsearch
from dotenv import load_dotenv
from elastic_client import es
load_dotenv(override=True)
# This file uses the 'books-pipeline' index, which is created in the 'upload_books_local_embed.py' file.
# If you wish to use the 'books-local' index, change the below line to:
# 'INDEX_NAME = f"{os.environ.get('INDEX_NAME')}-local"'
INDEX_NAME = f'{os.environ.get("INDEX_NAME")}-pipeline'
MODEL_ID = os.environ.get("MODEL_ID")
def vector_search(query_string):
search_result = es.search(
index=INDEX_NAME,
knn={
"field": "description_embedding",
"k": 10,
"num_candidates": 50,
"query_vector_builder": {
"text_embedding": {"model_id": MODEL_ID, "model_text": query_string}
},
},
)
return search_result
def search(query_string):
search_result = es.search(
index=INDEX_NAME, body={"query": {"match": {"book_description": query_string}}}
)
return search_result
def hybrid_search(query_string):
search_result = es.search(
index=INDEX_NAME,
body={
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": query_string,
"fields": [
"book_title",
"author_name",
"book_description",
],
}
},
]
}
},
"knn": {
"field": "description_embedding",
"k": 10,
"query_vector_builder": {
"text_embedding": {"model_id": MODEL_ID, "model_text": query_string}
},
},
"rank": {"rrf": {}},
"size": 10,
},
)
return search_result
def print_results(search_result):
print(f"Total hits: {search_result['hits']['total']['value']}")
for hit in search_result["hits"]["hits"]:
print(f"Book: {hit['_source']['book_title']}")
print(f"Author: {hit['_source']['author_name']}")
print(f"Description: {hit['_source']['book_description']}")
print(f"Score: {hit['_score']}")
print("")
query_string = "Dinosaurs are still alive"
vector_results = vector_search(query_string)
print_results(vector_results)
bm25_results = search(query_string)
print_results(bm25_results)
hybrid_results = hybrid_search(query_string)
print_results(hybrid_results)