From 9e54c227f1b422d4fa083e7821595f140445e6b9 Mon Sep 17 00:00:00 2001 From: ehude Date: Thu, 22 Feb 2024 02:33:33 +0200 Subject: [PATCH] community[patch]: Bug Neo4j VectorStore when having multiple indexes the sort is not working and the store that returned is random (#17396) Bug fix: when having multiple indexes the sort is not working and the store that returned is random. The following small fix resolves the issue. --- .../vectorstores/neo4j_vector.py | 2 +- .../vectorstores/test_neo4jvector.py | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/vectorstores/neo4j_vector.py b/libs/community/langchain_community/vectorstores/neo4j_vector.py index bb8f1b9b30..5e9057c533 100644 --- a/libs/community/langchain_community/vectorstores/neo4j_vector.py +++ b/libs/community/langchain_community/vectorstores/neo4j_vector.py @@ -77,7 +77,7 @@ def sort_by_index_name( lst: List[Dict[str, Any]], index_name: str ) -> List[Dict[str, Any]]: """Sort first element to match the index_name if exists""" - return sorted(lst, key=lambda x: x.get("index_name") != index_name) + return sorted(lst, key=lambda x: x.get("name") != index_name) def remove_lucene_chars(text: str) -> str: diff --git a/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py b/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py index cb0c79a3a0..13aef52a40 100644 --- a/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py +++ b/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py @@ -678,3 +678,46 @@ def test_hybrid_score_normalization() -> None: # Both FT and Vector must return 1.0 score assert output == [{"text": "foo", "score": 1.0}, {"text": "foo", "score": 1.0}] drop_vector_indexes(docsearch) + + +def test_index_fetching() -> None: + """testing correct index creation and fetching""" + embeddings = FakeEmbeddings() + + def create_store( + node_label: str, index: str, text_properties: List[str] + ) -> Neo4jVector: + return Neo4jVector.from_existing_graph( + embedding=embeddings, + url=url, + username=username, + password=password, + index_name=index, + node_label=node_label, + text_node_properties=text_properties, + embedding_node_property="embedding", + ) + + def fetch_store(index_name: str) -> Neo4jVector: + store = Neo4jVector.from_existing_index( + embedding=embeddings, + url=url, + username=username, + password=password, + index_name=index_name, + ) + return store + + # create index 0 + index_0_str = "index0" + create_store("label0", index_0_str, ["text"]) + + # create index 1 + index_1_str = "index1" + create_store("label1", index_1_str, ["text"]) + + index_1_store = fetch_store(index_1_str) + assert index_1_store.index_name == index_1_str + + index_0_store = fetch_store(index_0_str) + assert index_0_store.index_name == index_0_str