fix: constrain relaxed search matches
This commit is contained in:
1 parent
bc57cb82fe
commit
3b56445bb2
2 files changed
+15
-2
No files matched your search
+10
-2
@@ -25,7 +25,10 @@ LEGAL_FORM_ALIASES = {
|
||||
COMPANY_REGISTRATION_DOCUMENTS = (("230044970", 3000), ("667", 2000), ("4", 1000))
|
||||
TAX_PROFIT_DOCUMENTS = (("112340", 3000),)
|
||||
BOILERPLATE_PREFIXES = {
|
||||
"ru": ("как регулируется", "правовое регулирование", "судебная практика", "сроки и ответственность"),
|
||||
"ru": (
|
||||
"как регулируется", "правовое регулирование", "судебная практика", "сроки и ответственность",
|
||||
"действующая редакция", "последние изменения", "основные положения", "комментарий к акту",
|
||||
),
|
||||
"ky": (),
|
||||
}
|
||||
|
||||
@@ -38,6 +41,10 @@ def relaxed_query_text(language: str, query: str) -> str:
|
||||
return value
|
||||
|
||||
|
||||
def is_named_act_query(language: str, query: str) -> bool:
|
||||
return language == "ru" and bool(re.search(r"\b(?:кодекс\w*|конституци\w*|закон\s+кыргызской\s+республики)\b", query.casefold()))
|
||||
|
||||
|
||||
def query_variants(language: str, query: str) -> list[str]:
|
||||
variants = [query]
|
||||
for alias, expansions in LEGAL_FORM_ALIASES[language].items():
|
||||
@@ -125,7 +132,7 @@ def build_search_query(language: str, query: str) -> dict:
|
||||
for variant in variants
|
||||
]
|
||||
relaxed_text = []
|
||||
if not registration_clauses:
|
||||
if not registration_clauses and not is_named_act_query(language, query):
|
||||
seen_relaxed = set()
|
||||
for variant in variants:
|
||||
value = relaxed_query_text(language, variant)
|
||||
@@ -141,6 +148,7 @@ def build_search_query(language: str, query: str) -> dict:
|
||||
"fields": [f"document_name_{language}^4", f"text_{language}"],
|
||||
"type": "cross_fields",
|
||||
"operator": "OR",
|
||||
"minimum_should_match": "75%",
|
||||
"boost": 0.1,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -57,6 +57,7 @@ class SearchRelevanceTest(unittest.TestCase):
|
||||
self.assertEqual(strict[0]["query"], "правовое регулирование расторжение договора аренды")
|
||||
self.assertEqual(relaxed[0]["query"], "расторжение договора аренды")
|
||||
self.assertEqual(relaxed[0]["operator"], "OR")
|
||||
self.assertEqual(relaxed[0]["minimum_should_match"], "75%")
|
||||
self.assertNotIn("boost", strict[0])
|
||||
|
||||
def test_relaxed_match_supports_kyrgyz_and_avoids_short_named_acts(self):
|
||||
@@ -68,6 +69,10 @@ class SearchRelevanceTest(unittest.TestCase):
|
||||
act_clauses = named_act["query"]["bool"]["must"]["bool"]["should"]
|
||||
self.assertFalse(any(item.get("multi_match", {}).get("boost") == 0.1 for item in act_clauses))
|
||||
|
||||
long_named_act = json.loads(build_search_body("ru", "последние изменения Гражданского кодекса Кыргызской Республики", 10))
|
||||
long_act_clauses = long_named_act["query"]["bool"]["must"]["bool"]["should"]
|
||||
self.assertFalse(any(item.get("multi_match", {}).get("boost") == 0.1 for item in long_act_clauses))
|
||||
|
||||
short_template = json.loads(build_search_body("ru", "как регулируется взыскание алиментов", 10))
|
||||
short_clauses = short_template["query"]["bool"]["must"]["bool"]["should"]
|
||||
self.assertTrue(any(item.get("multi_match", {}).get("query") == "взыскание алиментов" and item["multi_match"].get("boost") == 0.1 for item in short_clauses))
|
||||
|
||||
Reference in new issue
Block a user