Skip to content

Commit 17cd747

Browse files
committed
Refactors _get_hybrid_query
1 parent 0b93788 commit 17cd747

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

src/neo4j_graphrag/neo4j_queries.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,32 +117,20 @@
117117

118118

119119
def _get_hybrid_query(neo4j_version_is_5_23_or_above: bool) -> str:
120-
if neo4j_version_is_5_23_or_above:
121-
return (
122-
f"CALL () {{ {VECTOR_INDEX_QUERY} "
123-
f"WITH collect({{node:node, score:score}}) AS nodes, max(score) AS vector_index_max_score "
124-
f"UNWIND nodes AS n "
125-
f"RETURN n.node AS node, (n.score / vector_index_max_score) AS score "
126-
f"UNION "
127-
f"{FULL_TEXT_SEARCH_QUERY} "
128-
f"WITH collect({{node:node, score:score}}) AS nodes, max(score) AS ft_index_max_score "
129-
f"UNWIND nodes AS n "
130-
f"RETURN n.node AS node, (n.score / ft_index_max_score) AS score }} "
131-
f"WITH node, max(score) AS score ORDER BY score DESC LIMIT $top_k"
132-
)
133-
else:
134-
return (
135-
f"CALL {{ {VECTOR_INDEX_QUERY} "
136-
f"WITH collect({{node:node, score:score}}) AS nodes, max(score) AS vector_index_max_score "
137-
f"UNWIND nodes AS n "
138-
f"RETURN n.node AS node, (n.score / vector_index_max_score) AS score "
139-
f"UNION "
140-
f"{FULL_TEXT_SEARCH_QUERY} "
141-
f"WITH collect({{node:node, score:score}}) AS nodes, max(score) AS ft_index_max_score "
142-
f"UNWIND nodes AS n "
143-
f"RETURN n.node AS node, (n.score / ft_index_max_score) AS score }} "
144-
f"WITH node, max(score) AS score ORDER BY score DESC LIMIT $top_k"
145-
)
120+
call_prefix = "CALL () { " if neo4j_version_is_5_23_or_above else "CALL { "
121+
query_body = (
122+
f"{VECTOR_INDEX_QUERY} "
123+
"WITH collect({node:node, score:score}) AS nodes, max(score) AS vector_index_max_score "
124+
"UNWIND nodes AS n "
125+
"RETURN n.node AS node, (n.score / vector_index_max_score) AS score "
126+
"UNION "
127+
f"{FULL_TEXT_SEARCH_QUERY} "
128+
"WITH collect({node:node, score:score}) AS nodes, max(score) AS ft_index_max_score "
129+
"UNWIND nodes AS n "
130+
"RETURN n.node AS node, (n.score / ft_index_max_score) AS score } "
131+
"WITH node, max(score) AS score ORDER BY score DESC LIMIT $top_k"
132+
)
133+
return call_prefix + query_body
146134

147135

148136
def _get_filtered_vector_query(

0 commit comments

Comments
 (0)