Skip to content

Commit

Permalink
Update deprecated Cypher query string for Delete query
Browse files Browse the repository at this point in the history
  • Loading branch information
willtai committed Nov 21, 2024
1 parent e01623c commit ee66ce2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
### Added

- Migrated all Neo4j-related code, including tests and integration tests, from the LangChain Community package to this package.

Updated
Improved subquery building for import queries to support different Neo4j versions.
Added unit tests for Neo4j version-specific query construction.
22 changes: 17 additions & 5 deletions libs/neo4j/langchain_neo4j/vectorstores/neo4j_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,17 +594,29 @@ def __init__(
if pre_delete_collection:
from neo4j.exceptions import DatabaseError

self.query(
f"MATCH (n:`{self.node_label}`) "
"CALL (n) { DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)
delete_query = self._build_delete_query()
self.query(delete_query)
# Delete index
try:
self.query(f"DROP INDEX {self.index_name}")
except DatabaseError: # Index didn't exist yet
pass

def _build_delete_query(self) -> str:
if self.neo4j_version_is_5_23_or_above:
query = (
f"MATCH (n:`{self.node_label}`) "
"CALL (n) { DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)
else:
query = (
f"MATCH (n:`{self.node_label}`) "
"CALL { WITH n DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)
return query

def query(
self,
query: str,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"
services:
neo4j:
image: neo4j:5.24.2
image: neo4j:5.22
restart: on-failure:0
hostname: neo4j-test
container_name: neo4j-test
Expand Down
28 changes: 28 additions & 0 deletions libs/neo4j/tests/unit_tests/vectorstores/test_neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,31 @@ def test_build_import_query_version_below_5_23(mock_vector_store: Neo4jVector) -
actual_query = mock_vector_store._build_import_query()

assert actual_query == expected_query


def test_build_delete_query_version_is_or_above_5_23(
mock_vector_store: Neo4jVector,
) -> None:
mock_vector_store.neo4j_version_is_5_23_or_above = True
expected_query = (
f"MATCH (n:`{mock_vector_store.node_label}`) "
"CALL (n) { DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)

actual_query = mock_vector_store._build_delete_query()

assert actual_query == expected_query


def test_build_delete_query_version_below_5_23(mock_vector_store: Neo4jVector) -> None:
mock_vector_store.neo4j_version_is_5_23_or_above = False
expected_query = (
f"MATCH (n:`{mock_vector_store.node_label}`) "
"CALL { WITH n DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)

actual_query = mock_vector_store._build_delete_query()

assert actual_query == expected_query

0 comments on commit ee66ce2

Please sign in to comment.