configure fuzziness in search

This commit is contained in:
simon 2022-11-02 11:59:31 +07:00
parent dcf317e471
commit 0f191d8a3e
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
1 changed files with 23 additions and 14 deletions

View File

@ -191,7 +191,7 @@ class SearchParser:
def __init__(self, search_query): def __init__(self, search_query):
self.query_words = search_query.lower().split() self.query_words = search_query.lower().split()
self.query_map = False self.query_map = {"term": [], "fuzzy": []}
self.append_to = "term" self.append_to = "term"
def run(self): def run(self):
@ -214,11 +214,11 @@ class SearchParser:
if ":" in first_word: if ":" in first_word:
index_match, query_string = first_word.split(":") index_match, query_string = first_word.split(":")
if index_match in key_word_map: if index_match in key_word_map:
self.query_map = key_word_map.get(index_match) self.query_map.update(key_word_map.get(index_match))
self.query_words[0] = query_string self.query_words[0] = query_string
return index_match return index_match
self.query_map = key_word_map.get("simple") self.query_map.update(key_word_map.get("simple"))
print(f"query_map: {self.query_map}") print(f"query_map: {self.query_map}")
return "simple" return "simple"
@ -229,29 +229,24 @@ class SearchParser:
return { return {
"simple": { "simple": {
"index": "ta_video,ta_channel,ta_playlist", "index": "ta_video,ta_channel,ta_playlist",
"term": [],
}, },
"video": { "video": {
"index": "ta_video", "index": "ta_video",
"term": [],
"channel": [], "channel": [],
"active": [], "active": [],
}, },
"channel": { "channel": {
"index": "ta_channel", "index": "ta_channel",
"term": [],
"active": [], "active": [],
"subscribed": [], "subscribed": [],
}, },
"playlist": { "playlist": {
"index": "ta_playlist", "index": "ta_playlist",
"term": [],
"active": [], "active": [],
"subscribed": [], "subscribed": [],
}, },
"full": { "full": {
"index": "ta_subtitle", "index": "ta_subtitle",
"term": [],
"lang": [], "lang": [],
"source": [], "source": [],
}, },
@ -329,6 +324,20 @@ class QueryBuilder:
return query return query
def _get_fuzzy(self):
"""return fuziness valuee"""
fuzzy_value = self.query_map.get("fuzzy", ["auto"])[0]
if fuzzy_value == "no":
return 0
if not fuzzy_value.isdigit():
return "auto"
if int(fuzzy_value) > 2:
return "2"
return fuzzy_value
def _build_simple(self): def _build_simple(self):
"""build simple cross index query""" """build simple cross index query"""
must_list = [] must_list = []
@ -339,7 +348,7 @@ class QueryBuilder:
"multi_match": { "multi_match": {
"query": term, "query": term,
"type": "bool_prefix", "type": "bool_prefix",
"fuzziness": "auto", "fuzziness": self._get_fuzzy(),
"operator": "and", "operator": "and",
"fields": [ "fields": [
"channel_name._2gram", "channel_name._2gram",
@ -368,7 +377,7 @@ class QueryBuilder:
"multi_match": { "multi_match": {
"query": term, "query": term,
"type": "bool_prefix", "type": "bool_prefix",
"fuzziness": "auto", "fuzziness": self._get_fuzzy(),
"operator": "and", "operator": "and",
"fields": [ "fields": [
"title._2gram^2", "title._2gram^2",
@ -390,7 +399,7 @@ class QueryBuilder:
"multi_match": { "multi_match": {
"query": channel, "query": channel,
"type": "bool_prefix", "type": "bool_prefix",
"fuzziness": "auto", "fuzziness": self._get_fuzzy(),
"operator": "and", "operator": "and",
"fields": [ "fields": [
"channel.channel_name._2gram", "channel.channel_name._2gram",
@ -413,7 +422,7 @@ class QueryBuilder:
"multi_match": { "multi_match": {
"query": term, "query": term,
"type": "bool_prefix", "type": "bool_prefix",
"fuzziness": "auto", "fuzziness": self._get_fuzzy(),
"operator": "and", "operator": "and",
"fields": [ "fields": [
"channel_description", "channel_description",
@ -445,7 +454,7 @@ class QueryBuilder:
"multi_match": { "multi_match": {
"query": term, "query": term,
"type": "bool_prefix", "type": "bool_prefix",
"fuzziness": "auto", "fuzziness": self._get_fuzzy(),
"operator": "and", "operator": "and",
"fields": [ "fields": [
"playlist_description", "playlist_description",
@ -477,7 +486,7 @@ class QueryBuilder:
"match": { "match": {
"subtitle_line": { "subtitle_line": {
"query": term, "query": term,
"fuzziness": "auto", "fuzziness": self._get_fuzzy(),
} }
} }
} }