-
Notifications
You must be signed in to change notification settings - Fork 2k
OpenSearch: Omit explicit document IDs when manageDocumentIds=false for AWS Serverless (issue - #3818) #4220
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
Open
lsh1215
wants to merge
2
commits into
spring-projects:main
Choose a base branch
from
lsh1215:fix/opensearch-serverless-document-id-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
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 hidden or 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 |
---|---|---|
|
@@ -102,6 +102,16 @@ | |
* }</pre> | ||
* | ||
* <p> | ||
* AWS OpenSearch Serverless usage example: | ||
* </p> | ||
* <pre>{@code | ||
* OpenSearchVectorStore vectorStore = OpenSearchVectorStore.builder(openSearchClient, embeddingModel) | ||
* .initializeSchema(true) | ||
* .manageDocumentIds(false) // Required for AWS OpenSearch Serverless | ||
* .build(); | ||
* }</pre> | ||
* | ||
* <p> | ||
* Advanced configuration example: | ||
* </p> | ||
* <pre>{@code | ||
|
@@ -170,6 +180,8 @@ public class OpenSearchVectorStore extends AbstractObservationVectorStore implem | |
|
||
private String similarityFunction; | ||
|
||
private final boolean manageDocumentIds; | ||
|
||
/** | ||
* Creates a new OpenSearchVectorStore using the builder pattern. | ||
* @param builder The configured builder instance | ||
|
@@ -187,6 +199,7 @@ protected OpenSearchVectorStore(Builder builder) { | |
// https://opensearch.org/docs/latest/search-plugins/knn/approximate-knn/#spaces | ||
this.similarityFunction = builder.similarityFunction; | ||
this.initializeSchema = builder.initializeSchema; | ||
this.manageDocumentIds = builder.manageDocumentIds; | ||
} | ||
|
||
/** | ||
|
@@ -210,14 +223,27 @@ public void doAdd(List<Document> documents) { | |
for (Document document : documents) { | ||
OpenSearchDocument openSearchDocument = new OpenSearchDocument(document.getId(), document.getText(), | ||
document.getMetadata(), embedding.get(documents.indexOf(document))); | ||
bulkRequestBuilder.operations(op -> op | ||
.index(idx -> idx.index(this.index).id(openSearchDocument.id()).document(openSearchDocument))); | ||
|
||
// Conditionally set document ID based on manageDocumentIds flag | ||
if (this.manageDocumentIds) { | ||
bulkRequestBuilder.operations(op -> op | ||
.index(idx -> idx.index(this.index).id(openSearchDocument.id()).document(openSearchDocument))); | ||
} | ||
else { | ||
bulkRequestBuilder | ||
.operations(op -> op.index(idx -> idx.index(this.index).document(openSearchDocument))); | ||
} | ||
} | ||
bulkRequest(bulkRequestBuilder.build()); | ||
} | ||
|
||
@Override | ||
public void doDelete(List<String> idList) { | ||
if (!this.manageDocumentIds) { | ||
logger.warn("Document ID management is disabled. Delete operations may not work as expected " | ||
+ "since document IDs are auto-generated by OpenSearch. Consider using filter-based deletion instead."); | ||
} | ||
|
||
BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder(); | ||
for (String id : idList) { | ||
bulkRequestBuilder.operations(op -> op.delete(idx -> idx.index(this.index).id(id))); | ||
|
@@ -417,6 +443,8 @@ public static class Builder extends AbstractVectorStoreBuilder<Builder> { | |
|
||
private String similarityFunction = COSINE_SIMILARITY_FUNCTION; | ||
|
||
private boolean manageDocumentIds = false; | ||
|
||
|
||
/** | ||
* Sets the OpenSearch client. | ||
* @param openSearchClient The OpenSearch client to use | ||
|
@@ -488,6 +516,28 @@ public Builder similarityFunction(String similarityFunction) { | |
return this; | ||
} | ||
|
||
/** | ||
* Sets whether to manage document IDs during indexing operations. | ||
* <p> | ||
* When set to {@code true} (default), document IDs will be explicitly set during | ||
* indexing operations. When set to {@code false}, OpenSearch will auto-generate | ||
* document IDs, which is required for AWS OpenSearch Serverless vector search | ||
* collections. | ||
* </p> | ||
* <p> | ||
* Note: When document ID management is disabled, the {@link #doDelete(List)} | ||
* method may not work as expected since document IDs are auto-generated by | ||
* OpenSearch. | ||
* </p> | ||
* @param manageDocumentIds true to manage document IDs (default), false to let | ||
* OpenSearch auto-generate IDs | ||
* @return The builder instance | ||
*/ | ||
public Builder manageDocumentIds(boolean manageDocumentIds) { | ||
this.manageDocumentIds = manageDocumentIds; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds a new OpenSearchVectorStore instance with the configured properties. | ||
* @return A new OpenSearchVectorStore instance | ||
|
This file contains hidden or 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
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.
Shouldn't this be
true
by default to let Spring AI managing the documentIDs and for backwards compatibility?.