diff --git a/src/neo4j_genai/neo4j_queries.py b/src/neo4j_genai/neo4j_queries.py index 105a5e285..456b86baa 100644 --- a/src/neo4j_genai/neo4j_queries.py +++ b/src/neo4j_genai/neo4j_queries.py @@ -53,7 +53,9 @@ def _get_hybrid_query() -> str: return ( f"CALL {{ {VECTOR_INDEX_QUERY} " - f"RETURN node, score " + f"WITH collect({{node:node, score:score}}) AS nodes, max(score) AS max " + f"UNWIND nodes AS n " + f"RETURN n.node AS node, (n.score / max) AS score " f"UNION " f"{FULL_TEXT_SEARCH_QUERY} " f"WITH collect({{node:node, score:score}}) AS nodes, max(score) AS max " diff --git a/tests/unit/test_neo4j_queries.py b/tests/unit/test_neo4j_queries.py index 14aee495a..e70c91d9e 100644 --- a/tests/unit/test_neo4j_queries.py +++ b/tests/unit/test_neo4j_queries.py @@ -35,7 +35,9 @@ def test_hybrid_search_basic() -> None: "CALL { " "CALL db.index.vector.queryNodes($vector_index_name, $top_k, $query_vector) " "YIELD node, score " - "RETURN node, score UNION " + "WITH collect({node:node, score:score}) AS nodes, max(score) AS max " + "UNWIND nodes AS n " + "RETURN n.node AS node, (n.score / max) AS score UNION " "CALL db.index.fulltext.queryNodes($fulltext_index_name, $query_text, {limit: $top_k}) " "YIELD node, score " "WITH collect({node:node, score:score}) AS nodes, max(score) AS max " @@ -125,7 +127,9 @@ def test_hybrid_search_with_retrieval_query() -> None: "CALL { " "CALL db.index.vector.queryNodes($vector_index_name, $top_k, $query_vector) " "YIELD node, score " - "RETURN node, score UNION " + "WITH collect({node:node, score:score}) AS nodes, max(score) AS max " + "UNWIND nodes AS n " + "RETURN n.node AS node, (n.score / max) AS score UNION " "CALL db.index.fulltext.queryNodes($fulltext_index_name, $query_text, {limit: $top_k}) " "YIELD node, score " "WITH collect({node:node, score:score}) AS nodes, max(score) AS max " @@ -145,7 +149,9 @@ def test_hybrid_search_with_properties() -> None: "CALL { " "CALL db.index.vector.queryNodes($vector_index_name, $top_k, $query_vector) " "YIELD node, score " - "RETURN node, score UNION " + "WITH collect({node:node, score:score}) AS nodes, max(score) AS max " + "UNWIND nodes AS n " + "RETURN n.node AS node, (n.score / max) AS score UNION " "CALL db.index.fulltext.queryNodes($fulltext_index_name, $query_text, {limit: $top_k}) " "YIELD node, score " "WITH collect({node:node, score:score}) AS nodes, max(score) AS max "