-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
jacoblee93
merged 4 commits into
langchain-ai:main
from
huuyafwww:bugfix/support_opensearch_serverless
Feb 9, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ interface VectorSearchOptions { | |
*/ | ||
export interface OpenSearchClientArgs { | ||
readonly client: Client; | ||
readonly service?: "es" | "aoss"; | ||
readonly indexName?: string; | ||
|
||
readonly vectorSearchOptions?: VectorSearchOptions; | ||
|
@@ -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; | ||
|
@@ -96,6 +100,7 @@ export class OpenSearchVectorStore extends VectorStore { | |
|
||
this.client = args.client; | ||
this.indexName = args.indexName ?? "documents"; | ||
this.isAoss = (args.service ?? "es") === "aoss"; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #2302 to comply with the fixes. |
||
} | ||
|
||
/** | ||
|
@@ -276,10 +295,16 @@ export class OpenSearchVectorStore extends VectorStore { | |
{ | ||
// map all metadata properties to be keyword | ||
"metadata.*": { | ||
match_mapping_type: "*", | ||
match_mapping_type: "string", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" }, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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