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
@@ -6,42 +6,20 @@ link:https://www.elastic.co/elasticsearch[Elasticsearch] is an open source searc
6
6
7
7
== Prerequisites
8
8
9
-
First you need access to an Elasticsearch instance.
10
-
11
-
On startup, the `ElasticsearchVectorStore` will attempt to create the index mapping.
12
-
13
-
Optionally, you can do this manually like so:
14
-
15
-
[source,text]
16
-
----
17
-
PUT spring-ai-document-index
18
-
{
19
-
"mappings": {
20
-
"properties": {
21
-
"embedding": {
22
-
"type": "dense_vector",
23
-
"dims": 1536,
24
-
"index": true,
25
-
"similarity": cosine
26
-
}
27
-
}
28
-
}
29
-
}
30
-
----
31
-
32
-
TIP: replace the `1536` with the actual embedding dimension if you are using a different dimension.
33
-
34
-
Next if required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingClient] to generate the embeddings stored by the `ElasticsearchVectorStore`.
9
+
* A running Elasticsearch instance. The following options are available:
For example, if you want to store your password as an environment variable but keep the rest in the plain `application.yml` file.
69
+
70
+
NOTE: If you choose to create a shell script for ease in future work, be sure to run it prior to starting your application by "sourcing" the file, i.e. `source <your_script_name>.sh`.
71
+
72
+
Spring Boot's auto-configuration feature for the Elasticsearch RestClient will create a bean instance that will be used by the `ElasticsearchVectorStore`.
59
73
60
-
For example to use the xref:api/embeddings/openai-embeddings.adoc[OpenAI EmbeddingClient] add the following dependency to your project:
74
+
== Auto-configuration
75
+
76
+
Spring AI provides Spring Boot auto-configuration for the Elasticsearch Vector Store.
77
+
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.
80
-
Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
81
97
82
-
To connect to and configure Elasticsearch, you need to provide access details for your instance.
83
-
A simple configuration can either be provided via Spring Boot's `application.yml`
98
+
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.
84
99
85
-
[source,yaml]
100
+
Here is an example of the needed bean:
101
+
102
+
[source,java]
86
103
----
87
-
spring:
88
-
elasticsearch:
89
-
uris: <elasticsearch instance URIs>
90
-
username: <elasticsearch username>
91
-
password: <elasticsearch password>
92
-
ai:
93
-
vectorstore:
94
-
elasticsearch:
95
-
index-name: spring-ai-index
104
+
@Bean
105
+
public EmbeddingClient embeddingCLient() {
106
+
// Can be any other EmbeddingClient implementation
107
+
return new OpenAiEmbeddingClient(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
108
+
}
96
109
----
97
110
98
-
TIP: Check the list of <<elasticsearchvector-properties, configuration parameters>> to learn about the default values and configuration options.
99
-
100
-
In your main code, create some documents:
111
+
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.
112
+
Please read the link:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low-usage-initialization.html[Elasticsearch Documentation]
113
+
for more in-depth information about the configuration of a custom RestClient.
101
114
102
115
[source,java]
103
116
----
104
-
List<Document> documents = List.of(
105
-
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
106
-
new Document("The World is Big and Salvation Lurks Around the Corner"),
107
-
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".
158
+
NOTE: Those (portable) filter expressions get automatically converted into the proprietary Elasticsearch `WHERE` link:https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-syntax-select.html#sql-syntax-where[filter expressions].
159
+
160
+
For example, this portable filter expression:
161
+
162
+
[source,sql]
163
+
----
164
+
author in ['john', 'jill'] && 'article_type' == 'blog'
165
+
----
166
+
167
+
is converted into the proprietary Elasticsearch filter format:
168
+
169
+
[source,text]
170
+
----
171
+
(metadata.author:john OR jill) AND metadata.article_type:blog
172
+
----
125
173
126
174
[[elasticsearchvector-properties]]
127
175
== ElasticsearchVectorStore Properties
128
176
129
-
You can use the following properties in your Spring Boot configuration to customize the Elasticsearch Vector Store.
130
-
177
+
You can use the following properties in your Spring Boot configuration to customize the Elasticsearch vector store.
0 commit comments