-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_es.py
50 lines (42 loc) · 1.33 KB
/
search_es.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
from config import settings
def search_topN(query_vec, es, no_of_recommendation):
"""
It takes a vector as input and returns the top 10 movies that are most similar to the input vector.
:param query_vec: The vector of the query movie
"""
script_query = {
"script_score": {
"query": {"match_all": {}},
"script": {
"params": {"query_vector": query_vec},
"source": "cosineSimilarity(params.query_vector, 'embedding') + 1.0",
},
}
}
response = es.search(
index=settings.INDEX_NAME,
body={
"size": no_of_recommendation,
"query": script_query,
"_source": ["movie_name", "genre"],
},
)
return response["hits"]["hits"]
# def search_movies(query, es, no_of_content):
# """
# It takes a query string, connects to the Elasticsearch cluster, and returns the top 10 results
# :param query: The query string to search for
# """
# script_query = {
# "query" : {
# "match" : {
# 'movie_name': query
# },
# },
# },
# response = es.search(
# index = settings.INDEX_NAME,
# body = script_query,
# size = no_of_content,
# )
# return response['hits']['hits']