Skip to content

Commit 47312c0

Browse files
authored
Harmonize return properties in Vector and Hybrid retrievers (#193)
* Harmonize retriever results * Undo changes to external retriever * Update CHANGELOG
1 parent d0528f4 commit 47312c0

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- Made `relations` and `potential_schema` optional in `SchemaBuilder`.
77
- Added a check to prevent the use of deprecated Cypher syntax for Neo4j versions 5.23.0 and above.
88

9+
### Changed
10+
- Vector and Hybrid retrievers used with `return_properties` now also return the node labels (`nodeLabels`) and the node's element ID (`id`).
11+
912
## 1.1.0
1013

1114
### Added

src/neo4j_graphrag/neo4j_queries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,5 @@ def get_query_tail(
253253
return retrieval_query
254254
if return_properties:
255255
return_properties_cypher = ", ".join([f".{prop}" for prop in return_properties])
256-
return f"RETURN node {{{return_properties_cypher}}} as node, score"
256+
return f"RETURN node {{{return_properties_cypher}}} AS node, labels(node) AS nodeLabels, elementId(node) AS id, score"
257257
return fallback_return if fallback_return else ""

tests/e2e/qdrant_e2e/test_qdrant_e2e.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def test_qdrant_neo4j_vector_input(driver: Driver, qdrant_client: QdrantClient)
6767
assert isinstance(results, RetrieverResult)
6868
assert len(results.items) == top_k
6969
assert isinstance(results.items[0], RetrieverResultItem)
70-
print("Results are: ", results.items)
7170
pattern = (
7271
r"<Record node=<Node element_id='.+' "
7372
r"labels=frozenset\({'Question'}\) properties={'question': 'In 1953 Watson \& "

tests/unit/retrievers/external/test_weaviate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def test_match_query_with_return_properties() -> None:
225225
"WITH match_param[0] AS match_id_value, match_param[1] AS score "
226226
"MATCH (node) "
227227
"WHERE node[$id_property] = match_id_value "
228-
"RETURN node {.name, .age} as node, score"
228+
"RETURN node {.name, .age} AS node, labels(node) AS nodeLabels, elementId(node) AS id, score"
229229
)
230230
assert match_query.strip() == expected.strip()
231231

tests/unit/test_neo4j_queries.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_vector_search_with_properties() -> None:
5656
expected = (
5757
"CALL db.index.vector.queryNodes($vector_index_name, $top_k, $query_vector) "
5858
"YIELD node, score "
59-
"RETURN node {.name, .age} as node, score"
59+
"RETURN node {.name, .age} AS node, labels(node) AS nodeLabels, elementId(node) AS id, score"
6060
)
6161
result, _ = get_search_query(SearchType.VECTOR, return_properties=properties)
6262
assert result.strip() == expected.strip()
@@ -159,7 +159,7 @@ def test_hybrid_search_with_properties() -> None:
159159
"RETURN n.node AS node, (n.score / ft_index_max_score) AS score "
160160
"} "
161161
"WITH node, max(score) AS score ORDER BY score DESC LIMIT $top_k "
162-
"RETURN node {.name, .age} as node, score"
162+
"RETURN node {.name, .age} AS node, labels(node) AS nodeLabels, elementId(node) AS id, score"
163163
)
164164
result, _ = get_search_query(SearchType.HYBRID, return_properties=properties)
165165
assert result.strip() == expected.strip()
@@ -174,7 +174,7 @@ def test_get_query_tail_with_retrieval_query() -> None:
174174

175175
def test_get_query_tail_with_properties() -> None:
176176
properties = ["name", "age"]
177-
expected = "RETURN node {.name, .age} as node, score"
177+
expected = "RETURN node {.name, .age} AS node, labels(node) AS nodeLabels, elementId(node) AS id, score"
178178
result = get_query_tail(return_properties=properties)
179179
assert result.strip() == expected.strip()
180180

@@ -204,7 +204,7 @@ def test_get_query_tail_ordering_no_retrieval_query() -> None:
204204
properties = ["name", "age"]
205205
fallback = "HELLO"
206206

207-
expected = "RETURN node {.name, .age} as node, score"
207+
expected = "RETURN node {.name, .age} AS node, labels(node) AS nodeLabels, elementId(node) AS id, score"
208208
result = get_query_tail(
209209
return_properties=properties,
210210
fallback_return=fallback,

0 commit comments

Comments
 (0)