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]: Support OpenSearch Serverless #4229

Merged
Merged
Changes from all 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
53 changes: 39 additions & 14 deletions libs/langchain-community/src/vectorstores/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface VectorSearchOptions {
*/
export interface OpenSearchClientArgs {
readonly client: Client;
readonly service?: "es" | "aoss";
readonly indexName?: string;

readonly vectorSearchOptions?: VectorSearchOptions;
Expand Down Expand Up @@ -71,6 +72,9 @@ export class OpenSearchVectorStore extends VectorStore {

private readonly indexName: string;

// if true, use the Amazon OpenSearch Serverless service instead of es
private readonly isAoss: boolean;

private readonly engine: OpenSearchEngine;

private readonly spaceType: OpenSearchSpaceType;
Expand All @@ -96,6 +100,7 @@ export class OpenSearchVectorStore extends VectorStore {

this.client = args.client;
this.indexName = args.indexName ?? "documents";
this.isAoss = (args.service ?? "es") === "aoss";
}

/**
Expand Down Expand Up @@ -136,21 +141,35 @@ export class OpenSearchVectorStore extends VectorStore {
);
const documentIds =
options?.ids ?? Array.from({ length: vectors.length }, () => uuid.v4());
const operations = vectors.flatMap((embedding, idx) => [
{
index: {
_index: this.indexName,
_id: documentIds[idx],
const operations = vectors.flatMap((embedding, idx) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const document: Record<string, any> = [
{
index: {
_index: this.indexName,
_id: documentIds[idx],
},
},
},
{
embedding,
metadata: documents[idx].metadata,
text: documents[idx].pageContent,
},
]);
{
embedding,
metadata: documents[idx].metadata,
text: documents[idx].pageContent,
},
];

// aoss does not support document id
if (this.isAoss) {
delete document[0].index?._id;
}
Comment on lines +161 to +163
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following errors that occurred when using OpenSearch Serverless have been addressed

Document ID is not supported in create/index operation request


return document;
});
await this.client.bulk({ body: operations });
await this.client.indices.refresh({ index: this.indexName });

// aoss does not support refresh
if (!this.isAoss) {
await this.client.indices.refresh({ index: this.indexName });
}
Comment on lines +169 to +172
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2302 to comply with the fixes.

}

/**
Expand Down Expand Up @@ -276,10 +295,16 @@ export class OpenSearchVectorStore extends VectorStore {
{
// map all metadata properties to be keyword
"metadata.*": {
match_mapping_type: "*",
match_mapping_type: "string",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this break anything?

mapping: { type: "keyword" },
},
},
{
"metadata.loc": {
match_mapping_type: "object",
mapping: { type: "object" },
},
},
],
properties: {
text: { type: "text" },
Expand Down
Loading