This is an automated email from the ASF dual-hosted git repository.
Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 83500a2d6896 CAMEL-27242: camel-spring-ai-vector-store - UPSERT and
vector search with the vectors of spring-ai-embeddings
83500a2d6896 is described below
commit 83500a2d6896793a9866ce6d649b826c0c705b86
Author: croway <[email protected]>
AuthorDate: Thu Nov 19 14:56:36 2026 +0100
CAMEL-27242: camel-spring-ai-vector-store - UPSERT and vector search with
the vectors of spring-ai-embeddings
UPSERT writes the float[] (or List of float[]) body of spring-ai-embeddings
with
VectorStore.upsert as it is, so the vector store does not embed the text
again.
It also takes an EmbeddedDocument or a List of EmbeddedDocument. The ids
come
from the new CamelSpringAiVectorStoreUpsertDocumentId(s) headers (a random
UUID
without them), not from CamelSpringAiVectorStoreDocumentIds, which ADD,
UPSERT
and SIMILARITY_SEARCH set to their output ids.
The searchByVector option (default false) makes SIMILARITY_SEARCH of a
float[]
body search with the vector instead of the text of the
CamelSpringAiEmbeddingInputText header.
A vector store without upsert or vector search fails with an
UnsupportedOperationException that points to ADD or to a text search. ADD
and
the default SIMILARITY_SEARCH are unchanged.
---
.../catalog/components/spring-ai-vector-store.json | 38 +-
.../docs/spring-ai-vector-store-component.adoc | 128 +++++-
.../camel-spring-ai-vector-store/pom.xml | 5 +
.../SpringAiVectorStoreComponentConfigurer.java | 6 +
...SpringAiVectorStoreConfigurationConfigurer.java | 6 +
.../SpringAiVectorStoreEndpointConfigurer.java | 6 +
.../SpringAiVectorStoreEndpointUriFactory.java | 3 +-
.../vectorstore/spring-ai-vector-store.json | 38 +-
.../docs/spring-ai-vector-store-component.adoc | 128 +++++-
.../SpringAiVectorStoreConfiguration.java | 22 +-
.../vectorstore/SpringAiVectorStoreEndpoint.java | 17 +-
.../vectorstore/SpringAiVectorStoreHeaders.java | 19 +-
.../vectorstore/SpringAiVectorStoreOperation.java | 3 +-
.../vectorstore/SpringAiVectorStoreProducer.java | 160 ++++++-
.../vectorstore/QdrantVectorStoreUpsertIT.java | 199 +++++++++
.../vectorstore/SpringAiVectorStoreUpsertTest.java | 497 +++++++++++++++++++++
...SpringAiVectorStoreComponentBuilderFactory.java | 25 +-
.../SpringAiVectorStoreEndpointBuilderFactory.java | 82 +++-
18 files changed, 1314 insertions(+), 68 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-ai-vector-store.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-ai-vector-store.json
index 4f2c718e705c..02de5286b478 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-ai-vector-store.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-ai-vector-store.json
@@ -27,33 +27,37 @@
"configuration": { "index": 0, "kind": "property", "displayName":
"Configuration", "group": "producer", "label": "", "required": false, "type":
"object", "javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"deprecated": false, "autowired": false, "secret": false, "description": "The
configuration." },
"filterExpression": { "index": 1, "kind": "property", "displayName":
"Filter Expression", "group": "producer", "label": "", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Filter expression for
metadata-based filtering in searches." },
"lazyStartProducer": { "index": 2, "kind": "property", "displayName":
"Lazy Start Producer", "group": "producer", "label": "producer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description":
"Whether the producer should be started lazy (on the first message). By
starting lazy you can use this to allow CamelContext and routes to startup in
situations where a producer may otherwise fail [...]
- "operation": { "index": 3, "kind": "property", "displayName": "Operation",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH" ], "deprecated": false,
"autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationF [...]
- "similarityThreshold": { "index": 4, "kind": "property", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarity [...]
- "topK": { "index": 5, "kind": "property", "displayName": "Top K", "group":
"producer", "label": "", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
- "vectorStore": { "index": 6, "kind": "property", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
- "autowiredEnabled": { "index": 7, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Whether autowiring is enabled. This is used for automatic autowiring options
(the option must be marked as autowired) by looking up in the registry to find
if there is a single instance of matching t [...]
+ "operation": { "index": 3, "kind": "property", "displayName": "Operation",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH", "UPSERT" ], "deprecated":
false, "autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"conf [...]
+ "searchByVector": { "index": 4, "kind": "property", "displayName": "Search
By Vector", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Whether
SIMILARITY_SEARCH of a float[] body, such as the vector [...]
+ "similarityThreshold": { "index": 5, "kind": "property", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarity [...]
+ "topK": { "index": 6, "kind": "property", "displayName": "Top K", "group":
"producer", "label": "", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
+ "vectorStore": { "index": 7, "kind": "property", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
+ "autowiredEnabled": { "index": 8, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Whether autowiring is enabled. This is used for automatic autowiring options
(the option must be marked as autowired) by looking up in the registry to find
if there is a single instance of matching t [...]
},
"headers": {
- "CamelSpringAiVectorStoreOperation": { "index": 0, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH" ], "deprecated": false,
"autowired": false, "secret": false, "description": "The operation to perform
(ADD, DELETE, SIMILARITY_SEARCH)", "constantName":
"org.apache.camel.component.springai.vectorstore.Spring [...]
+ "CamelSpringAiVectorStoreOperation": { "index": 0, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH", "UPSERT" ], "deprecated":
false, "autowired": false, "secret": false, "description": "The operation to
perform (ADD, DELETE, SIMILARITY_SEARCH, UPSERT)", "constantName":
"org.apache.camel.component.springai. [...]
"CamelSpringAiVectorStoreTopK": { "index": 1, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "Integer", "deprecated": false, "autowired": false, "secret":
false, "description": "The maximum number of similar documents to return
(topK)", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#TOP_K"
},
"CamelSpringAiVectorStoreSimilarityThreshold": { "index": 2, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "Double", "deprecated": false, "autowired": false, "secret":
false, "description": "The similarity threshold (0-1)", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#SIMILARITY_THRESHOLD"
},
"CamelSpringAiVectorStoreFilterExpression": { "index": 3, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "String", "deprecated": false, "autowired": false, "secret":
false, "description": "Filter expression for metadata-based filtering",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#FILTER_EXPRESSION"
},
- "CamelSpringAiVectorStoreDocumentIds": { "index": 4, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "java.util.List<String>", "deprecated": false, "autowired": false,
"secret": false, "description": "List of document IDs (output of ADD and
SIMILARITY_SEARCH, and input of DELETE when the message has no body)",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENT_IDS"
},
- "CamelSpringAiVectorStoreSimilarDocuments": { "index": 5, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "java.util.List<org.springframework.ai.document.Document>",
"deprecated": false, "autowired": false, "secret": false, "description": "List
of similar documents found", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#SIMILAR_DOCUMENTS"
},
- "CamelSpringAiVectorStoreDocumentsAdded": { "index": 6, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "Integer", "deprecated": false, "autowired": false, "secret":
false, "description": "Number of documents added", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_ADDED"
},
- "CamelSpringAiVectorStoreDocumentsDeleted": { "index": 7, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "Integer", "deprecated": false, "autowired": false,
"secret": false, "description": "Number of document IDs that DELETE gave to the
vector store, as the vector store does not tell how many of them existed",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_DELETED"
}
+ "CamelSpringAiVectorStoreUpsertDocumentId": { "index": 4, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "String", "deprecated": false, "autowired": false, "secret":
false, "description": "The id of the document that UPSERT writes from a float[]
body: the document with this id is replaced. Without it, the document gets a
random UUID", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#UPSE
[...]
+ "CamelSpringAiVectorStoreUpsertDocumentIds": { "index": 5, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "java.util.List<String>", "deprecated": false, "autowired":
false, "secret": false, "description": "The ids of the documents that UPSERT
writes from a List of float[] body, one per vector: the documents with these
ids are replaced. Without it, each document gets a random UUID",
"constantName": "org.apache.camel.component.spri [...]
+ "CamelSpringAiVectorStoreDocumentIds": { "index": 6, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "java.util.List<String>", "deprecated": false, "autowired": false,
"secret": false, "description": "List of document IDs (output of ADD, UPSERT
and SIMILARITY_SEARCH, and input of DELETE when the message has no body)",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENT_IDS"
},
+ "CamelSpringAiVectorStoreSimilarDocuments": { "index": 7, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "java.util.List<org.springframework.ai.document.Document>",
"deprecated": false, "autowired": false, "secret": false, "description": "List
of similar documents found", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#SIMILAR_DOCUMENTS"
},
+ "CamelSpringAiVectorStoreDocumentsAdded": { "index": 8, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "Integer", "deprecated": false, "autowired": false, "secret":
false, "description": "Number of documents added by ADD, or written by UPSERT",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_ADDED"
},
+ "CamelSpringAiVectorStoreDocumentsDeleted": { "index": 9, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "Integer", "deprecated": false, "autowired": false,
"secret": false, "description": "Number of document IDs that DELETE gave to the
vector store, as the vector store does not tell how many of them existed",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_DELETED"
}
},
"bodyTypes": {
- "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "DELETE", "carriers": [ "text", "list", "pojo", "null" ]
}, { "option": "operation", "value": "SIMILARITY_SEARCH", "carriers": [ "text",
"list" ] } ], "description": "ADD takes a Document, a List of Document or of
texts, or text (another body is added as its text); a List with another element
fails. After spring-ai-embeddings it adds the float[] (or List of float[]) body
with the text of the CamelS [...]
- "producerOut": { "carriers": [ "unchanged" ], "byOption": [ { "option":
"operation", "value": "SIMILARITY_SEARCH", "carriers": [ "list" ] } ],
"description": "SIMILARITY_SEARCH gives the List of Document found. ADD and
DELETE keep the body, with the ids and the count in headers" }
+ "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "DELETE", "carriers": [ "text", "list", "pojo", "null" ]
}, { "option": "operation", "value": "SIMILARITY_SEARCH", "carriers": [ "text",
"list" ] }, { "option": "operation", "value": "UPSERT", "carriers": [ "list",
"pojo" ] } ], "description": "ADD takes a Document, a List of Document or of
texts, or text (another body is added as its text); a List with another element
fails. After spring-ai-embedd [...]
+ "producerOut": { "carriers": [ "unchanged" ], "byOption": [ { "option":
"operation", "value": "SIMILARITY_SEARCH", "carriers": [ "list" ] } ],
"description": "SIMILARITY_SEARCH gives the List of Document found. ADD, UPSERT
and DELETE keep the body, with the ids and the count in headers" }
},
"properties": {
"storeId": { "index": 0, "kind": "path", "displayName": "Store Id",
"group": "producer", "label": "", "required": true, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "The id" },
"filterExpression": { "index": 1, "kind": "parameter", "displayName":
"Filter Expression", "group": "producer", "label": "", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Filter expression for
metadata-based filtering in searches." },
- "operation": { "index": 2, "kind": "parameter", "displayName":
"Operation", "group": "producer", "label": "", "required": false, "type":
"enum", "javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH" ], "deprecated": false,
"autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configuration [...]
- "similarityThreshold": { "index": 3, "kind": "parameter", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarit [...]
- "topK": { "index": 4, "kind": "parameter", "displayName": "Top K",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
- "vectorStore": { "index": 5, "kind": "parameter", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
- "lazyStartProducer": { "index": 6, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Whether the producer should be started
lazy (on the first message). By starting lazy you can use this to allow
CamelContext and routes to startup in situations where a produc [...]
+ "operation": { "index": 2, "kind": "parameter", "displayName":
"Operation", "group": "producer", "label": "", "required": false, "type":
"enum", "javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH", "UPSERT" ], "deprecated":
false, "autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"con [...]
+ "searchByVector": { "index": 3, "kind": "parameter", "displayName":
"Search By Vector", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Whether
SIMILARITY_SEARCH of a float[] body, such as the vector [...]
+ "similarityThreshold": { "index": 4, "kind": "parameter", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarit [...]
+ "topK": { "index": 5, "kind": "parameter", "displayName": "Top K",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
+ "vectorStore": { "index": 6, "kind": "parameter", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
+ "lazyStartProducer": { "index": 7, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Whether the producer should be started
lazy (on the first message). By starting lazy you can use this to allow
CamelContext and routes to startup in situations where a produc [...]
}
}
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ai-vector-store-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ai-vector-store-component.adoc
index 484246bb8aee..f5f41170e8b3 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ai-vector-store-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ai-vector-store-component.adoc
@@ -21,7 +21,7 @@ Vector stores enable:
* Storage of documents with embeddings
* Similarity search based on vector distance
* Metadata-based filtering
-* Document management (add, delete)
+* Document management (add, upsert, delete)
== URI format
@@ -40,7 +40,7 @@ include::partial$component-endpoint-headers.adoc[]
== Operations
-The component supports three main operations:
+The component supports four operations:
=== ADD Operation
@@ -56,10 +56,39 @@ a `float[]` body with the `CamelSpringAiEmbeddingInputText`
header, or a List of
The `CamelSpringAiEmbeddingInputText(s)` headers are only used together with a
vector body, so a header left over from
an earlier spring-ai-embeddings call does not replace a Document or text body.
+ADD of a vector stores its text, and the vector store embeds the text again
with its own embedding model. The vector
+only goes in the `embedding` metadata of the document. To store the vector
itself, use UPSERT.
+
After documents are added, the component sets the
`CamelSpringAiVectorStoreDocumentIds` header containing a list of the IDs of
the added documents. This allows you to reference or track the stored documents
in subsequent processing steps.
NOTE: *Best Practice:* Use the ADD operation directly with the vector store
component when working with text. The component will automatically handle
embedding generation internally if the vector store is configured with an
embedding model. This is the recommended approach as it simplifies your routes
and reduces the number of steps.
+=== UPSERT Operation
+
+Writes vectors that are already computed, such as the output of the
spring-ai-embeddings component, as they are. The
+embedding model of the vector store is not called. A document with the same ID
is replaced, otherwise a new document is
+added. UPSERT takes:
+
+* A `float[]` body with the `CamelSpringAiEmbeddingInputText` header, the text
of the vector. The
+`CamelSpringAiVectorStoreUpsertDocumentId` header is the ID of the document.
Without it, the document gets a random UUID.
+* A List of `float[]` body with the `CamelSpringAiEmbeddingInputTexts` header,
with one text per vector. The
+`CamelSpringAiVectorStoreUpsertDocumentIds` header is the List of IDs, with
one ID per vector. Without it, each document
+gets a random UUID.
+* A Spring AI `EmbeddedDocument` (a Document with its vector), or a List of
`EmbeddedDocument`. The ID and the metadata
+are the ones of the Document, and the ID headers are not used.
+
+A vector without its text fails, and so does a List of texts or of IDs with
another size than the List of vectors.
+UPSERT does not write the `embedding` and `precomputed_embedding` metadata of
ADD.
+
+Like ADD, UPSERT sets the `CamelSpringAiVectorStoreDocumentsAdded` header to
the number of documents written, and the
+`CamelSpringAiVectorStoreDocumentIds` header to their IDs. UPSERT does not
read the IDs from
+`CamelSpringAiVectorStoreDocumentIds`, as ADD, UPSERT and SIMILARITY_SEARCH
set this header to their own output IDs.
+
+The vector store must support UPSERT. In Spring AI 2.1, the Chroma, Couchbase,
Elasticsearch, MariaDB, MongoDB Atlas,
+Neo4j, OpenSearch, PgVector, Qdrant, Redis and Typesense vector stores do.
With another vector store, such as
+`SimpleVectorStore`, UPSERT fails with an `UnsupportedOperationException` and
nothing is written: use ADD instead.
+Some vector stores only accept IDs in a given format, for example Qdrant needs
UUIDs.
+
=== DELETE Operation
Removes documents from the vector store by their IDs. The body has the
documents to delete: an ID, a Document, or a
@@ -76,11 +105,30 @@ Searches for documents similar to a query text. After the
spring-ai-embeddings c
`float[]` vector of the text, the query is the text in the
`CamelSpringAiEmbeddingInputText` header (the vector store
embeds the query itself).
+With the `searchByVector` option, the `float[]` body is the query: the vector
store searches with this vector, and does
+not embed a text. The `CamelSpringAiEmbeddingInputText` header is then not
needed. A text body is still searched as a
+text. The vector store must support the search with a vector, as for UPSERT,
otherwise the search fails with an
+`UnsupportedOperationException`. The `topK`, `similarityThreshold` and
`filterExpression` options and headers apply as
+for a text.
+
The component returns matching documents in the message body and sets the
following headers:
* `CamelSpringAiVectorStoreSimilarDocuments` - List of Document objects found
* `CamelSpringAiVectorStoreDocumentIds` - List of IDs of the found documents
+=== ADD or UPSERT
+
+* With ADD and a text query, the vector store embeds the texts with its own
embedding model, when it writes and when it
+searches. This is the simplest way.
+* With UPSERT and `searchByVector`, the route computes the vectors, for
example with the spring-ai-embeddings
+component, and the vector store writes and searches with these vectors as they
are. Use it to choose the embedding
+model in the route, or to embed each text only once.
+
+IMPORTANT: Use the same embedding model, with the same settings, to write and
to search. A vector of another model gives
+meaningless results, and no error is raised. This also applies when you mix
the operations: documents written with
+UPSERT and searched with a text use the embedding model of the vector store
for the query, and documents written with
+ADD and searched with `searchByVector` use the model of the route for the
query.
+
== Usage Examples
=== Add Documents from Text (Recommended)
@@ -176,6 +224,81 @@ YAML::
====
NOTE: This approach is only necessary when you need to access or modify the
embeddings between generation and storage. For most use cases, the direct ADD
operation (shown above) is preferred.
+With ADD, the vector store embeds the text again: to store the vectors of the
embeddings component, use UPSERT (shown below).
+
+=== Store and Search with the Vectors of the Embeddings Component
+
+UPSERT stores the vectors that the spring-ai-embeddings component computes,
and `searchByVector` searches with the
+vector of the query. The vector store does not embed any text, so it must
support UPSERT and the search with a vector.
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:store")
+ .to("spring-ai-embeddings:embed?embeddingModel=#embeddingModel")
+
.to("spring-ai-vector-store:myStore?operation=UPSERT&vectorStore=#vectorStore");
+
+from("direct:search")
+ .to("spring-ai-embeddings:embed?embeddingModel=#embeddingModel")
+
.to("spring-ai-vector-store:myStore?operation=SIMILARITY_SEARCH&searchByVector=true&topK=5&vectorStore=#vectorStore");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+ <from uri="direct:store"/>
+ <to uri="spring-ai-embeddings:embed?embeddingModel=#embeddingModel"/>
+ <to
uri="spring-ai-vector-store:myStore?operation=UPSERT&vectorStore=#vectorStore"/>
+</route>
+<route>
+ <from uri="direct:search"/>
+ <to uri="spring-ai-embeddings:embed?embeddingModel=#embeddingModel"/>
+ <to
uri="spring-ai-vector-store:myStore?operation=SIMILARITY_SEARCH&searchByVector=true&topK=5&vectorStore=#vectorStore"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: direct:store
+ steps:
+ - to:
+ uri: spring-ai-embeddings:embed
+ parameters:
+ embeddingModel: "#embeddingModel"
+ - to:
+ uri: spring-ai-vector-store:myStore
+ parameters:
+ operation: UPSERT
+ vectorStore: "#vectorStore"
+- route:
+ from:
+ uri: direct:search
+ steps:
+ - to:
+ uri: spring-ai-embeddings:embed
+ parameters:
+ embeddingModel: "#embeddingModel"
+ - to:
+ uri: spring-ai-vector-store:myStore
+ parameters:
+ operation: SIMILARITY_SEARCH
+ searchByVector: true
+ topK: 5
+ vectorStore: "#vectorStore"
+----
+====
+
+To replace a document, for example when its text changes, set its ID in the
`CamelSpringAiVectorStoreUpsertDocumentId`
+header (or the IDs in `CamelSpringAiVectorStoreUpsertDocumentIds` for a List
of texts) before UPSERT.
=== Similarity Search
@@ -305,4 +428,5 @@ IMPORTANT: Make sure your vector store is configured with
an embedding model to
* `topK` - Maximum number of results (default: 5)
* `similarityThreshold` - Minimum similarity score 0-1 (default: 0.0)
* `filterExpression` - Metadata-based filter expression
+* `searchByVector` - Search with the `float[]` body instead of the text of the
`CamelSpringAiEmbeddingInputText` header (default: false)
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/pom.xml
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/pom.xml
index d364f1177a7c..f43e0ce11f08 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/pom.xml
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/pom.xml
@@ -33,6 +33,11 @@
<name>Camel :: Spring AI :: Vector Store</name>
<description>Spring AI Vector Store</description>
+ <properties>
+ <!-- Qdrant is not available on these platforms -->
+ <skipITs.s390x>true</skipITs.s390x>
+ </properties>
+
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreComponentConfigurer.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreComponentConfigurer.java
index 538d46606fb0..beb464f78102 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreComponentConfigurer.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreComponentConfigurer.java
@@ -39,6 +39,8 @@ public class SpringAiVectorStoreComponentConfigurer extends
PropertyConfigurerSu
case "lazystartproducer":
case "lazyStartProducer":
target.setLazyStartProducer(property(camelContext, boolean.class, value));
return true;
case "operation":
getOrCreateConfiguration(target).setOperation(property(camelContext,
org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation.class,
value)); return true;
+ case "searchbyvector":
+ case "searchByVector":
getOrCreateConfiguration(target).setSearchByVector(property(camelContext,
boolean.class, value)); return true;
case "similaritythreshold":
case "similarityThreshold":
getOrCreateConfiguration(target).setSimilarityThreshold(property(camelContext,
double.class, value)); return true;
case "topk":
@@ -65,6 +67,8 @@ public class SpringAiVectorStoreComponentConfigurer extends
PropertyConfigurerSu
case "lazystartproducer":
case "lazyStartProducer": return boolean.class;
case "operation": return
org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation.class;
+ case "searchbyvector":
+ case "searchByVector": return boolean.class;
case "similaritythreshold":
case "similarityThreshold": return double.class;
case "topk":
@@ -87,6 +91,8 @@ public class SpringAiVectorStoreComponentConfigurer extends
PropertyConfigurerSu
case "lazystartproducer":
case "lazyStartProducer": return target.isLazyStartProducer();
case "operation": return
getOrCreateConfiguration(target).getOperation();
+ case "searchbyvector":
+ case "searchByVector": return
getOrCreateConfiguration(target).isSearchByVector();
case "similaritythreshold":
case "similarityThreshold": return
getOrCreateConfiguration(target).getSimilarityThreshold();
case "topk":
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfigurationConfigurer.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfigurationConfigurer.java
index 59cdd4ed8789..3f4c6e3464ed 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfigurationConfigurer.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfigurationConfigurer.java
@@ -27,6 +27,8 @@ public class SpringAiVectorStoreConfigurationConfigurer
extends org.apache.camel
case "filterexpression":
case "filterExpression":
target.setFilterExpression(property(camelContext, java.lang.String.class,
value)); return true;
case "operation": target.setOperation(property(camelContext,
org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation.class,
value)); return true;
+ case "searchbyvector":
+ case "searchByVector": target.setSearchByVector(property(camelContext,
boolean.class, value)); return true;
case "similaritythreshold":
case "similarityThreshold":
target.setSimilarityThreshold(property(camelContext, double.class, value));
return true;
case "topk":
@@ -43,6 +45,8 @@ public class SpringAiVectorStoreConfigurationConfigurer
extends org.apache.camel
case "filterexpression":
case "filterExpression": return java.lang.String.class;
case "operation": return
org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation.class;
+ case "searchbyvector":
+ case "searchByVector": return boolean.class;
case "similaritythreshold":
case "similarityThreshold": return double.class;
case "topk":
@@ -60,6 +64,8 @@ public class SpringAiVectorStoreConfigurationConfigurer
extends org.apache.camel
case "filterexpression":
case "filterExpression": return target.getFilterExpression();
case "operation": return target.getOperation();
+ case "searchbyvector":
+ case "searchByVector": return target.isSearchByVector();
case "similaritythreshold":
case "similarityThreshold": return target.getSimilarityThreshold();
case "topk":
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointConfigurer.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointConfigurer.java
index d0e64f31e158..4d8956dfc231 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointConfigurer.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointConfigurer.java
@@ -29,6 +29,8 @@ public class SpringAiVectorStoreEndpointConfigurer extends
PropertyConfigurerSup
case "lazystartproducer":
case "lazyStartProducer":
target.setLazyStartProducer(property(camelContext, boolean.class, value));
return true;
case "operation":
target.getConfiguration().setOperation(property(camelContext,
org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation.class,
value)); return true;
+ case "searchbyvector":
+ case "searchByVector":
target.getConfiguration().setSearchByVector(property(camelContext,
boolean.class, value)); return true;
case "similaritythreshold":
case "similarityThreshold":
target.getConfiguration().setSimilarityThreshold(property(camelContext,
double.class, value)); return true;
case "topk":
@@ -52,6 +54,8 @@ public class SpringAiVectorStoreEndpointConfigurer extends
PropertyConfigurerSup
case "lazystartproducer":
case "lazyStartProducer": return boolean.class;
case "operation": return
org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation.class;
+ case "searchbyvector":
+ case "searchByVector": return boolean.class;
case "similaritythreshold":
case "similarityThreshold": return double.class;
case "topk":
@@ -71,6 +75,8 @@ public class SpringAiVectorStoreEndpointConfigurer extends
PropertyConfigurerSup
case "lazystartproducer":
case "lazyStartProducer": return target.isLazyStartProducer();
case "operation": return target.getConfiguration().getOperation();
+ case "searchbyvector":
+ case "searchByVector": return
target.getConfiguration().isSearchByVector();
case "similaritythreshold":
case "similarityThreshold": return
target.getConfiguration().getSimilarityThreshold();
case "topk":
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointUriFactory.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointUriFactory.java
index 583ceda99799..638ca6ddd9a8 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointUriFactory.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpointUriFactory.java
@@ -24,10 +24,11 @@ public class SpringAiVectorStoreEndpointUriFactory extends
org.apache.camel.supp
private static final Set<String> ENDPOINT_IDENTITY_PROPERTY_NAMES;
private static final Map<String, String> MULTI_VALUE_PREFIXES;
static {
- Set<String> props = new HashSet<>(7);
+ Set<String> props = new HashSet<>(8);
props.add("filterExpression");
props.add("lazyStartProducer");
props.add("operation");
+ props.add("searchByVector");
props.add("similarityThreshold");
props.add("storeId");
props.add("topK");
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/resources/META-INF/org/apache/camel/component/springai/vectorstore/spring-ai-vector-store.json
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/resources/META-INF/org/apache/camel/component/springai/vectorstore/spring-ai-vector-store.json
index 4f2c718e705c..02de5286b478 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/resources/META-INF/org/apache/camel/component/springai/vectorstore/spring-ai-vector-store.json
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/generated/resources/META-INF/org/apache/camel/component/springai/vectorstore/spring-ai-vector-store.json
@@ -27,33 +27,37 @@
"configuration": { "index": 0, "kind": "property", "displayName":
"Configuration", "group": "producer", "label": "", "required": false, "type":
"object", "javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"deprecated": false, "autowired": false, "secret": false, "description": "The
configuration." },
"filterExpression": { "index": 1, "kind": "property", "displayName":
"Filter Expression", "group": "producer", "label": "", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Filter expression for
metadata-based filtering in searches." },
"lazyStartProducer": { "index": 2, "kind": "property", "displayName":
"Lazy Start Producer", "group": "producer", "label": "producer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description":
"Whether the producer should be started lazy (on the first message). By
starting lazy you can use this to allow CamelContext and routes to startup in
situations where a producer may otherwise fail [...]
- "operation": { "index": 3, "kind": "property", "displayName": "Operation",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH" ], "deprecated": false,
"autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationF [...]
- "similarityThreshold": { "index": 4, "kind": "property", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarity [...]
- "topK": { "index": 5, "kind": "property", "displayName": "Top K", "group":
"producer", "label": "", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
- "vectorStore": { "index": 6, "kind": "property", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
- "autowiredEnabled": { "index": 7, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Whether autowiring is enabled. This is used for automatic autowiring options
(the option must be marked as autowired) by looking up in the registry to find
if there is a single instance of matching t [...]
+ "operation": { "index": 3, "kind": "property", "displayName": "Operation",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH", "UPSERT" ], "deprecated":
false, "autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"conf [...]
+ "searchByVector": { "index": 4, "kind": "property", "displayName": "Search
By Vector", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Whether
SIMILARITY_SEARCH of a float[] body, such as the vector [...]
+ "similarityThreshold": { "index": 5, "kind": "property", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarity [...]
+ "topK": { "index": 6, "kind": "property", "displayName": "Top K", "group":
"producer", "label": "", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
+ "vectorStore": { "index": 7, "kind": "property", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
+ "autowiredEnabled": { "index": 8, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Whether autowiring is enabled. This is used for automatic autowiring options
(the option must be marked as autowired) by looking up in the registry to find
if there is a single instance of matching t [...]
},
"headers": {
- "CamelSpringAiVectorStoreOperation": { "index": 0, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH" ], "deprecated": false,
"autowired": false, "secret": false, "description": "The operation to perform
(ADD, DELETE, SIMILARITY_SEARCH)", "constantName":
"org.apache.camel.component.springai.vectorstore.Spring [...]
+ "CamelSpringAiVectorStoreOperation": { "index": 0, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH", "UPSERT" ], "deprecated":
false, "autowired": false, "secret": false, "description": "The operation to
perform (ADD, DELETE, SIMILARITY_SEARCH, UPSERT)", "constantName":
"org.apache.camel.component.springai. [...]
"CamelSpringAiVectorStoreTopK": { "index": 1, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "Integer", "deprecated": false, "autowired": false, "secret":
false, "description": "The maximum number of similar documents to return
(topK)", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#TOP_K"
},
"CamelSpringAiVectorStoreSimilarityThreshold": { "index": 2, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "Double", "deprecated": false, "autowired": false, "secret":
false, "description": "The similarity threshold (0-1)", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#SIMILARITY_THRESHOLD"
},
"CamelSpringAiVectorStoreFilterExpression": { "index": 3, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "String", "deprecated": false, "autowired": false, "secret":
false, "description": "Filter expression for metadata-based filtering",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#FILTER_EXPRESSION"
},
- "CamelSpringAiVectorStoreDocumentIds": { "index": 4, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "java.util.List<String>", "deprecated": false, "autowired": false,
"secret": false, "description": "List of document IDs (output of ADD and
SIMILARITY_SEARCH, and input of DELETE when the message has no body)",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENT_IDS"
},
- "CamelSpringAiVectorStoreSimilarDocuments": { "index": 5, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "java.util.List<org.springframework.ai.document.Document>",
"deprecated": false, "autowired": false, "secret": false, "description": "List
of similar documents found", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#SIMILAR_DOCUMENTS"
},
- "CamelSpringAiVectorStoreDocumentsAdded": { "index": 6, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "Integer", "deprecated": false, "autowired": false, "secret":
false, "description": "Number of documents added", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_ADDED"
},
- "CamelSpringAiVectorStoreDocumentsDeleted": { "index": 7, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "Integer", "deprecated": false, "autowired": false,
"secret": false, "description": "Number of document IDs that DELETE gave to the
vector store, as the vector store does not tell how many of them existed",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_DELETED"
}
+ "CamelSpringAiVectorStoreUpsertDocumentId": { "index": 4, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "String", "deprecated": false, "autowired": false, "secret":
false, "description": "The id of the document that UPSERT writes from a float[]
body: the document with this id is replaced. Without it, the document gets a
random UUID", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#UPSE
[...]
+ "CamelSpringAiVectorStoreUpsertDocumentIds": { "index": 5, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "java.util.List<String>", "deprecated": false, "autowired":
false, "secret": false, "description": "The ids of the documents that UPSERT
writes from a List of float[] body, one per vector: the documents with these
ids are replaced. Without it, each document gets a random UUID",
"constantName": "org.apache.camel.component.spri [...]
+ "CamelSpringAiVectorStoreDocumentIds": { "index": 6, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "java.util.List<String>", "deprecated": false, "autowired": false,
"secret": false, "description": "List of document IDs (output of ADD, UPSERT
and SIMILARITY_SEARCH, and input of DELETE when the message has no body)",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENT_IDS"
},
+ "CamelSpringAiVectorStoreSimilarDocuments": { "index": 7, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "java.util.List<org.springframework.ai.document.Document>",
"deprecated": false, "autowired": false, "secret": false, "description": "List
of similar documents found", "constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#SIMILAR_DOCUMENTS"
},
+ "CamelSpringAiVectorStoreDocumentsAdded": { "index": 8, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "Integer", "deprecated": false, "autowired": false, "secret":
false, "description": "Number of documents added by ADD, or written by UPSERT",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_ADDED"
},
+ "CamelSpringAiVectorStoreDocumentsDeleted": { "index": 9, "kind":
"header", "displayName": "", "group": "producer", "label": "", "required":
false, "javaType": "Integer", "deprecated": false, "autowired": false,
"secret": false, "description": "Number of document IDs that DELETE gave to the
vector store, as the vector store does not tell how many of them existed",
"constantName":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreHeaders#DOCUMENTS_DELETED"
}
},
"bodyTypes": {
- "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "DELETE", "carriers": [ "text", "list", "pojo", "null" ]
}, { "option": "operation", "value": "SIMILARITY_SEARCH", "carriers": [ "text",
"list" ] } ], "description": "ADD takes a Document, a List of Document or of
texts, or text (another body is added as its text); a List with another element
fails. After spring-ai-embeddings it adds the float[] (or List of float[]) body
with the text of the CamelS [...]
- "producerOut": { "carriers": [ "unchanged" ], "byOption": [ { "option":
"operation", "value": "SIMILARITY_SEARCH", "carriers": [ "list" ] } ],
"description": "SIMILARITY_SEARCH gives the List of Document found. ADD and
DELETE keep the body, with the ids and the count in headers" }
+ "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "DELETE", "carriers": [ "text", "list", "pojo", "null" ]
}, { "option": "operation", "value": "SIMILARITY_SEARCH", "carriers": [ "text",
"list" ] }, { "option": "operation", "value": "UPSERT", "carriers": [ "list",
"pojo" ] } ], "description": "ADD takes a Document, a List of Document or of
texts, or text (another body is added as its text); a List with another element
fails. After spring-ai-embedd [...]
+ "producerOut": { "carriers": [ "unchanged" ], "byOption": [ { "option":
"operation", "value": "SIMILARITY_SEARCH", "carriers": [ "list" ] } ],
"description": "SIMILARITY_SEARCH gives the List of Document found. ADD, UPSERT
and DELETE keep the body, with the ids and the count in headers" }
},
"properties": {
"storeId": { "index": 0, "kind": "path", "displayName": "Store Id",
"group": "producer", "label": "", "required": true, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "The id" },
"filterExpression": { "index": 1, "kind": "parameter", "displayName":
"Filter Expression", "group": "producer", "label": "", "required": false,
"type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Filter expression for
metadata-based filtering in searches." },
- "operation": { "index": 2, "kind": "parameter", "displayName":
"Operation", "group": "producer", "label": "", "required": false, "type":
"enum", "javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH" ], "deprecated": false,
"autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configuration [...]
- "similarityThreshold": { "index": 3, "kind": "parameter", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarit [...]
- "topK": { "index": 4, "kind": "parameter", "displayName": "Top K",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
- "vectorStore": { "index": 5, "kind": "parameter", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
- "lazyStartProducer": { "index": 6, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Whether the producer should be started
lazy (on the first message). By starting lazy you can use this to allow
CamelContext and routes to startup in situations where a produc [...]
+ "operation": { "index": 2, "kind": "parameter", "displayName":
"Operation", "group": "producer", "label": "", "required": false, "type":
"enum", "javaType":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation",
"enum": [ "ADD", "DELETE", "SIMILARITY_SEARCH", "UPSERT" ], "deprecated":
false, "autowired": false, "secret": false, "defaultValue": "ADD",
"configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"con [...]
+ "searchByVector": { "index": 3, "kind": "parameter", "displayName":
"Search By Vector", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "Whether
SIMILARITY_SEARCH of a float[] body, such as the vector [...]
+ "similarityThreshold": { "index": 4, "kind": "parameter", "displayName":
"Similarity Threshold", "group": "producer", "label": "", "required": false,
"type": "number", "javaType": "double", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 0.0, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The minimum similarity
score threshold (0-1) for similarit [...]
+ "topK": { "index": 5, "kind": "parameter", "displayName": "Top K",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 5, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The maximum number of
similar documents to return for similarity search." },
+ "vectorStore": { "index": 6, "kind": "parameter", "displayName": "Vector
Store", "group": "producer", "label": "", "required": true, "type": "object",
"javaType": "org.springframework.ai.vectorstore.VectorStore", "deprecated":
false, "autowired": true, "secret": false, "configurationClass":
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreConfiguration",
"configurationField": "configuration", "description": "The VectorStore to use
for vector operations." },
+ "lazyStartProducer": { "index": 7, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Whether the producer should be started
lazy (on the first message). By starting lazy you can use this to allow
CamelContext and routes to startup in situations where a produc [...]
}
}
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/docs/spring-ai-vector-store-component.adoc
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/docs/spring-ai-vector-store-component.adoc
index 484246bb8aee..f5f41170e8b3 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/docs/spring-ai-vector-store-component.adoc
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/docs/spring-ai-vector-store-component.adoc
@@ -21,7 +21,7 @@ Vector stores enable:
* Storage of documents with embeddings
* Similarity search based on vector distance
* Metadata-based filtering
-* Document management (add, delete)
+* Document management (add, upsert, delete)
== URI format
@@ -40,7 +40,7 @@ include::partial$component-endpoint-headers.adoc[]
== Operations
-The component supports three main operations:
+The component supports four operations:
=== ADD Operation
@@ -56,10 +56,39 @@ a `float[]` body with the `CamelSpringAiEmbeddingInputText`
header, or a List of
The `CamelSpringAiEmbeddingInputText(s)` headers are only used together with a
vector body, so a header left over from
an earlier spring-ai-embeddings call does not replace a Document or text body.
+ADD of a vector stores its text, and the vector store embeds the text again
with its own embedding model. The vector
+only goes in the `embedding` metadata of the document. To store the vector
itself, use UPSERT.
+
After documents are added, the component sets the
`CamelSpringAiVectorStoreDocumentIds` header containing a list of the IDs of
the added documents. This allows you to reference or track the stored documents
in subsequent processing steps.
NOTE: *Best Practice:* Use the ADD operation directly with the vector store
component when working with text. The component will automatically handle
embedding generation internally if the vector store is configured with an
embedding model. This is the recommended approach as it simplifies your routes
and reduces the number of steps.
+=== UPSERT Operation
+
+Writes vectors that are already computed, such as the output of the
spring-ai-embeddings component, as they are. The
+embedding model of the vector store is not called. A document with the same ID
is replaced, otherwise a new document is
+added. UPSERT takes:
+
+* A `float[]` body with the `CamelSpringAiEmbeddingInputText` header, the text
of the vector. The
+`CamelSpringAiVectorStoreUpsertDocumentId` header is the ID of the document.
Without it, the document gets a random UUID.
+* A List of `float[]` body with the `CamelSpringAiEmbeddingInputTexts` header,
with one text per vector. The
+`CamelSpringAiVectorStoreUpsertDocumentIds` header is the List of IDs, with
one ID per vector. Without it, each document
+gets a random UUID.
+* A Spring AI `EmbeddedDocument` (a Document with its vector), or a List of
`EmbeddedDocument`. The ID and the metadata
+are the ones of the Document, and the ID headers are not used.
+
+A vector without its text fails, and so does a List of texts or of IDs with
another size than the List of vectors.
+UPSERT does not write the `embedding` and `precomputed_embedding` metadata of
ADD.
+
+Like ADD, UPSERT sets the `CamelSpringAiVectorStoreDocumentsAdded` header to
the number of documents written, and the
+`CamelSpringAiVectorStoreDocumentIds` header to their IDs. UPSERT does not
read the IDs from
+`CamelSpringAiVectorStoreDocumentIds`, as ADD, UPSERT and SIMILARITY_SEARCH
set this header to their own output IDs.
+
+The vector store must support UPSERT. In Spring AI 2.1, the Chroma, Couchbase,
Elasticsearch, MariaDB, MongoDB Atlas,
+Neo4j, OpenSearch, PgVector, Qdrant, Redis and Typesense vector stores do.
With another vector store, such as
+`SimpleVectorStore`, UPSERT fails with an `UnsupportedOperationException` and
nothing is written: use ADD instead.
+Some vector stores only accept IDs in a given format, for example Qdrant needs
UUIDs.
+
=== DELETE Operation
Removes documents from the vector store by their IDs. The body has the
documents to delete: an ID, a Document, or a
@@ -76,11 +105,30 @@ Searches for documents similar to a query text. After the
spring-ai-embeddings c
`float[]` vector of the text, the query is the text in the
`CamelSpringAiEmbeddingInputText` header (the vector store
embeds the query itself).
+With the `searchByVector` option, the `float[]` body is the query: the vector
store searches with this vector, and does
+not embed a text. The `CamelSpringAiEmbeddingInputText` header is then not
needed. A text body is still searched as a
+text. The vector store must support the search with a vector, as for UPSERT,
otherwise the search fails with an
+`UnsupportedOperationException`. The `topK`, `similarityThreshold` and
`filterExpression` options and headers apply as
+for a text.
+
The component returns matching documents in the message body and sets the
following headers:
* `CamelSpringAiVectorStoreSimilarDocuments` - List of Document objects found
* `CamelSpringAiVectorStoreDocumentIds` - List of IDs of the found documents
+=== ADD or UPSERT
+
+* With ADD and a text query, the vector store embeds the texts with its own
embedding model, when it writes and when it
+searches. This is the simplest way.
+* With UPSERT and `searchByVector`, the route computes the vectors, for
example with the spring-ai-embeddings
+component, and the vector store writes and searches with these vectors as they
are. Use it to choose the embedding
+model in the route, or to embed each text only once.
+
+IMPORTANT: Use the same embedding model, with the same settings, to write and
to search. A vector of another model gives
+meaningless results, and no error is raised. This also applies when you mix
the operations: documents written with
+UPSERT and searched with a text use the embedding model of the vector store
for the query, and documents written with
+ADD and searched with `searchByVector` use the model of the route for the
query.
+
== Usage Examples
=== Add Documents from Text (Recommended)
@@ -176,6 +224,81 @@ YAML::
====
NOTE: This approach is only necessary when you need to access or modify the
embeddings between generation and storage. For most use cases, the direct ADD
operation (shown above) is preferred.
+With ADD, the vector store embeds the text again: to store the vectors of the
embeddings component, use UPSERT (shown below).
+
+=== Store and Search with the Vectors of the Embeddings Component
+
+UPSERT stores the vectors that the spring-ai-embeddings component computes,
and `searchByVector` searches with the
+vector of the query. The vector store does not embed any text, so it must
support UPSERT and the search with a vector.
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:store")
+ .to("spring-ai-embeddings:embed?embeddingModel=#embeddingModel")
+
.to("spring-ai-vector-store:myStore?operation=UPSERT&vectorStore=#vectorStore");
+
+from("direct:search")
+ .to("spring-ai-embeddings:embed?embeddingModel=#embeddingModel")
+
.to("spring-ai-vector-store:myStore?operation=SIMILARITY_SEARCH&searchByVector=true&topK=5&vectorStore=#vectorStore");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+ <from uri="direct:store"/>
+ <to uri="spring-ai-embeddings:embed?embeddingModel=#embeddingModel"/>
+ <to
uri="spring-ai-vector-store:myStore?operation=UPSERT&vectorStore=#vectorStore"/>
+</route>
+<route>
+ <from uri="direct:search"/>
+ <to uri="spring-ai-embeddings:embed?embeddingModel=#embeddingModel"/>
+ <to
uri="spring-ai-vector-store:myStore?operation=SIMILARITY_SEARCH&searchByVector=true&topK=5&vectorStore=#vectorStore"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: direct:store
+ steps:
+ - to:
+ uri: spring-ai-embeddings:embed
+ parameters:
+ embeddingModel: "#embeddingModel"
+ - to:
+ uri: spring-ai-vector-store:myStore
+ parameters:
+ operation: UPSERT
+ vectorStore: "#vectorStore"
+- route:
+ from:
+ uri: direct:search
+ steps:
+ - to:
+ uri: spring-ai-embeddings:embed
+ parameters:
+ embeddingModel: "#embeddingModel"
+ - to:
+ uri: spring-ai-vector-store:myStore
+ parameters:
+ operation: SIMILARITY_SEARCH
+ searchByVector: true
+ topK: 5
+ vectorStore: "#vectorStore"
+----
+====
+
+To replace a document, for example when its text changes, set its ID in the
`CamelSpringAiVectorStoreUpsertDocumentId`
+header (or the IDs in `CamelSpringAiVectorStoreUpsertDocumentIds` for a List
of texts) before UPSERT.
=== Similarity Search
@@ -305,4 +428,5 @@ IMPORTANT: Make sure your vector store is configured with
an embedding model to
* `topK` - Maximum number of results (default: 5)
* `similarityThreshold` - Minimum similarity score 0-1 (default: 0.0)
* `filterExpression` - Metadata-based filter expression
+* `searchByVector` - Search with the `float[]` body instead of the text of the
`CamelSpringAiEmbeddingInputText` header (default: false)
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfiguration.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfiguration.java
index 2e85d3573c4e..93d54d6c0f7d 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfiguration.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreConfiguration.java
@@ -43,6 +43,14 @@ public class SpringAiVectorStoreConfiguration implements
Cloneable {
@UriParam
private String filterExpression;
+ @UriParam(defaultValue = "false",
+ description = "Whether SIMILARITY_SEARCH of a float[] body, such
as the vector of spring-ai-embeddings,"
+ + " searches with this vector. By default it
searches for the text of the"
+ + " CamelSpringAiEmbeddingInputText header, which
the vector store embeds again. The vector"
+ + " store must support the search with a vector,
and the vector must come from the same"
+ + " embedding model as the stored vectors.")
+ private boolean searchByVector;
+
public VectorStore getVectorStore() {
return vectorStore;
}
@@ -59,7 +67,7 @@ public class SpringAiVectorStoreConfiguration implements
Cloneable {
}
/**
- * The operation to perform on the vector store (ADD, DELETE,
SIMILARITY_SEARCH).
+ * The operation to perform on the vector store (ADD, DELETE,
SIMILARITY_SEARCH, UPSERT).
*/
public void setOperation(SpringAiVectorStoreOperation operation) {
this.operation = operation;
@@ -98,6 +106,18 @@ public class SpringAiVectorStoreConfiguration implements
Cloneable {
this.filterExpression = filterExpression;
}
+ public boolean isSearchByVector() {
+ return searchByVector;
+ }
+
+ /**
+ * Whether SIMILARITY_SEARCH of a float[] body searches with this vector,
instead of the text of the
+ * CamelSpringAiEmbeddingInputText header.
+ */
+ public void setSearchByVector(boolean searchByVector) {
+ this.searchByVector = searchByVector;
+ }
+
public SpringAiVectorStoreConfiguration copy() {
try {
return (SpringAiVectorStoreConfiguration) super.clone();
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpoint.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpoint.java
index 9ae250ea4e5a..1c81d55715fd 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpoint.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreEndpoint.java
@@ -44,7 +44,9 @@ import org.apache.camel.support.DefaultEndpoint;
Carrier.TEXT,
Carrier.LIST, Carrier.POJO,
Carrier.NULL
}),
@BodyType.When(option = "operation",
value = "SIMILARITY_SEARCH",
- carriers = {
Carrier.TEXT, Carrier.LIST }) },
+ carriers = {
Carrier.TEXT, Carrier.LIST }),
+ @BodyType.When(option = "operation",
value = "UPSERT",
+ carriers = {
Carrier.LIST, Carrier.POJO }) },
description = "ADD takes a Document, a List
of Document or of texts, or text (another"
+ " body is added as its
text); a List with another element fails."
+ " After spring-ai-embeddings
it adds the float[] (or List of float[])"
@@ -54,13 +56,18 @@ import org.apache.camel.support.DefaultEndpoint;
+ " SIMILARITY_SEARCH); the
CamelSpringAiVectorStoreDocumentIds header is"
+ " used only without a body.
SIMILARITY_SEARCH takes the query text, or"
+ " after spring-ai-embeddings
the float[] body with the"
- + "
CamelSpringAiEmbeddingInputText header. Otherwise no body fails. The"
- + "
CamelSpringAiVectorStoreOperation header overrides operation"),
+ + "
CamelSpringAiEmbeddingInputText header, or the float[] alone with"
+ + " searchByVector. UPSERT
writes the float[] (or List of float[]) body of"
+ + " spring-ai-embeddings as it
is, with the text of the"
+ + "
CamelSpringAiEmbeddingInputText(s) header, or takes an"
+ + " EmbeddedDocument or a List
of EmbeddedDocument. Otherwise no body"
+ + " fails. The
CamelSpringAiVectorStoreOperation header overrides"
+ + " operation"),
producerOut = @BodyType(value = Carrier.UNCHANGED,
byOption = @BodyType.When(option =
"operation", value = "SIMILARITY_SEARCH",
carriers =
Carrier.LIST),
- description = "SIMILARITY_SEARCH gives the
List of Document found. ADD and DELETE keep"
- + " the body, with the ids
and the count in headers"))
+ description = "SIMILARITY_SEARCH gives the
List of Document found. ADD, UPSERT and"
+ + " DELETE keep the body,
with the ids and the count in headers"))
public class SpringAiVectorStoreEndpoint extends DefaultEndpoint {
@Metadata(required = true)
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreHeaders.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreHeaders.java
index f29368874733..adad387dd471 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreHeaders.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreHeaders.java
@@ -20,7 +20,7 @@ import org.apache.camel.spi.Metadata;
public class SpringAiVectorStoreHeaders {
// Operation control
- @Metadata(description = "The operation to perform (ADD, DELETE,
SIMILARITY_SEARCH)",
+ @Metadata(description = "The operation to perform (ADD, DELETE,
SIMILARITY_SEARCH, UPSERT)",
javaType =
"org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation")
public static final String OPERATION = "CamelSpringAiVectorStoreOperation";
@@ -34,9 +34,20 @@ public class SpringAiVectorStoreHeaders {
@Metadata(description = "Filter expression for metadata-based filtering",
javaType = "String")
public static final String FILTER_EXPRESSION =
"CamelSpringAiVectorStoreFilterExpression";
+ // Upsert parameters: not the DocumentIds header, which ADD, UPSERT and
SIMILARITY_SEARCH set to their output ids
+ @Metadata(description = "The id of the document that UPSERT writes from a
float[] body: the document with this id is"
+ + " replaced. Without it, the document gets a
random UUID",
+ javaType = "String")
+ public static final String UPSERT_DOCUMENT_ID =
"CamelSpringAiVectorStoreUpsertDocumentId";
+
+ @Metadata(description = "The ids of the documents that UPSERT writes from
a List of float[] body, one per vector: the"
+ + " documents with these ids are replaced. Without
it, each document gets a random UUID",
+ javaType = "java.util.List<String>")
+ public static final String UPSERT_DOCUMENT_IDS =
"CamelSpringAiVectorStoreUpsertDocumentIds";
+
// Delete parameters / Results
- @Metadata(description = "List of document IDs (output of ADD and
SIMILARITY_SEARCH, and input of DELETE when the message has no"
- + " body)",
+ @Metadata(description = "List of document IDs (output of ADD, UPSERT and
SIMILARITY_SEARCH, and input of DELETE when the"
+ + " message has no body)",
javaType = "java.util.List<String>")
public static final String DOCUMENT_IDS =
"CamelSpringAiVectorStoreDocumentIds";
@@ -45,7 +56,7 @@ public class SpringAiVectorStoreHeaders {
javaType =
"java.util.List<org.springframework.ai.document.Document>")
public static final String SIMILAR_DOCUMENTS =
"CamelSpringAiVectorStoreSimilarDocuments";
- @Metadata(description = "Number of documents added", javaType = "Integer")
+ @Metadata(description = "Number of documents added by ADD, or written by
UPSERT", javaType = "Integer")
public static final String DOCUMENTS_ADDED =
"CamelSpringAiVectorStoreDocumentsAdded";
@Metadata(description = "Number of document IDs that DELETE gave to the
vector store, as the vector store does not tell how many"
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreOperation.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreOperation.java
index ff3311371cbb..0f93249b5805 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreOperation.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreOperation.java
@@ -19,5 +19,6 @@ package org.apache.camel.component.springai.vectorstore;
public enum SpringAiVectorStoreOperation {
ADD,
DELETE,
- SIMILARITY_SEARCH
+ SIMILARITY_SEARCH,
+ UPSERT
}
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreProducer.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreProducer.java
index efea5df027e3..52f03a54ffaa 100644
---
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreProducer.java
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/main/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreProducer.java
@@ -25,6 +25,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.support.DefaultProducer;
import org.springframework.ai.document.Document;
+import org.springframework.ai.vectorstore.EmbeddedDocument;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
@@ -64,6 +65,9 @@ public class SpringAiVectorStoreProducer extends
DefaultProducer {
case SIMILARITY_SEARCH:
processSimilaritySearch(exchange, vectorStore);
break;
+ case UPSERT:
+ processUpsert(exchange, vectorStore);
+ break;
default:
throw new IllegalArgumentException("Unknown operation: " +
operation);
}
@@ -138,6 +142,114 @@ public class SpringAiVectorStoreProducer extends
DefaultProducer {
message.setHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
documentIds);
}
+ /**
+ * Writes the vectors of the body as they are, so the embedding model of
the vector store is not called. A document
+ * with the same id is replaced.
+ */
+ private void processUpsert(Exchange exchange, VectorStore vectorStore) {
+ final Message message = exchange.getMessage();
+ List<EmbeddedDocument> entries = embeddedDocuments(message);
+
+ // An empty list writes nothing, as VectorStore.upsert does
+ if (!entries.isEmpty()) {
+ try {
+ vectorStore.upsert(entries);
+ } catch (UnsupportedOperationException e) {
+ throw new UnsupportedOperationException(
+ "The vector store " + vectorStore.getName() + " does
not support UPSERT (" + e.getMessage()
+ + "). Use ADD, where
the vector store embeds the text with its own"
+ + " embedding model",
+ e);
+ }
+ }
+
+ message.setHeader(SpringAiVectorStoreHeaders.DOCUMENTS_ADDED,
entries.size());
+ message.setHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
+ entries.stream().map(entry ->
entry.document().getId()).toList());
+ }
+
+ /**
+ * The documents and their vectors in the body: the float[] or List of
float[] of spring-ai-embeddings, with their
+ * texts in the input text headers, or EmbeddedDocuments.
+ */
+ private static List<EmbeddedDocument> embeddedDocuments(Message message) {
+ Object body = message.getBody();
+ if (body instanceof float[] vector) {
+ String inputText = message.getHeader(INPUT_TEXT, String.class);
+ if (inputText == null) {
+ throw new IllegalArgumentException(
+ "UPSERT of a float[] body needs the " + INPUT_TEXT + "
header with the text of the embedding");
+ }
+ String id =
message.getHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_ID, String.class);
+ return List.of(new EmbeddedDocument(createDocument(id, inputText),
vector));
+ }
+ if (body instanceof EmbeddedDocument entry) {
+ return List.of(entry);
+ }
+ if (body instanceof List<?> list) {
+ if (!list.isEmpty() && list.get(0) instanceof float[]) {
+ return embeddedDocumentsOfVectors(message, list);
+ }
+ List<EmbeddedDocument> entries = new ArrayList<>(list.size());
+ for (int i = 0; i < list.size(); i++) {
+ if (!(list.get(i) instanceof EmbeddedDocument entry)) {
+ throw new IllegalArgumentException(
+ "UPSERT of a List body takes a List of float[] or
of EmbeddedDocument, not an element of type "
+ + typeName(list.get(i))
+ " at index " + i);
+ }
+ entries.add(entry);
+ }
+ return entries;
+ }
+ throw new IllegalArgumentException(
+ "UPSERT takes a float[] or a List of float[] body (the output
of spring-ai-embeddings), an EmbeddedDocument"
+ + " or a List of EmbeddedDocument,
not " + typeName(body));
+ }
+
+ private static List<EmbeddedDocument> embeddedDocumentsOfVectors(Message
message, List<?> vectors) {
+ List<String> inputTexts = message.getHeader(INPUT_TEXTS, List.class);
+ if (inputTexts == null || inputTexts.size() != vectors.size()) {
+ throw new IllegalArgumentException(
+ "UPSERT of a List of float[] body needs the " + INPUT_TEXTS
+ + " header with one text per
embedding, got "
+ + (inputTexts == null ? "no
header" : inputTexts.size() + " texts")
+ + " for " + vectors.size() + "
embeddings");
+ }
+ List<?> ids =
message.getHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_IDS, List.class);
+ if (ids != null && ids.size() != vectors.size()) {
+ throw new IllegalArgumentException(
+ "UPSERT of a List of float[] body needs one id per
embedding in the "
+ +
SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_IDS + " header, got "
+ + ids.size() + " ids for " +
vectors.size() + " embeddings");
+ }
+
+ List<EmbeddedDocument> entries = new ArrayList<>(vectors.size());
+ for (int i = 0; i < vectors.size(); i++) {
+ if (!(vectors.get(i) instanceof float[] vector)) {
+ throw new IllegalArgumentException(
+ "UPSERT of a List of float[] body has an element of
type " + typeName(vectors.get(i))
+ + " at index " + i);
+ }
+ String id = null;
+ if (ids != null) {
+ if (ids.get(i) == null) {
+ throw new IllegalArgumentException(
+ "The " +
SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_IDS + " header has no id at index "
+ i);
+ }
+ id = ids.get(i).toString();
+ }
+ entries.add(new EmbeddedDocument(createDocument(id,
inputTexts.get(i)), vector));
+ }
+ return entries;
+ }
+
+ /**
+ * A document of the text, without metadata. Without an id, the document
gets a random UUID.
+ */
+ private static Document createDocument(String id, String text) {
+ return id == null ? new Document(text) : new Document(id, text,
Map.of());
+ }
+
private static String typeName(Object value) {
return value == null ? "null" : value.getClass().getName();
}
@@ -222,18 +334,26 @@ public class SpringAiVectorStoreProducer extends
DefaultProducer {
private void processSimilaritySearch(Exchange exchange, VectorStore
vectorStore) {
final Message message = exchange.getMessage();
- // Get search parameters. After spring-ai-embeddings the body is the
vector of the text, so search for its text
- String query;
- if (message.getBody() instanceof float[]) {
- query = message.getHeader(INPUT_TEXT, String.class);
- if (query == null) {
- throw new IllegalArgumentException(
- "SIMILARITY_SEARCH of a float[] body needs the " +
INPUT_TEXT + " header with the query text");
+ // Get search parameters. After spring-ai-embeddings the body is the
vector of the text, so search with the
+ // vector itself (searchByVector), or for its text
+ float[] queryVector = null;
+ String query = null;
+ if (message.getBody() instanceof float[] vector) {
+ if (getEndpoint().getConfiguration().isSearchByVector()) {
+ queryVector = vector;
+ } else {
+ query = message.getHeader(INPUT_TEXT, String.class);
+ if (query == null) {
+ throw new IllegalArgumentException(
+ "SIMILARITY_SEARCH of a float[] body needs the " +
INPUT_TEXT
+ + " header with the
query text, or the searchByVector option to"
+ + " search with the
vector");
+ }
}
} else {
query = message.getBody(String.class);
}
- if (query == null || query.isEmpty()) {
+ if (queryVector == null && (query == null || query.isEmpty())) {
throw new IllegalArgumentException("Query text is required for
SIMILARITY_SEARCH operation");
}
@@ -252,11 +372,13 @@ public class SpringAiVectorStoreProducer extends
DefaultProducer {
getEndpoint().getConfiguration().getFilterExpression(),
String.class);
- // Build search request
+ // Build search request. The search with a vector has no query text
SearchRequest.Builder builder = SearchRequest.builder()
- .query(query)
.topK(topK)
.similarityThreshold(similarityThreshold);
+ if (query != null) {
+ builder.query(query);
+ }
if (filterExpression != null && !filterExpression.isEmpty()) {
builder.filterExpression(filterExpression);
@@ -265,7 +387,9 @@ public class SpringAiVectorStoreProducer extends
DefaultProducer {
SearchRequest searchRequest = builder.build();
// Perform similarity search
- List<Document> similarDocuments =
vectorStore.similaritySearch(searchRequest);
+ List<Document> similarDocuments = queryVector != null
+ ? similaritySearchByVector(vectorStore, queryVector,
searchRequest)
+ : vectorStore.similaritySearch(searchRequest);
// Set results in headers
message.setHeader(SpringAiVectorStoreHeaders.SIMILAR_DOCUMENTS,
similarDocuments);
@@ -278,4 +402,18 @@ public class SpringAiVectorStoreProducer extends
DefaultProducer {
message.setBody(similarDocuments);
}
+
+ private static List<Document> similaritySearchByVector(
+ VectorStore vectorStore, float[] queryVector, SearchRequest
searchRequest) {
+ try {
+ return vectorStore.similaritySearch(queryVector, searchRequest);
+ } catch (UnsupportedOperationException e) {
+ throw new UnsupportedOperationException(
+ "The vector store " + vectorStore.getName() + " does not
support SIMILARITY_SEARCH with a vector ("
+ + e.getMessage() + ").
Turn off searchByVector to search for the text"
+ + " of the " + INPUT_TEXT
+ " header, which the vector store embeds"
+ + " with its own embedding
model",
+ e);
+ }
+ }
}
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/test/java/org/apache/camel/component/springai/vectorstore/QdrantVectorStoreUpsertIT.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/test/java/org/apache/camel/component/springai/vectorstore/QdrantVectorStoreUpsertIT.java
new file mode 100644
index 000000000000..4bdec5f67921
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/test/java/org/apache/camel/component/springai/vectorstore/QdrantVectorStoreUpsertIT.java
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springai.vectorstore;
+
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+
+import io.qdrant.client.QdrantClient;
+import io.qdrant.client.QdrantGrpcClient;
+import io.qdrant.client.grpc.Collections;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.infra.qdrant.services.QdrantService;
+import org.apache.camel.test.infra.qdrant.services.QdrantServiceFactory;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.springframework.ai.document.Document;
+import org.springframework.ai.embedding.EmbeddingModel;
+import org.springframework.ai.embedding.EmbeddingRequest;
+import org.springframework.ai.embedding.EmbeddingResponse;
+import org.springframework.ai.vectorstore.EmbeddedDocument;
+import org.springframework.ai.vectorstore.qdrant.QdrantVectorStore;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * UPSERT and SIMILARITY_SEARCH with searchByVector on Qdrant, with the
vectors as they are: the embedding model of the
+ * vector store fails if it is called.
+ */
+public class QdrantVectorStoreUpsertIT extends CamelTestSupport {
+
+ @RegisterExtension
+ static QdrantService QDRANT =
QdrantServiceFactory.createSingletonService();
+
+ private static final float[] CAT = { 1f, 0f, 0f };
+ private static final float[] KITTEN = { 0.9f, 0.1f, 0f };
+ private static final float[] CAR = { 0f, 0f, 1f };
+
+ private QdrantClient qdrantClient;
+ private String collectionName;
+ private QdrantVectorStore vectorStore;
+
+ @Override
+ protected void setupResources() throws Exception {
+ super.setupResources();
+
+ collectionName = "upsert_collection_" + System.nanoTime();
+ qdrantClient = new QdrantClient(
+ QdrantGrpcClient.newBuilder(QDRANT.host(), QDRANT.port(),
false).build());
+ qdrantClient.createCollectionAsync(collectionName,
+
Collections.VectorParams.newBuilder().setDistance(Collections.Distance.Cosine).setSize(CAT.length).build())
+ .get();
+
+ vectorStore = QdrantVectorStore.builder(qdrantClient, new
FailingEmbeddingModel())
+ .collectionName(collectionName)
+ .build();
+ }
+
+ @Override
+ protected void cleanupResources() throws Exception {
+ if (qdrantClient != null) {
+ qdrantClient.close();
+ }
+ super.cleanupResources();
+ }
+
+ @Test
+ public void testUpsertVectorsAndSearchByVector() {
+ String catId = UUID.randomUUID().toString();
+ String kittenId = UUID.randomUUID().toString();
+ String carId = UUID.randomUUID().toString();
+
+ Message upserted = fluentTemplate.to("direct:upsert")
+ .withBody(List.of(CAT, KITTEN, CAR))
+ .withHeader("CamelSpringAiEmbeddingInputTexts", List.of("A
cat", "A kitten", "A car"))
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_IDS,
List.of(catId, kittenId, carId))
+ .request(Message.class);
+
+
assertThat(upserted.getHeader(SpringAiVectorStoreHeaders.DOCUMENTS_ADDED,
Integer.class)).isEqualTo(3);
+ assertThat(upserted.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class))
+ .containsExactly(catId, kittenId, carId);
+
+ Awaitility.await()
+ .atMost(10, TimeUnit.SECONDS)
+ .untilAsserted(() -> {
+ Message found = fluentTemplate.to("direct:searchByVector")
+ .withBody(new float[] { 1f, 0.05f, 0f })
+ .request(Message.class);
+
+ List<Document> documents = found.getBody(List.class);
+
assertThat(documents).extracting(Document::getId).containsExactly(catId,
kittenId);
+
assertThat(documents).extracting(Document::getText).containsExactly("A cat", "A
kitten");
+ // UPSERT does not write the embedding and
precomputed_embedding metadata of ADD
+
assertThat(documents.get(0).getMetadata()).doesNotContainKeys("embedding",
"precomputed_embedding");
+ });
+ }
+
+ @Test
+ public void testUpsertReplacesDocumentWithSameId() throws Exception {
+ String id = UUID.randomUUID().toString();
+
+ fluentTemplate.to("direct:upsert")
+ .withBody(CAT)
+ .withHeader("CamelSpringAiEmbeddingInputText", "Old text")
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_ID, id)
+ .send();
+ fluentTemplate.to("direct:upsert")
+ .withBody(CAR)
+ .withHeader("CamelSpringAiEmbeddingInputText", "New text")
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_ID, id)
+ .send();
+
+ Awaitility.await()
+ .atMost(10, TimeUnit.SECONDS)
+ .untilAsserted(() -> {
+
assertThat(qdrantClient.countAsync(collectionName).get()).isEqualTo(1L);
+
+ Message found = fluentTemplate.to("direct:searchByVector")
+ .withBody(CAR)
+ .request(Message.class);
+ List<Document> documents = found.getBody(List.class);
+
assertThat(documents).extracting(Document::getId).containsExactly(id);
+
assertThat(documents).extracting(Document::getText).containsExactly("New text");
+ });
+ }
+
+ @Test
+ public void testUpsertEmbeddedDocumentsAndSearchByVectorWithFilter() {
+ String camelId = UUID.randomUUID().toString();
+ String otherId = UUID.randomUUID().toString();
+
+ fluentTemplate.to("direct:upsert")
+ .withBody(List.of(
+ new EmbeddedDocument(new Document(camelId, "A cat from
Camel", Map.of("source", "camel")), CAT),
+ new EmbeddedDocument(
+ new Document(otherId, "A kitten from
elsewhere", Map.of("source", "other")),
+ KITTEN)))
+ .send();
+
+ Awaitility.await()
+ .atMost(10, TimeUnit.SECONDS)
+ .untilAsserted(() -> {
+ Message found = fluentTemplate.to("direct:searchByVector")
+ .withBody(KITTEN)
+
.withHeader(SpringAiVectorStoreHeaders.FILTER_EXPRESSION, "source == 'camel'")
+ .request(Message.class);
+
assertThat(found.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS, List.class))
+ .containsExactly(camelId);
+ });
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ context.getRegistry().bind("vectorStore", vectorStore);
+
+ from("direct:upsert")
+
.to("spring-ai-vector-store:test?vectorStore=#vectorStore&operation=UPSERT");
+
+ from("direct:searchByVector")
+
.to("spring-ai-vector-store:test?vectorStore=#vectorStore&operation=SIMILARITY_SEARCH"
+ + "&searchByVector=true&topK=2");
+ }
+ };
+ }
+
+ private static final class FailingEmbeddingModel implements EmbeddingModel
{
+
+ @Override
+ public EmbeddingResponse call(EmbeddingRequest request) {
+ throw new IllegalStateException("The embedding model of the vector
store must not be called");
+ }
+
+ @Override
+ public float[] embed(Document document) {
+ throw new IllegalStateException("The embedding model of the vector
store must not be called");
+ }
+ }
+}
diff --git
a/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/test/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreUpsertTest.java
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/test/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreUpsertTest.java
new file mode 100644
index 000000000000..cf6d15a4e4f6
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-ai/camel-spring-ai-vector-store/src/test/java/org/apache/camel/component/springai/vectorstore/SpringAiVectorStoreUpsertTest.java
@@ -0,0 +1,497 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springai.vectorstore;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Message;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.springframework.ai.document.Document;
+import org.springframework.ai.embedding.Embedding;
+import org.springframework.ai.embedding.EmbeddingModel;
+import org.springframework.ai.embedding.EmbeddingRequest;
+import org.springframework.ai.embedding.EmbeddingResponse;
+import org.springframework.ai.embedding.EmbeddingResponseMetadata;
+import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
+import org.springframework.ai.vectorstore.EmbeddedDocument;
+import org.springframework.ai.vectorstore.SearchRequest;
+import org.springframework.ai.vectorstore.SimpleVectorStore;
+import
org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
+import
org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * UPSERT and SIMILARITY_SEARCH with searchByVector write and search with the
vectors of spring-ai-embeddings, without
+ * the embedding model of the vector store.
+ */
+public class SpringAiVectorStoreUpsertTest extends CamelTestSupport {
+
+ private static final String UPSERT =
"spring-ai-vector-store:test?operation=UPSERT";
+ private static final String SEARCH_BY_VECTOR
+ =
"spring-ai-vector-store:test?operation=SIMILARITY_SEARCH&searchByVector=true";
+
+ private static final float[] CAT = { 1f, 0f, 0f };
+ private static final float[] KITTEN = { 0.9f, 0.1f, 0f };
+ private static final float[] CAR = { 0f, 0f, 1f };
+
+ private InMemoryVectorStore vectorStore;
+ private SimpleVectorStore simpleVectorStore;
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+
+ vectorStore = new InMemoryVectorStore();
+ context.getComponent(SpringAiVectorStore.SCHEME,
SpringAiVectorStoreComponent.class)
+ .getConfiguration().setVectorStore(vectorStore);
+
+ // SimpleVectorStore supports neither UPSERT nor the search with a
vector
+ simpleVectorStore = SimpleVectorStore.builder(new
FixedEmbeddingModel()).build();
+ context.getRegistry().bind("simpleVectorStore", simpleVectorStore);
+
+ return context;
+ }
+
+ @Test
+ public void testUpsertVector() {
+ float[] vector = { 0.1f, 0.2f, 0.3f };
+
+ Message result = fluentTemplate.to(UPSERT)
+ .withBody(vector)
+ .withHeader("CamelSpringAiEmbeddingInputText", "Hello world")
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_ID,
"doc-1")
+ .request(Message.class);
+
+
assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENTS_ADDED,
Integer.class)).isEqualTo(1);
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class)).containsExactly("doc-1");
+ assertThat(result.getBody()).isSameAs(vector);
+
+ EmbeddedDocument stored = vectorStore.entries.get("doc-1");
+ assertThat(stored.embedding()).containsExactly(vector);
+ assertThat(stored.document().getText()).isEqualTo("Hello world");
+ // UPSERT does not write the embedding and precomputed_embedding
metadata of ADD
+ assertThat(stored.document().getMetadata()).isEmpty();
+ }
+
+ @Test
+ public void testUpsertVectorWithoutIdGetsRandomUuid() {
+ Message result = fluentTemplate.to(UPSERT)
+ .withBody(CAT)
+ .withHeader("CamelSpringAiEmbeddingInputText", "A cat")
+ .request(Message.class);
+
+ List<String> ids =
result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS, List.class);
+ assertThat(ids).hasSize(1);
+ assertThat(UUID.fromString(ids.get(0))).isNotNull();
+ assertThat(vectorStore.entries).containsOnlyKeys(ids.get(0));
+ }
+
+ @Test
+ public void testUpsertReplacesDocumentWithSameId() {
+ fluentTemplate.to(UPSERT)
+ .withBody(CAT)
+ .withHeader("CamelSpringAiEmbeddingInputText", "Old text")
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_ID,
"doc-1")
+ .send();
+ fluentTemplate.to(UPSERT)
+ .withBody(CAR)
+ .withHeader("CamelSpringAiEmbeddingInputText", "New text")
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_ID,
"doc-1")
+ .send();
+
+ assertThat(vectorStore.entries).containsOnlyKeys("doc-1");
+
assertThat(vectorStore.entries.get("doc-1").document().getText()).isEqualTo("New
text");
+
assertThat(vectorStore.entries.get("doc-1").embedding()).containsExactly(CAR);
+ }
+
+ @Test
+ public void testUpsertVectors() {
+ Message result = fluentTemplate.to(UPSERT)
+ .withBody(List.of(CAT, CAR))
+ .withHeader("CamelSpringAiEmbeddingInputTexts", List.of("A
cat", "A car"))
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_IDS,
List.of("doc-cat", "doc-car"))
+ .request(Message.class);
+
+
assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENTS_ADDED,
Integer.class)).isEqualTo(2);
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class))
+ .containsExactly("doc-cat", "doc-car");
+
assertThat(vectorStore.entries.get("doc-cat").document().getText()).isEqualTo("A
cat");
+
assertThat(vectorStore.entries.get("doc-cat").embedding()).containsExactly(CAT);
+
assertThat(vectorStore.entries.get("doc-car").document().getText()).isEqualTo("A
car");
+
assertThat(vectorStore.entries.get("doc-car").embedding()).containsExactly(CAR);
+ }
+
+ @Test
+ public void testUpsertVectorsWithoutIdsGetRandomUuids() {
+ Message result = fluentTemplate.to(UPSERT)
+ .withBody(List.of(CAT, CAR))
+ .withHeader("CamelSpringAiEmbeddingInputTexts", List.of("A
cat", "A car"))
+ .request(Message.class);
+
+ List<String> ids =
result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS, List.class);
+ assertThat(ids).hasSize(2).doesNotHaveDuplicates();
+ ids.forEach(id -> assertThat(UUID.fromString(id)).isNotNull());
+
assertThat(vectorStore.entries.keySet()).containsExactlyElementsOf(ids);
+ }
+
+ @Test
+ public void testUpsertDoesNotReadDocumentIdsHeader() {
+ // ADD, UPSERT and SIMILARITY_SEARCH set the DocumentIds header to
their output ids: a later UPSERT must not
+ // replace these documents
+ Message result = fluentTemplate.to(UPSERT)
+ .withBody(List.of(CAT, CAR))
+ .withHeader("CamelSpringAiEmbeddingInputTexts", List.of("A
cat", "A car"))
+ .withHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.of("found-1", "found-2"))
+ .request(Message.class);
+
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class))
+ .doesNotContain("found-1", "found-2");
+ assertThat(vectorStore.entries).doesNotContainKeys("found-1",
"found-2");
+ }
+
+ @Test
+ public void testUpsertVectorsWithMismatchedIdsFails() {
+ assertThatThrownBy(() -> fluentTemplate.to(UPSERT)
+ .withBody(List.of(CAT, CAR))
+ .withHeader("CamelSpringAiEmbeddingInputTexts", List.of("A
cat", "A car"))
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_IDS,
List.of("doc-cat"))
+ .request())
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(IllegalArgumentException.class)
+
.hasMessageContaining(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_IDS)
+ .hasMessageContaining("1 ids for 2 embeddings");
+
+ assertThat(vectorStore.entries).isEmpty();
+ }
+
+ @Test
+ public void testUpsertVectorWithoutInputTextFails() {
+ assertThatThrownBy(() -> fluentTemplate.to(UPSERT)
+ .withBody(CAT)
+ .request())
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("CamelSpringAiEmbeddingInputText");
+
+ assertThat(vectorStore.entries).isEmpty();
+ }
+
+ @Test
+ public void testUpsertVectorsWithMismatchedInputTextsFails() {
+ assertThatThrownBy(() -> fluentTemplate.to(UPSERT)
+ .withBody(List.of(CAT, CAR))
+ .withHeader("CamelSpringAiEmbeddingInputTexts", List.of("Only
one text"))
+ .request())
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("CamelSpringAiEmbeddingInputTexts")
+ .hasMessageContaining("1 texts for 2 embeddings");
+
+ assertThat(vectorStore.entries).isEmpty();
+ }
+
+ @Test
+ public void testUpsertEmbeddedDocument() {
+ Document document = new Document("doc-1", "A cat", Map.of("source",
"test"));
+
+ Message result = fluentTemplate.to(UPSERT)
+ .withBody(new EmbeddedDocument(document, CAT))
+ // the id headers go with a vector body only
+ .withHeader(SpringAiVectorStoreHeaders.UPSERT_DOCUMENT_ID,
"other-id")
+ .request(Message.class);
+
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class)).containsExactly("doc-1");
+ assertThat(vectorStore.entries).containsOnlyKeys("doc-1");
+
assertThat(vectorStore.entries.get("doc-1").document().getMetadata()).containsEntry("source",
"test");
+
assertThat(vectorStore.entries.get("doc-1").embedding()).containsExactly(CAT);
+ }
+
+ @Test
+ public void testUpsertListOfEmbeddedDocuments() {
+ Message result = fluentTemplate.to(UPSERT)
+ .withBody(List.of(
+ new EmbeddedDocument(new Document("doc-cat", "A cat",
Map.of()), CAT),
+ new EmbeddedDocument(new Document("doc-car", "A car",
Map.of()), CAR)))
+ .request(Message.class);
+
+
assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENTS_ADDED,
Integer.class)).isEqualTo(2);
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class))
+ .containsExactly("doc-cat", "doc-car");
+ assertThat(vectorStore.entries).containsOnlyKeys("doc-cat", "doc-car");
+ }
+
+ @Test
+ public void testUpsertEmptyListWritesNothing() {
+ // spring-ai-embeddings of an empty List gives an empty List
+ Message result = fluentTemplate.to(UPSERT +
"&vectorStore=#simpleVectorStore")
+ .withBody(new ArrayList<float[]>())
+ .withHeader("CamelSpringAiEmbeddingInputTexts", List.of())
+ .request(Message.class);
+
+
assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENTS_ADDED,
Integer.class)).isZero();
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class)).isEmpty();
+ }
+
+ @Test
+ public void testUpsertListOfTextsFails() {
+ assertThatThrownBy(() -> fluentTemplate.to(UPSERT)
+ .withBody(List.of("A text"))
+ .request())
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("a List of float[] or of
EmbeddedDocument")
+ .hasMessageContaining("java.lang.String");
+ }
+
+ @Test
+ public void testUpsertTextFails() {
+ assertThatThrownBy(() -> fluentTemplate.to(UPSERT)
+ .withBody("A text")
+ .request())
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("UPSERT takes a float[]")
+ .hasMessageContaining("java.lang.String");
+ }
+
+ @Test
+ public void testUpsertToVectorStoreWithoutUpsertFails() {
+ assertThatThrownBy(() -> fluentTemplate.to(UPSERT +
"&vectorStore=#simpleVectorStore")
+ .withBody(CAT)
+ .withHeader("CamelSpringAiEmbeddingInputText", "A cat")
+ .request())
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining("SimpleVectorStore does not support
UPSERT")
+ .hasMessageContaining("Use ADD")
+ .cause()
+ .isInstanceOf(UnsupportedOperationException.class);
+
+
assertThat(simpleVectorStore.similaritySearch(SearchRequest.builder().query("any").topK(100).build()))
+ .isEmpty();
+ }
+
+ @Test
+ public void testSearchByVector() {
+ upsert("doc-cat", "A cat", CAT);
+ upsert("doc-kitten", "A kitten", KITTEN);
+ upsert("doc-car", "A car", CAR);
+
+ Message result = fluentTemplate.to(SEARCH_BY_VECTOR + "&topK=2")
+ .withBody(new float[] { 1f, 0.05f, 0f })
+ .request(Message.class);
+
+ List<Document> found = result.getBody(List.class);
+
assertThat(found).extracting(Document::getId).containsExactly("doc-cat",
"doc-kitten");
+
assertThat(result.getHeader(SpringAiVectorStoreHeaders.SIMILAR_DOCUMENTS,
List.class)).isEqualTo(found);
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class))
+ .containsExactly("doc-cat", "doc-kitten");
+ }
+
+ @Test
+ public void testSearchByVectorIgnoresInputTextHeader() {
+ upsert("doc-cat", "A cat", CAT);
+ upsert("doc-car", "A car", CAR);
+
+ // the vector store would embed the text with its embedding model,
which fails
+ Message result = fluentTemplate.to(SEARCH_BY_VECTOR + "&topK=1")
+ .withBody(CAR)
+ .withHeader("CamelSpringAiEmbeddingInputText", "A cat")
+ .request(Message.class);
+
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class)).containsExactly("doc-car");
+ }
+
+ @Test
+ public void testSearchByVectorWithSearchParameters() {
+ upsert("doc-cat", "A cat", CAT);
+ upsert("doc-kitten", "A kitten", KITTEN);
+ upsert("doc-car", "A car", CAR);
+
+ Message result = fluentTemplate.to(SEARCH_BY_VECTOR + "&topK=5")
+ .withBody(CAT)
+ .withHeader(SpringAiVectorStoreHeaders.TOP_K, 3)
+ .withHeader(SpringAiVectorStoreHeaders.SIMILARITY_THRESHOLD,
0.5)
+ .withHeader(SpringAiVectorStoreHeaders.FILTER_EXPRESSION,
"source == 'test'")
+ .request(Message.class);
+
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class))
+ .containsExactly("doc-cat", "doc-kitten");
+ assertThat(vectorStore.lastSearchRequest.getTopK()).isEqualTo(3);
+
assertThat(vectorStore.lastSearchRequest.getSimilarityThreshold()).isEqualTo(0.5);
+
assertThat(vectorStore.lastSearchRequest.getFilterExpression()).isNotNull();
+ }
+
+ @Test
+ public void testSearchByVectorOfTextSearchesText() {
+ simpleVectorStore.add(List.of(new Document("doc-1", "A cat",
Map.of())));
+
+ Message result = fluentTemplate.to(SEARCH_BY_VECTOR +
"&vectorStore=#simpleVectorStore")
+ .withBody("cat")
+ .request(Message.class);
+
+ assertThat(result.getHeader(SpringAiVectorStoreHeaders.DOCUMENT_IDS,
List.class)).containsExactly("doc-1");
+ }
+
+ @Test
+ public void testSearchByVectorOnVectorStoreWithoutIt() {
+ assertThatThrownBy(() -> fluentTemplate.to(SEARCH_BY_VECTOR +
"&vectorStore=#simpleVectorStore")
+ .withBody(CAT)
+ .request())
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining("SimpleVectorStore does not support
SIMILARITY_SEARCH with a vector")
+ .hasMessageContaining("searchByVector")
+ .cause()
+ .isInstanceOf(UnsupportedOperationException.class);
+ }
+
+ private void upsert(String id, String text, float[] vector) {
+ vectorStore.upsert(List.of(new EmbeddedDocument(new Document(id, text,
Map.of()), vector)));
+ }
+
+ /**
+ * A vector store with UPSERT and the search with a vector, as PgVector or
Qdrant, which keeps the documents in
+ * memory. Its embedding model fails, as UPSERT and the search with a
vector do not embed a text.
+ */
+ static final class InMemoryVectorStore extends
AbstractObservationVectorStore {
+
+ final Map<String, EmbeddedDocument> entries = new LinkedHashMap<>();
+ SearchRequest lastSearchRequest;
+
+ InMemoryVectorStore() {
+ super(new Builder());
+ }
+
+ @Override
+ protected boolean supportsUpsert() {
+ return true;
+ }
+
+ @Override
+ protected void doUpsert(List<EmbeddedDocument> list) {
+ list.forEach(entry -> entries.put(entry.document().getId(),
entry));
+ }
+
+ @Override
+ protected boolean supportsQueryEmbeddingSearch() {
+ return true;
+ }
+
+ @Override
+ protected List<Document> doSimilaritySearch(float[] queryEmbedding,
SearchRequest request) {
+ lastSearchRequest = request;
+ return entries.values().stream()
+ .map(entry ->
entry.document().mutate().score(cosine(queryEmbedding,
entry.embedding())).build())
+ .filter(document -> document.getScore() >=
request.getSimilarityThreshold())
+
.sorted(Comparator.comparing(Document::getScore).reversed())
+ .limit(request.getTopK())
+ .toList();
+ }
+
+ private static double cosine(float[] a, float[] b) {
+ double dot = 0;
+ double normA = 0;
+ double normB = 0;
+ for (int i = 0; i < a.length; i++) {
+ dot += a[i] * b[i];
+ normA += a[i] * a[i];
+ normB += b[i] * b[i];
+ }
+ return dot / (Math.sqrt(normA) * Math.sqrt(normB));
+ }
+
+ @Override
+ public void doAdd(List<Document> documents) {
+ throw new IllegalStateException("ADD is not expected");
+ }
+
+ @Override
+ public void doDelete(List<String> idList) {
+ idList.forEach(entries::remove);
+ }
+
+ @Override
+ public List<Document> doSimilaritySearch(SearchRequest request) {
+ throw new IllegalStateException("The search with a text is not
expected");
+ }
+
+ @Override
+ public VectorStoreObservationContext.Builder
createObservationContextBuilder(String operationName) {
+ return VectorStoreObservationContext.builder("in-memory",
operationName);
+ }
+
+ static final class Builder extends AbstractVectorStoreBuilder<Builder>
{
+
+ Builder() {
+ super(new FailingEmbeddingModel());
+ }
+
+ @Override
+ public InMemoryVectorStore build() {
+ return new InMemoryVectorStore();
+ }
+ }
+ }
+
+ private static final class FailingEmbeddingModel implements EmbeddingModel
{
+
+ @Override
+ public EmbeddingResponse call(EmbeddingRequest request) {
+ throw new IllegalStateException("The embedding model of the vector
store must not be called");
+ }
+
+ @Override
+ public float[] embed(Document document) {
+ throw new IllegalStateException("The embedding model of the vector
store must not be called");
+ }
+ }
+
+ private static final class FixedEmbeddingModel implements EmbeddingModel {
+
+ @Override
+ public EmbeddingResponse call(EmbeddingRequest request) {
+ List<Embedding> embeddings = new ArrayList<>();
+ for (int i = 0; i < request.getInstructions().size(); i++) {
+ embeddings.add(new Embedding(new float[] { 0.1f, 0.2f, 0.3f },
i));
+ }
+ return new EmbeddingResponse(embeddings, new
EmbeddingResponseMetadata());
+ }
+
+ @Override
+ public float[] embed(Document document) {
+ return new float[] { 0.1f, 0.2f, 0.3f };
+ }
+ }
+}
diff --git
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringAiVectorStoreComponentBuilderFactory.java
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringAiVectorStoreComponentBuilderFactory.java
index f868203a8bfc..b2cbb9ed7b92 100644
---
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringAiVectorStoreComponentBuilderFactory.java
+++
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringAiVectorStoreComponentBuilderFactory.java
@@ -109,7 +109,7 @@ public interface SpringAiVectorStoreComponentBuilderFactory
{
/**
* The operation to perform on the vector store (ADD, DELETE,
- * SIMILARITY_SEARCH).
+ * SIMILARITY_SEARCH, UPSERT).
*
* The option is a:
*
<code>org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation</code>
type.
@@ -125,6 +125,28 @@ public interface
SpringAiVectorStoreComponentBuilderFactory {
return this;
}
+
+ /**
+ * Whether SIMILARITY_SEARCH of a float[] body, such as the vector of
+ * spring-ai-embeddings, searches with this vector. By default it
+ * searches for the text of the CamelSpringAiEmbeddingInputText header,
+ * which the vector store embeds again. The vector store must support
+ * the search with a vector, and the vector must come from the same
+ * embedding model as the stored vectors.
+ *
+ * The option is a: <code>boolean</code> type.
+ *
+ * Default: false
+ * Group: producer
+ *
+ * @param searchByVector the value to set
+ * @return the dsl builder
+ */
+ default SpringAiVectorStoreComponentBuilder searchByVector(boolean
searchByVector) {
+ doSetProperty("searchByVector", searchByVector);
+ return this;
+ }
+
/**
* The minimum similarity score threshold (0-1) for similarity search.
*
@@ -220,6 +242,7 @@ public interface SpringAiVectorStoreComponentBuilderFactory
{
case "filterExpression":
getOrCreateConfiguration((SpringAiVectorStoreComponent)
component).setFilterExpression((java.lang.String) value); return true;
case "lazyStartProducer": ((SpringAiVectorStoreComponent)
component).setLazyStartProducer((boolean) value); return true;
case "operation":
getOrCreateConfiguration((SpringAiVectorStoreComponent)
component).setOperation((org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation)
value); return true;
+ case "searchByVector":
getOrCreateConfiguration((SpringAiVectorStoreComponent)
component).setSearchByVector((boolean) value); return true;
case "similarityThreshold":
getOrCreateConfiguration((SpringAiVectorStoreComponent)
component).setSimilarityThreshold((double) value); return true;
case "topK":
getOrCreateConfiguration((SpringAiVectorStoreComponent)
component).setTopK((int) value); return true;
case "vectorStore":
getOrCreateConfiguration((SpringAiVectorStoreComponent)
component).setVectorStore((org.springframework.ai.vectorstore.VectorStore)
value); return true;
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringAiVectorStoreEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringAiVectorStoreEndpointBuilderFactory.java
index 8cb374da2f5f..b05addcb8f85 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringAiVectorStoreEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringAiVectorStoreEndpointBuilderFactory.java
@@ -60,7 +60,7 @@ public interface SpringAiVectorStoreEndpointBuilderFactory {
}
/**
* The operation to perform on the vector store (ADD, DELETE,
- * SIMILARITY_SEARCH).
+ * SIMILARITY_SEARCH, UPSERT).
*
* The option is a:
*
<code>org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation</code>
type.
@@ -77,7 +77,7 @@ public interface SpringAiVectorStoreEndpointBuilderFactory {
}
/**
* The operation to perform on the vector store (ADD, DELETE,
- * SIMILARITY_SEARCH).
+ * SIMILARITY_SEARCH, UPSERT).
*
* The option will be converted to a
*
<code>org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation</code>
type.
@@ -92,6 +92,46 @@ public interface SpringAiVectorStoreEndpointBuilderFactory {
doSetProperty("operation", operation);
return this;
}
+ /**
+ * Whether SIMILARITY_SEARCH of a float[] body, such as the vector of
+ * spring-ai-embeddings, searches with this vector. By default it
+ * searches for the text of the CamelSpringAiEmbeddingInputText header,
+ * which the vector store embeds again. The vector store must support
+ * the search with a vector, and the vector must come from the same
+ * embedding model as the stored vectors.
+ *
+ * The option is a: <code>boolean</code> type.
+ *
+ * Default: false
+ * Group: producer
+ *
+ * @param searchByVector the value to set
+ * @return the dsl builder
+ */
+ default SpringAiVectorStoreEndpointBuilder searchByVector(boolean
searchByVector) {
+ doSetProperty("searchByVector", searchByVector);
+ return this;
+ }
+ /**
+ * Whether SIMILARITY_SEARCH of a float[] body, such as the vector of
+ * spring-ai-embeddings, searches with this vector. By default it
+ * searches for the text of the CamelSpringAiEmbeddingInputText header,
+ * which the vector store embeds again. The vector store must support
+ * the search with a vector, and the vector must come from the same
+ * embedding model as the stored vectors.
+ *
+ * The option will be converted to a <code>boolean</code> type.
+ *
+ * Default: false
+ * Group: producer
+ *
+ * @param searchByVector the value to set
+ * @return the dsl builder
+ */
+ default SpringAiVectorStoreEndpointBuilder searchByVector(String
searchByVector) {
+ doSetProperty("searchByVector", searchByVector);
+ return this;
+ }
/**
* The minimum similarity score threshold (0-1) for similarity search.
*
@@ -313,7 +353,7 @@ public interface SpringAiVectorStoreEndpointBuilderFactory {
public static final SpringAiVectorStoreHeaderNameBuilder INSTANCE =
new SpringAiVectorStoreHeaderNameBuilder();
/**
- * The operation to perform (ADD, DELETE, SIMILARITY_SEARCH).
+ * The operation to perform (ADD, DELETE, SIMILARITY_SEARCH, UPSERT).
*
* The option is a: {@code
*
org.apache.camel.component.springai.vectorstore.SpringAiVectorStoreOperation}
type.
@@ -364,8 +404,38 @@ public interface SpringAiVectorStoreEndpointBuilderFactory
{
return "CamelSpringAiVectorStoreFilterExpression";
}
/**
- * List of document IDs (output of ADD and SIMILARITY_SEARCH, and input
- * of DELETE when the message has no body).
+ * The id of the document that UPSERT writes from a float[] body: the
+ * document with this id is replaced. Without it, the document gets a
+ * random UUID.
+ *
+ * The option is a: {@code String} type.
+ *
+ * Group: producer
+ *
+ * @return the name of the header {@code
+ * SpringAiVectorStoreUpsertDocumentId}.
+ */
+ public String springAiVectorStoreUpsertDocumentId() {
+ return "CamelSpringAiVectorStoreUpsertDocumentId";
+ }
+ /**
+ * The ids of the documents that UPSERT writes from a List of float[]
+ * body, one per vector: the documents with these ids are replaced.
+ * Without it, each document gets a random UUID.
+ *
+ * The option is a: {@code java.util.List<String>} type.
+ *
+ * Group: producer
+ *
+ * @return the name of the header {@code
+ * SpringAiVectorStoreUpsertDocumentIds}.
+ */
+ public String springAiVectorStoreUpsertDocumentIds() {
+ return "CamelSpringAiVectorStoreUpsertDocumentIds";
+ }
+ /**
+ * List of document IDs (output of ADD, UPSERT and SIMILARITY_SEARCH,
+ * and input of DELETE when the message has no body).
*
* The option is a: {@code java.util.List<String>} type.
*
@@ -392,7 +462,7 @@ public interface SpringAiVectorStoreEndpointBuilderFactory {
return "CamelSpringAiVectorStoreSimilarDocuments";
}
/**
- * Number of documents added.
+ * Number of documents added by ADD, or written by UPSERT.
*
* The option is a: {@code Integer} type.
*