From 2d572603db73bd8523d67f585b0063e40ca9ef3e Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Wed, 4 Sep 2024 12:29:04 -0700 Subject: [PATCH] fix(community): ElasticVectorSearch: exclude metadata filters not working due changing to term instead of terms (#6688) Co-authored-by: Laurens Tsestigh <90600075+LaurensTsestigh@users.noreply.github.com> --- .../langchain-community/src/vectorstores/elasticsearch.ts | 7 ++++--- .../src/vectorstores/tests/elasticsearch.int.test.ts | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libs/langchain-community/src/vectorstores/elasticsearch.ts b/libs/langchain-community/src/vectorstores/elasticsearch.ts index 14a4ee12ac03..cccc439bb7a9 100644 --- a/libs/langchain-community/src/vectorstores/elasticsearch.ts +++ b/libs/langchain-community/src/vectorstores/elasticsearch.ts @@ -343,10 +343,11 @@ export class ElasticVectorSearch extends VectorStore { }, }); } else if (condition.operator === "exclude") { + const toExclude = { [metadataField]: condition.value }; must_not.push({ - term: { - [metadataField]: condition.value, - }, + ...(Array.isArray(condition.value) + ? { terms: toExclude } + : { term: toExclude }), }); } else if (condition.operator === "or") { should.push({ diff --git a/libs/langchain-community/src/vectorstores/tests/elasticsearch.int.test.ts b/libs/langchain-community/src/vectorstores/tests/elasticsearch.int.test.ts index 08ec39ac0f57..41045196892a 100644 --- a/libs/langchain-community/src/vectorstores/tests/elasticsearch.int.test.ts +++ b/libs/langchain-community/src/vectorstores/tests/elasticsearch.int.test.ts @@ -114,6 +114,14 @@ describe("ElasticVectorSearch", () => { }, ]); expect(results2).toHaveLength(1); + const results3 = await store.similaritySearch("*", 11, [ + { + field: "a", + value: [createdAt], + operator: "exclude", + }, + ]); + expect(results3).toHaveLength(1); }); test.skip("ElasticVectorSearch integration with text splitting metadata", async () => {