You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add builder pattern to TypesenseVectorStore and refactor package name
Introduces a builder pattern for configuring TypesenseVectorStore instances and
moves the implementation to the org.springframework.ai.vectorstore.typesense
package. This change:
- Makes configuration more flexible and type-safe through builder methods
- Improves code organization by moving to a dedicated vector store package
- Deprecates old constructors in favor of the builder pattern
- Adds comprehensive validation of configuration options
- Enhances documentation with clear usage examples
- Adds dedicated builder test class for better test coverage
- Add builder tests
- update reference docs
The builder pattern simplifies TypesenseVectorStore configuration while ensuring
proper validation of all settings. The package move aligns with Spring AI's
architectural patterns and improves maintainability by grouping related classes
together.
review
This section walks you through setting up `TypesenseVectorStore` to store document embeddings and perform similarity searches.
4
4
5
-
link:https://typesense.org[Typesense] Typesense is an open source, typo tolerant search engine that is optimized for instant sub-50ms searches, while providing an intuitive developer experience.
5
+
link:https://typesense.org[Typesense] is an open source typo tolerant search engine that is optimized for instant sub-50ms searches while providing an intuitive developer experience. It provides vector search capabilities that allow you to store and query high-dimensional vectors alongside your regular search data.
2. `EmbeddingModel` instance to compute the document embeddings. Several options are available:
14
-
- If required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] to generate the embeddings stored by the `TypesenseVectorStore`.
9
+
* A running Typesense instance. The following options are available:
* If required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] to generate the embeddings stored by the `TypesenseVectorStore`.
15
13
16
14
== Auto-configuration
17
15
18
-
Spring AI provides Spring Boot auto-configuration for the Typesense Vector Sore.
19
-
To enable it, add the following dependency to your project's Maven `pom.xml` file:
16
+
Spring AI provides Spring Boot auto-configuration for the Typesense Vector Store.
17
+
To enable it add the following dependency to your project's Maven `pom.xml` file:
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
39
37
40
-
TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
41
-
42
-
Additionally, you will need a configured `EmbeddingModel` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information.
43
-
44
-
Here is an example of the needed bean:
45
-
46
-
[source,java]
47
-
----
48
-
@Bean
49
-
public EmbeddingModel embeddingModel() {
50
-
// Can be any other EmbeddingModel implementation.
51
-
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
52
-
}
53
-
----
38
+
Please have a look at the list of xref:#_configuration_properties[configuration parameters] for the vector store to learn about the default values and configuration options.
54
39
55
-
To connect to Typesense you need to provide access details for your instance.
56
-
A simple configuration can either be provided via Spring Boot's _application.yml_,
40
+
TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
57
41
58
-
[source,yaml]
59
-
----
60
-
spring:
61
-
ai:
62
-
vectorstore:
63
-
typesense:
64
-
collectionName: "vector_store"
65
-
embeddingDimension: 1536
66
-
client:
67
-
protocl: http
68
-
host: localhost
69
-
port: 8108
70
-
apiKey: xyz
71
-
----
42
+
The vector store implementation can initialize the requisite schema for you but you must opt-in by setting `...initialize-schema=true` in the `application.properties` file.
72
43
73
-
Please have a look at the list of xref:#_configuration_properties[configuration parameters] for the vector store to learn about the default values and configuration options.
44
+
Additionally you will need a configured `EmbeddingModel` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information.
74
45
75
-
Now you can Auto-wire the Typesense Vector Store in your application and use it
46
+
Now you can auto-wire the `TypesenseVectorStore` as a vector store in your application:
76
47
77
48
[source,java]
78
49
----
79
50
@Autowired VectorStore vectorStore;
80
51
81
52
// ...
82
53
83
-
List<Document> documents = List.of(
54
+
List<Document> documents = List.of(
84
55
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
85
56
new Document("The World is Big and Salvation Lurks Around the Corner"),
86
57
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
@@ -89,156 +60,174 @@ List <Document> documents = List.of(
You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with `TypesenseVectorStore` as well.
FilterExpressionBuilder b = new FilterExpressionBuilder();
113
+
|`spring.ai.vectorstore.typesense.client.port`
114
+
|Port
115
+
|`8108`
134
116
135
-
vectorStore.similaritySearch(
136
-
SearchRequest
137
-
.query("The World")
138
-
.withTopK(TOP_K)
139
-
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
140
-
.withFilterExpression(b.and(
141
-
b.in("country", "UK", "NL"),
142
-
b.gte("year", 2020)).build()));
143
-
----
117
+
|`spring.ai.vectorstore.typesense.client.api-key`
118
+
|API Key
119
+
|`xyz`
120
+
|===
144
121
145
-
The portable filter expressions get automatically converted into link:https://typesense.org/docs/0.24.0/api/search.html#filter-parameters[Typesense Search Filters].
146
-
For example, the following portable filter expression:
122
+
== Manual Configuration
147
123
148
-
[source,sql]
124
+
Instead of using the Spring Boot auto-configuration you can manually configure the Typesense vector store. For this you need to add the `spring-ai-typesense-store` to your project:
Configuration configuration = new Configuration(nodes, Duration.ofSeconds(5), "xyz");
197
-
return new Client(configuration);
176
+
public EmbeddingModel embeddingModel() {
177
+
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
198
178
}
199
179
----
200
180
201
-
[NOTE]
202
-
====
203
-
It is more convenient and preferred to create the `TypesenseVectorStore` as a Bean.
204
-
But if you decide to create it manually, then you must call the `TypesenseVectorStore#afterPropertiesSet()` after setting the properties and before using the client.
205
-
====
181
+
== Metadata Filtering
206
182
183
+
You can leverage the generic portable xref:api/vectordbs.adoc#metadata-filters[metadata filters] with Typesense store as well.
207
184
208
-
Then in your main code, create some documents:
185
+
For example you can use either the text expression language:
209
186
210
187
[source,java]
211
188
----
212
-
List<Document> documents = List.of(
213
-
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)),
214
-
new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()),
215
-
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023)));
189
+
vectorStore.similaritySearch(
190
+
SearchRequest.defaults()
191
+
.withQuery("The World")
192
+
.withTopK(TOP_K)
193
+
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
194
+
.withFilterExpression("country in ['UK', 'NL'] && year >= 2020"));
216
195
----
217
196
218
-
Now add the documents to your vector store:
219
-
197
+
or programmatically using the `Filter.Expression` DSL:
220
198
221
199
[source,java]
222
200
----
223
-
vectorStore.add(documents);
201
+
FilterExpressionBuilder b = new FilterExpressionBuilder();
And finally, retrieve documents similar to a query:
212
+
NOTE: Those (portable) filter expressions get automatically converted into link:https://typesense.org/docs/0.24.0/api/search.html#filter-parameters[Typesense Search Filters].
If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".
221
+
is converted into the proprietary Typesense filter format:
222
+
223
+
[source,text]
224
+
----
225
+
country: ['UK', 'NL'] && year: >=2020
226
+
----
237
227
238
228
[NOTE]
239
229
====
240
230
If you are not retrieving the documents in the expected order or the search results are not as expected, check the embedding model you are using.
241
231
242
232
Embedding models can have a significant impact on the search results (i.e. make sure if your data is in Spanish to use a Spanish or multilingual embedding model).
Copy file name to clipboardExpand all lines: spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/typesense/TypesenseVectorStoreAutoConfiguration.java
0 commit comments