Skip to content

Commit 4b532ae

Browse files
committed
Improve Elasticsearch Vector DB documentation
1 parent f90c51a commit 4b532ae

File tree

2 files changed

+69
-58
lines changed

2 files changed

+69
-58
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/elasticsearch.adoc

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,12 @@ dependencies {
3737

3838
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
3939

40-
Please have a look at the list of <<elasticsearchvector-properties,configuration parameters>> for the vector store to learn about the default values and configuration options.
41-
42-
Here is an example of the needed bean:
40+
TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
4341

44-
[source,java]
45-
----
46-
@Bean
47-
public EmbeddingClient embeddingCLient() {
48-
// Can be any other EmbeddingClient implementation
49-
return new OpenAiEmbeddingClient(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
50-
}
51-
----
42+
Please have a look at the list of <<elasticsearchvector-properties,configuration parameters>> for the vector store to learn about the default values and configuration options.
5243

53-
In cases where the Spring Boot auto-configured Elasticsearch `RestClient` bean is not what you want or need, you can still define your own bean.
54-
Please read the link:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low-usage-initialization.html[Elasticsearch Documentation]
55-
for more in-depth information about the configuration of a custom RestClient.
44+
Additionally, you will need a configured `EmbeddingClient` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingClient] section for more information.
5645

57-
[source,java]
58-
----
59-
@Bean
60-
public RestClient restClienbt() {
61-
RestClientBuilder builder = RestClient.builder(new HttpHost("<host>", 9200, "http"));
62-
Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "Basic <encoded username and password>") };
63-
builder.setDefaultHeaders(defaultHeaders);
64-
return builder.build();
65-
}
66-
----
6746

6847
Now you can auto-wire the `ElasticsearchVectorStore` as a vector store in your application.
6948

@@ -128,20 +107,19 @@ The Spring Boot properties starting with `spring.elasticsearch.*` are used to co
128107
|===
129108
|Property | Description | Default Value
130109

131-
| spring.elasticsearch.connection-timeout | Connection timeout used when communicating with Elasticsearch. | 1s
132-
| spring.elasticsearch.password | Password for authentication with Elasticsearch. | -
133-
| spring.elasticsearch.username | Username for authentication with Elasticsearch.| -
134-
| spring.elasticsearch.path-prefix | Prefix added to the path of every request sent to Elasticsearch. | -
135-
| spring.elasticsearch.restclient.sniffer.delay-after-failure | Delay of a sniff execution scheduled after a failure.| 1m
136-
| spring.elasticsearch.restclient.sniffer.interval | Interval between consecutive ordinary sniff executions. | 5m
137-
| spring.elasticsearch.restclient.ssl.bundle | SSL bundle name. | -
138-
| spring.elasticsearch.socket-keep-alive | Whether to enable socket keep alive between client and Elasticsearch. | false
139-
| spring.elasticsearch.socket-timeout | Socket timeout used when communicating with Elasticsearch. | 30s
140-
| spring.elasticsearch.uris | Comma-separated list of the Elasticsearch instances to use. | http://localhost:9200
110+
| `spring.elasticsearch.connection-timeout` | Connection timeout used when communicating with Elasticsearch. | `1s`
111+
| `spring.elasticsearch.password` | Password for authentication with Elasticsearch. | -
112+
| `spring.elasticsearch.username` | Username for authentication with Elasticsearch.| -
113+
| `spring.elasticsearch.uris` | Comma-separated list of the Elasticsearch instances to use. | `http://localhost:9200`
114+
| `spring.elasticsearch.path-prefix` | Prefix added to the path of every request sent to Elasticsearch. | -
115+
| `spring.elasticsearch.restclient.sniffer.delay-after-failure` | Delay of a sniff execution scheduled after a failure.| `1m`
116+
| `spring.elasticsearch.restclient.sniffer.interval` | Interval between consecutive ordinary sniff executions. | `5m`
117+
| `spring.elasticsearch.restclient.ssl.bundle` | SSL bundle name. | -
118+
| `spring.elasticsearch.socket-keep-alive` | Whether to enable socket keep alive between client and Elasticsearch. | `false`
119+
| `spring.elasticsearch.socket-timeout` | Socket timeout used when communicating with Elasticsearch. | `30s`
141120
|===
142121

143-
144-
The properties with `spring.ai.vectorstore.elasticsearch.*` prefix help to configure Elasticsearch vector store.
122+
Properties starting with the `spring.ai.vectorstore.elasticsearch.*` prefix are used to configure `ElasticsearchVectorStore`.
145123

146124
|===
147125
|Property | Description | Default Value
@@ -152,30 +130,9 @@ The properties with `spring.ai.vectorstore.elasticsearch.*` prefix help to confi
152130
|`spring.ai.vectorstore.elasticsearch.similarity` | The similarity function to use. | `cosine`
153131
|===
154132

155-
=== Manual Configuration
156-
157-
Instead of using the Spring Boot auto-configuration, you can manually configure the Elasticsearch vector store. \For this you need to add the `spring-ai-elasticsearch-store` to your project:
158-
159-
[source,xml]
160-
----
161-
<dependency>
162-
<groupId>org.springframework.ai</groupId>
163-
<artifactId>spring-ai-elasticsearch-store</artifactId>
164-
</dependency>
165-
----
166-
167-
or to your Gradle `build.gradle` build file.
168-
169-
[source,groovy]
170-
----
171-
dependencies {
172-
implementation 'org.springframework.ai:spring-ai-elasticsearch-store'
173-
}
174-
----
175-
176133
== Metadata Filtering
177134

178-
You can leverage the generic, portable xref:api/vectordbs.adoc#metadata-filters[metadata filters] with Elasticsearcg as well.
135+
You can leverage the generic, portable xref:api/vectordbs.adoc#metadata-filters[metadata filters] with Elasticsearch as well.
179136

180137
For example, you can use either the text expression language:
181138

@@ -219,3 +176,55 @@ is converted into the proprietary Elasticsearch filter format:
219176
(metadata.author:john OR jill) AND metadata.article_type:blog
220177
----
221178

179+
== Manual Configuration
180+
181+
Instead of using the Spring Boot auto-configuration, you can manually configure the Elasticsearch vector store. \For this you need to add the `spring-ai-elasticsearch-store` to your project:
182+
183+
[source,xml]
184+
----
185+
<dependency>
186+
<groupId>org.springframework.ai</groupId>
187+
<artifactId>spring-ai-elasticsearch-store</artifactId>
188+
</dependency>
189+
----
190+
191+
or to your Gradle `build.gradle` build file.
192+
193+
[source,groovy]
194+
----
195+
dependencies {
196+
implementation 'org.springframework.ai:spring-ai-elasticsearch-store'
197+
}
198+
----
199+
200+
201+
Create an Elasticsearch `RestClient` bean.
202+
Read the link:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low-usage-initialization.html[Elasticsearch Documentation] for more in-depth information about the configuration of a custom RestClient.
203+
204+
[source,java]
205+
----
206+
@Bean
207+
public RestClient restClient() {
208+
RestClientBuilder builder = RestClient.builder(new HttpHost("<host>", 9200, "http"));
209+
Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "Basic <encoded username and password>") };
210+
builder.setDefaultHeaders(defaultHeaders);
211+
return builder.build();
212+
}
213+
----
214+
215+
and then create the `ElasticsearchVectorStore` bean:
216+
217+
[source,java]
218+
----
219+
@Bean
220+
public ElasticsearchVectorStore vectorStore(EmbeddingClient embeddingClient, RestClient restClient) {
221+
return new ElasticsearchVectorStore( restClient, embeddingClient);
222+
}
223+
224+
// This can be any EmbeddingClient implementation.
225+
@Bean
226+
public EmbeddingClient embeddingClient() {
227+
return new OpenAiEmbeddingClient(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
228+
}
229+
----
230+

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/elasticsearch/ElasticsearchVectorStoreAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.ai.autoconfigure.vectorstore.elasticsearch;
1717

1818
import org.elasticsearch.client.RestClient;
19+
1920
import org.springframework.ai.embedding.EmbeddingClient;
2021
import org.springframework.ai.vectorstore.ElasticsearchVectorStore;
2122
import org.springframework.ai.vectorstore.ElasticsearchVectorStoreOptions;
@@ -32,6 +33,7 @@
3233
* @author Wei Jiang
3334
* @since 1.0.0
3435
*/
36+
3537
@AutoConfiguration(after = ElasticsearchRestClientAutoConfiguration.class)
3638
@ConditionalOnClass({ ElasticsearchVectorStore.class, EmbeddingClient.class, RestClient.class })
3739
@EnableConfigurationProperties(ElasticsearchVectorStoreProperties.class)

0 commit comments

Comments
 (0)