Skip to content

Commit 5fbb694

Browse files
authored
Update Cypher retrieval queries (#259)
* Update Cypher retrieval queries to handle cases where the matched node has no relationship * Use camelCase for variable names * Ruff * Fix the fix
1 parent 1921dd5 commit 5fbb694

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

docs/source/user_guide_rag.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,9 @@ certain movie properties, the retrieval query can be structured as follows:
561561
.. code:: python
562562
563563
retrieval_query = """
564-
MATCH
565-
(actor:Actor)-[:ACTED_IN]->(node)
566-
RETURN
567-
node.title AS movie_title,
568-
node.plot AS movie_plot,
569-
collect(actor.name) AS actors;
564+
RETURN node.title as movieTitle,
565+
node.plot as moviePlot,
566+
collect { MATCH (actor:Actor)-[:ACTED_IN]->(node) RETURN actor.name } AS actors
570567
"""
571568
retriever = VectorCypherRetriever(
572569
driver,
@@ -593,8 +590,8 @@ The `content` field is a formatted string containing the key information intende
593590
.. code:: python
594591
595592
def result_formatter(record: neo4j.Record) -> RetrieverResultItem:
593+
content=f"Movie title: {record.get('movieTitle')}, description: {record.get('movieDescription')}, actors: {record.get('actors')}",
596594
return RetrieverResultItem(
597-
content=f"Movie title: {record.get('movieTitle')}, description: {record.get('movieDescription')}, actors: {record.get('actors')}",
598595
metadata={
599596
"title": record.get('movieTitle'),
600597
"score": record.get("score"),
@@ -604,7 +601,7 @@ The `content` field is a formatted string containing the key information intende
604601
retriever = VectorCypherRetriever(
605602
driver,
606603
index_name=INDEX_NAME,
607-
retrieval_query="MATCH (node)<-[:ACTED_IN]-(p:Person) RETURN node.title as movieTitle, node.plot as movieDescription, collect(p.name) as actors, score",
604+
retrieval_query="OPTIONAL MATCH (node)<-[:ACTED_IN]-(p:Person) RETURN node.title as movieTitle, node.plot as movieDescription, collect(p.name) as actors, score",
608605
result_formatter=result_formatter,
609606
)
610607
@@ -763,7 +760,7 @@ the graph and return more context:
763760
driver,
764761
INDEX_NAME,
765762
FULLTEXT_INDEX_NAME,
766-
retrieval_query="MATCH (node)-[:AUTHORED_BY]->(author:Author)" "RETURN author.name"
763+
retrieval_query="MATCH (node)-[:AUTHORED_BY]->(author:Author) RETURN author.name"
767764
embedder=embedder,
768765
)
769766

examples/customize/retrievers/result_formatter_vector_cypher_retriever.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121

2222
# for each Movie node matched by the vector search, retrieve more context:
2323
# the name of all actors starring in that movie
24-
RETRIEVAL_QUERY = " MATCH (node)<-[:ACTED_IN]-(p:Person) RETURN node.title as movieTitle, node.plot as moviePlot, collect(p.name) as actors, score as similarityScore"
24+
RETRIEVAL_QUERY = """
25+
RETURN node.title as movieTitle,
26+
node.plot as moviePlot,
27+
collect { MATCH (actor:Actor)-[:ACTED_IN]->(node) RETURN actor.name } AS actors,
28+
score as similarityScore
29+
"""
2530

2631

2732
def my_result_formatter(record: neo4j.Record) -> RetrieverResultItem:

examples/retrieve/hybrid_cypher_retriever.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222

2323
# for each Movie node matched by the vector search, retrieve more context:
2424
# the name of all actors starring in that movie
25-
RETRIEVAL_QUERY = " MATCH (node)<-[:ACTED_IN]-(p:Person) RETURN node.title as movieTitle, node.plot as moviePlot, collect(p.name) as actors, score as similarityScore"
25+
RETRIEVAL_QUERY = """
26+
RETURN node.title as movieTitle,
27+
node.plot as moviePlot,
28+
collect { MATCH (actor:Actor)-[:ACTED_IN]->(node) RETURN actor.name } AS actors,
29+
score as similarityScore
30+
"""
2631

2732
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
2833
# Initialize the retriever

examples/retrieve/vector_cypher_retriever.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

2121
# for each Movie node matched by the vector search, retrieve more context:
2222
# the name of all actors starring in that movie
23-
RETRIEVAL_QUERY = " MATCH (node)<-[:ACTED_IN]-(p:Person) RETURN node.title as movieTitle, node.plot as moviePlot, collect(p.name) as actors, score as similarityScore"
23+
RETRIEVAL_QUERY = """
24+
RETURN node.title as movieTitle,
25+
node.plot as moviePlot,
26+
collect { MATCH (actor:Actor)-[:ACTED_IN]->(node) RETURN actor.name } AS actors,
27+
score as similarityScore
28+
"""
2429

2530
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
2631
# Initialize the retriever

0 commit comments

Comments
 (0)