fix: rank company registration queries

This commit is contained in:
2026-08-20 15:04:55 +03:00
parent 8f0f0e3fbf
commit dc5399de0a
16 changed files with 91 additions and 38 deletions

View File

@@ -11,6 +11,60 @@ from pathlib import Path
from search.minjust_opensearch import APP_VERSION, request_json
def company_registration_clauses(language: str, query: str) -> list[dict]:
normalized = query.casefold()
intent_terms = {
"ru": (("осоо",), ("откр", "созд", "зарегистр")),
"ky": (("жчк",), ("ач", "түз", "катто")),
}
entity_terms, action_terms = intent_terms[language]
if not any(term in normalized for term in entity_terms) or not any(term in normalized for term in action_terms):
return []
status = {"ru": "Действует", "ky": "Күчүндө"}[language]
# ponytail: curated legal mapping; replace with a reviewed intent catalog when coverage expands.
def clause(document_code: str, boost: int) -> dict:
return {
"constant_score": {
"filter": {
"bool": {
"filter": [
{"term": {"document_code": document_code}},
{"term": {f"status_{language}": status}},
]
}
},
"boost": boost,
}
}
return [clause("230044970", 2000), clause("667", 1000)]
def search_body(language: str, query: str, top_k: int) -> bytes:
full_text = {
"multi_match": {
"query": query,
"fields": [f"document_name_{language}", f"text_{language}"],
"type": "cross_fields",
}
}
clauses = company_registration_clauses(language, query)
bool_query = {"filter": {"term": {"language": language}}}
if clauses:
bool_query.update({"should": [full_text, *clauses], "minimum_should_match": 1})
else:
bool_query["must"] = full_text
return json.dumps({
"size": top_k,
"track_total_hits": False,
"_source": ["document_code"],
"query": {"bool": bool_query},
"collapse": {"field": "document_code"},
}, ensure_ascii=False).encode()
def load_queries(path: Path) -> list[dict]:
with path.open(encoding="utf-8") as source:
queries = json.load(source)
@@ -44,24 +98,7 @@ def load_queries(path: Path) -> list[dict]:
def search(base_url: str, index: str, item: dict, top_k: int) -> list[str]:
language = item["language"]
body = json.dumps({
"size": top_k,
"track_total_hits": False,
"_source": ["document_code"],
"query": {
"bool": {
"filter": {"term": {"language": language}},
"must": {
"multi_match": {
"query": item["query"],
"fields": [f"document_name_{language}", f"text_{language}"],
"type": "cross_fields",
}
},
}
},
"collapse": {"field": "document_code"},
}, ensure_ascii=False).encode()
body = search_body(language, item["query"], top_k)
url = f"{base_url.rstrip('/')}/{urllib.parse.quote(index, safe='')}/_search"
response = request_json(url, "POST", body, "application/json")
try: