From 77449439c9a3e6e76be70e23196f6f3d84fbd947 Mon Sep 17 00:00:00 2001
From: Tomaz Bratanic <bratanic.tomaz@gmail.com>
Date: Tue, 13 Feb 2024 08:39:32 +0100
Subject: [PATCH] Fix neo4j vector for multiple indexes

---
 .../src/vectorstores/neo4j_vector.ts          |  3 +-
 .../tests/neo4j_vector.int.test.ts            | 80 +++++++++++++++++++
 2 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/libs/langchain-community/src/vectorstores/neo4j_vector.ts b/libs/langchain-community/src/vectorstores/neo4j_vector.ts
index 75ef364b1d12..84e02a06684f 100644
--- a/libs/langchain-community/src/vectorstores/neo4j_vector.ts
+++ b/libs/langchain-community/src/vectorstores/neo4j_vector.ts
@@ -519,8 +519,7 @@ export class Neo4jVectorStore extends VectorStore {
   ): Array<{ [key: string]: any }> {
     return values.sort(
       (a, b) =>
-        (a.index_name === indexName ? -1 : 0) -
-        (b.index_name === indexName ? -1 : 0)
+        (a.name === indexName ? -1 : 0) - (b.name === indexName ? -1 : 0)
     );
   }
 
diff --git a/libs/langchain-community/src/vectorstores/tests/neo4j_vector.int.test.ts b/libs/langchain-community/src/vectorstores/tests/neo4j_vector.int.test.ts
index 2cbc6406e719..f73b38847e50 100644
--- a/libs/langchain-community/src/vectorstores/tests/neo4j_vector.int.test.ts
+++ b/libs/langchain-community/src/vectorstores/tests/neo4j_vector.int.test.ts
@@ -516,3 +516,83 @@ test.skip("Test escape lucene characters", async () => {
   await dropVectorIndexes(neo4jVectorStore);
   await neo4jVectorStore.close();
 });
+
+test.skip("Test multiple index", async () => {
+  const url = process.env.NEO4J_URI as string;
+  const username = process.env.NEO4J_USERNAME as string;
+  const password = process.env.NEO4J_PASSWORD as string;
+
+  expect(url).toBeDefined();
+  expect(username).toBeDefined();
+  expect(password).toBeDefined();
+
+  const embeddings = new FakeEmbeddingsWithOsDimension();
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  const metadatas: any[] = [];
+
+  const foo = await Neo4jVectorStore.fromTexts(["foo"], metadatas, embeddings, {
+    url,
+    username,
+    password,
+    indexName: "Foo",
+    nodeLabel: "Foo",
+  });
+
+  const bar = await Neo4jVectorStore.fromTexts(["bar"], metadatas, embeddings, {
+    url,
+    username,
+    password,
+    indexName: "Bar",
+    nodeLabel: "Bar",
+  });
+
+  const fooExistingIndex = await Neo4jVectorStore.fromExistingIndex(
+    embeddings,
+    {
+      url,
+      username,
+      password,
+      indexName: "Foo",
+    }
+  );
+
+  const fooOutput = await fooExistingIndex.similaritySearch(
+    "This is the end of the world!",
+    1
+  );
+  const fooExpectedResult = [
+    new Document({
+      pageContent: "foo",
+      metadata: {},
+    }),
+  ];
+  expect(fooOutput).toStrictEqual(fooExpectedResult);
+
+  const barExistingIndex = await Neo4jVectorStore.fromExistingIndex(
+    embeddings,
+    {
+      url,
+      username,
+      password,
+      indexName: "Bar",
+    }
+  );
+
+  const barOutput = await barExistingIndex.similaritySearch(
+    "This is the end of the world!",
+    1
+  );
+  const barExpectedResult = [
+    new Document({
+      pageContent: "bar",
+      metadata: {},
+    }),
+  ];
+  expect(barOutput).toStrictEqual(barExpectedResult);
+
+  await dropVectorIndexes(barExistingIndex);
+  await foo.close();
+  await bar.close();
+  await barExistingIndex.close();
+  await fooExistingIndex.close();
+});