Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community[patch]: add support for or in elastic vector query #5568

Merged
merged 5 commits into from
May 29, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions libs/langchain-community/src/vectorstores/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ export class ElasticVectorSearch extends VectorStore {
must: { [operator: string]: { [field: string]: any } }[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
must_not: { [operator: string]: { [field: string]: any } }[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
should?: { [operator: string]: { [field: string]: any } }[];
} {
if (filter == null) return { must: [], must_not: [] };
const filters = Array.isArray(filter)
Expand All @@ -328,6 +330,7 @@ export class ElasticVectorSearch extends VectorStore {

const must = [];
const must_not = [];
const should = [];
for (const condition of filters) {
const metadataField = `metadata.${condition.field}`;
if (condition.operator === "exists") {
Expand All @@ -342,15 +345,27 @@ export class ElasticVectorSearch extends VectorStore {
[metadataField]: condition.value,
},
});
} else {
} else if (condition.operator === "or") {
should.push({
term: {
[metadataField]: condition.value,
},
});
}else {
must.push({
[condition.operator]: {
[metadataField]: condition.value,
},
});
}
}
return { must, must_not };
const result = { must, must_not };

if(should.length > 0){
djaffer marked this conversation as resolved.
Show resolved Hide resolved
result.should = should;
result.minimum_should_match = 1;
}
return result;
}

/**
Expand Down