updated music search

This commit is contained in:
Alexander Karpov 2024-04-22 14:17:11 +03:00
parent 4a2b86509e
commit 7e188865cc
2 changed files with 15 additions and 10 deletions

View File

@ -35,7 +35,8 @@ class SongDocument(Document):
name = fields.TextField( name = fields.TextField(
attr="name", attr="name",
fields={ fields={
"raw": fields.KeywordField(normalizer="lowercase"), "raw": fields.KeywordField(),
"exact": fields.KeywordField(normalizer="lowercase"),
}, },
) )
suggest = fields.CompletionField() suggest = fields.CompletionField()

View File

@ -12,8 +12,11 @@ def search_song(query):
search_query = ES_Q( search_query = ES_Q(
"bool", "bool",
should=[ should=[
ES_Q("match", name=query), ES_Q("match_phrase", name__exact=query, boost=3), # Boost for exact matches
ES_Q("match", name__russian=query), ES_Q(
"match_phrase", name__raw=query.lower(), boost=2.5
), # Case-insensitive exact match
ES_Q("match", name__russian=query), # Match in Russian field
ES_Q( ES_Q(
"multi_match", "multi_match",
query=query, query=query,
@ -24,7 +27,7 @@ def search_song(query):
"authors.name.raw^3", "authors.name.raw^3",
"album.name^3", "album.name^3",
"album.name.raw^3", "album.name.raw^3",
"name.raw^2", "meta.*",
], ],
type="best_fields", type="best_fields",
fuzziness="AUTO", fuzziness="AUTO",
@ -38,8 +41,8 @@ def search_song(query):
fields=["authors.name", "authors.name.raw"], fields=["authors.name", "authors.name.raw"],
fuzziness="AUTO", fuzziness="AUTO",
), ),
boost=2,
), ),
ES_Q("wildcard", name__raw=f"*{query.lower()}*"),
ES_Q( ES_Q(
"nested", "nested",
path="album", path="album",
@ -49,13 +52,14 @@ def search_song(query):
fields=["album.name", "album.name.raw"], fields=["album.name", "album.name.raw"],
fuzziness="AUTO", fuzziness="AUTO",
), ),
boost=2,
), ),
ES_Q( ES_Q(
"nested", "wildcard", name__raw=f"*{query.lower()}*"
path="album", ), # Wildcard search for partial matches
query=ES_Q("wildcard", album__name__raw=f"*{query.lower()}*"), ES_Q(
), "wildcard", meta__raw=f"*{query.lower()}*"
ES_Q("wildcard", meta__raw=f"*{query.lower()}*"), ), # Wildcard search in meta fields
], ],
minimum_should_match=1, minimum_should_match=1,
) )