-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_query_ml_zoomcamp.py
49 lines (43 loc) · 1.38 KB
/
search_query_ml_zoomcamp.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
from elasticsearch import Elasticsearch
# Initialize Elasticsearch client
es = Elasticsearch(
['https://localhost:9200'],
basic_auth=('elastic', 'XqqHEf4B_3-7EGAhnO0L'),
verify_certs=False # Only for testing purposes
)
index_name = "faq_index"
# Define the search query with a filter for the course field
search_query = {
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "How do I execute a command in a running docker container?",
"fields": ["question^4", "text"],
"type": "best_fields"
}
}
],
"filter": [
{
"term": {
"course": "machine-learning-zoomcamp"
}
}
]
}
},
"size": 3
}
# Execute the search query
response = es.search(index=index_name, body=search_query)
# Print the top 3 results
hits = response['hits']['hits']
for i, hit in enumerate(hits):
print(f"Result {i + 1} - Score: {hit['_score']}")
print(f"Question: {hit['_source']['question']}")
print(f"Text: {hit['_source']['text']}\n")
# Print the 3rd question specifically
third_question = hits[2]['_source']['question']
print("The 3rd question returned by the search engine is:", third_question)