From 5b37115e46e4e1eb744f1daedb3f145928d1c704 Mon Sep 17 00:00:00 2001 From: Kazgatari <56477241+Kazgatari@users.noreply.github.com> Date: Sun, 21 Jun 2026 12:36:36 +0200 Subject: [PATCH] changed logic of semantic and hybrid query. Tested different boostings --- service/src/main/inse/topics/logic.py | 106 ++++++++++++++++---------- 1 file changed, 66 insertions(+), 40 deletions(-) diff --git a/service/src/main/inse/topics/logic.py b/service/src/main/inse/topics/logic.py index adc56a7a..94646f16 100644 --- a/service/src/main/inse/topics/logic.py +++ b/service/src/main/inse/topics/logic.py @@ -256,10 +256,19 @@ def _datetime_str_or_none(value: Any) -> str | None: return f"{text}T00:00:00Z" return text +# Configuration for vector search similarity thresholds and boosts +SIMILARITY_THRESHOLD = 0.62 +BOOST_LEXICAL = 1.0 +# higher values for semantic weights, because cosine similarity is between 0 and 1 +BOOST_DISPLAY_NAME_VECTOR = 30.0 +BOOST_DESCRIPTION_VECTOR = 15.0 +BOOST_KEYWORD_VECTORS = 25.0 + async def vector_search_topics( *, query: str, limit: int = 25, + search_type: str = "hybrid", ) -> TopicsListResponse: """ Perform a semantic similarity search on the topicvectors index. @@ -287,53 +296,70 @@ async def vector_search_topics( ) # 2. Query Elasticsearch using standard Bool Should hybrid search + should_clauses = [] + + # Lexical multi_match search (only in hybrid mode) + if search_type == "hybrid": + should_clauses.append({ + "multi_match": { + "query": query, + "fields": [ + "display_name^3", + "keywords^2", + "description^1" + ], + "boost": BOOST_LEXICAL + } + }) + + # Semantic KNN clauses + should_clauses.extend([ + { + "knn": { + "field": "display_name_vector", + "query_vector": query_vector, + "k": limit, + "num_candidates": 100, + "similarity": SIMILARITY_THRESHOLD, + "boost": BOOST_DISPLAY_NAME_VECTOR + } + }, + { + "knn": { + "field": "description_vector", + "query_vector": query_vector, + "k": limit, + "num_candidates": 100, + "similarity": SIMILARITY_THRESHOLD, + "boost": BOOST_DESCRIPTION_VECTOR + } + }, + { + "nested": { + "path": "keyword_vectors", + "query": { + "knn": { + "field": "keyword_vectors.vector", + "query_vector": query_vector, + "k": limit, + "num_candidates": 100, + "similarity": SIMILARITY_THRESHOLD + } + }, + "score_mode": "max", + "boost": BOOST_KEYWORD_VECTORS + } + } + ]) body = { "query": { "bool": { - "should": [ - { - "multi_match": { - "query": query, - "fields": [ - "display_name^3", - "keywords^2", - "description^1" - ], - "boost": 1.0 - } - }, - { - "knn": { - "field": "display_name_vector", - "query_vector": query_vector, - "k": limit, - "num_candidates": 100, - "similarity": 0.62, - "boost": 2.0 - } - }, - { - "nested": { - "path": "description_vectors", - "query": { - "knn": { - "field": "description_vectors.vector", - "query_vector": query_vector, - "k": limit, - "num_candidates": 100, - "similarity": 0.62 - } - }, - "score_mode": "max", - "boost": 1.0 - } - } - ] + "should": should_clauses } }, "_source": { - "excludes": ["display_name_vector", "description_vectors", "description", "siblings"] # remove unrequired fields (description, sibling currently not needed) + "excludes": ["display_name_vector", "description_vector", "keyword_vectors", "description"] # remove unrequired fields }, "size": limit }