mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2025-02-22 13:00:34 +03:00
updated song search
This commit is contained in:
parent
bf182dbd0a
commit
6c15494aab
|
@ -10,28 +10,60 @@
|
||||||
def search_song(query):
|
def search_song(query):
|
||||||
search = SongDocument.search()
|
search = SongDocument.search()
|
||||||
|
|
||||||
# Build a multi_match query that searches in song name, authors' names, and album names
|
# Split the query into words
|
||||||
multi_match_query = ES_Q(
|
terms = query.strip().split()
|
||||||
"multi_match",
|
|
||||||
query=query,
|
# Initialize must and should clauses
|
||||||
fields=[
|
must_clauses = []
|
||||||
"name^5",
|
should_clauses = []
|
||||||
"name.raw^10",
|
|
||||||
"name.exact^15",
|
# Build queries for song names
|
||||||
"authors.name^4",
|
song_name_queries = [
|
||||||
"authors.name.raw^8",
|
ES_Q("match_phrase", name={"query": query, "boost": 5}),
|
||||||
"authors.name.exact^12",
|
ES_Q("match", name={"query": query, "fuzziness": "AUTO", "boost": 4}),
|
||||||
"album.name^3",
|
ES_Q("wildcard", name={"value": f"*{query.lower()}*", "boost": 2}),
|
||||||
"album.name.raw^6",
|
]
|
||||||
"album.name.exact^9",
|
|
||||||
],
|
# Build queries for author names
|
||||||
fuzziness="AUTO",
|
author_name_queries = [
|
||||||
operator="and",
|
ES_Q(
|
||||||
type="best_fields",
|
"nested",
|
||||||
)
|
path="authors",
|
||||||
|
query=ES_Q("match_phrase", name={"query": query, "boost": 5}),
|
||||||
|
),
|
||||||
|
ES_Q(
|
||||||
|
"nested",
|
||||||
|
path="authors",
|
||||||
|
query=ES_Q("match", name={"query": query, "fuzziness": "AUTO", "boost": 4}),
|
||||||
|
),
|
||||||
|
ES_Q(
|
||||||
|
"nested",
|
||||||
|
path="authors",
|
||||||
|
query=ES_Q("wildcard", name={"value": f"*{query.lower()}*", "boost": 2}),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
# If the query contains multiple terms, assume it might include both song and author names
|
||||||
|
if len(terms) > 1:
|
||||||
|
# Build combined queries
|
||||||
|
must_clauses.extend(
|
||||||
|
[
|
||||||
|
ES_Q("bool", should=song_name_queries),
|
||||||
|
ES_Q("bool", should=author_name_queries),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# If single term, search both song and author names but with lower boost
|
||||||
|
should_clauses.extend(song_name_queries + author_name_queries)
|
||||||
|
|
||||||
|
# Combine must and should clauses
|
||||||
|
if must_clauses:
|
||||||
|
search_query = ES_Q("bool", must=must_clauses, should=should_clauses)
|
||||||
|
else:
|
||||||
|
search_query = ES_Q("bool", should=should_clauses, minimum_should_match=1)
|
||||||
|
|
||||||
# Execute search with size limit
|
# Execute search with size limit
|
||||||
search = search.query(multi_match_query).extra(size=20)
|
search = search.query(search_query).extra(size=20)
|
||||||
response = search.execute()
|
response = search.execute()
|
||||||
|
|
||||||
if response.hits:
|
if response.hits:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user