Skip to content

Commit 3b79a1a

Browse files
Vrryoutzolov
authored andcommitted
Add documentation for Elasticsearch VectorStore.
1 parent 8fa688f commit 3b79a1a

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
= Elasticsearch
2+
3+
This section walks you through setting up the Elasticsearch `VectorStore` to store document embeddings and perform similarity searches.
4+
5+
link:https://www.elastic.co/elasticsearch[Elasticsearch] is an open source search and analytics engine based on the Apache Lucene library.
6+
7+
== Prerequisites
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`.
35+
36+
== Dependencies
37+
38+
Add the ElasticsearchVectorStore boot starter dependency to your project:
39+
40+
[source,xml]
41+
----
42+
<dependency>
43+
<groupId>org.springframework.ai</groupId>
44+
<artifactId>spring-ai-elasticsearch-store-spring-boot-starter</artifactId>
45+
</dependency>
46+
----
47+
48+
or to your Gradle `build.gradle` build file.
49+
50+
[source,groovy]
51+
----
52+
dependencies {
53+
implementation 'org.springframework.ai:spring-ai-elasticsearch-store-spring-boot-starter'
54+
}
55+
----
56+
57+
The Vector Store, also requires an `EmbeddingClient` instance to calculate embeddings for the documents.
58+
You can pick one of the available xref:api/embeddings.adoc#available-implementations[EmbeddingClient Implementations].
59+
60+
For example to use the xref:api/embeddings/openai-embeddings.adoc[OpenAI EmbeddingClient] add the following dependency to your project:
61+
62+
[source,xml]
63+
----
64+
<dependency>
65+
<groupId>org.springframework.ai</groupId>
66+
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
67+
</dependency>
68+
----
69+
70+
or to your Gradle `build.gradle` build file.
71+
72+
[source,groovy]
73+
----
74+
dependencies {
75+
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
76+
}
77+
----
78+
79+
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+
82+
To connect to Elasticsearch, create a `RestClient` bean and provide the access details for your instance.
83+
84+
[source,java]
85+
----
86+
@Bean
87+
public RestClient restClient() {
88+
return RestClient.builder(HttpHost.create("http://localhost:9200"))
89+
.build();
90+
}
91+
----
92+
93+
Next, you can provide the `ElasticsearchVectorStore` configuration via Spring Boot's `application.yml`:
94+
95+
[source,yaml]
96+
----
97+
spring:
98+
ai:
99+
vectorstore:
100+
elasticsearch:
101+
index-name: spring-ai-index
102+
----
103+
104+
TIP: Check the list of <<elasticsearchvector-properties, configuration parameters>> to learn about the default values and configuration options.
105+
106+
In your main code, create some documents:
107+
108+
[source,java]
109+
----
110+
List<Document> documents = List.of(
111+
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
112+
new Document("The World is Big and Salvation Lurks Around the Corner"),
113+
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
114+
----
115+
116+
Add the documents to Elasticsearch:
117+
118+
[source,java]
119+
----
120+
vectorStore.add(List.of(document));
121+
----
122+
123+
And finally, retrieve documents similar to a query:
124+
125+
[source,java]
126+
----
127+
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
128+
----
129+
130+
If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".
131+
132+
[[elasticsearchvector-properties]]
133+
== ElasticsearchVectorStore Properties
134+
135+
You can use the following properties in your Spring Boot configuration to customize the Elasticsearch Vector Store.
136+
137+
138+
|===
139+
|Property |Default Value
140+
141+
|`spring.ai.vectorstore.elasticsearch.index-name`
142+
|spring-ai-document-index
143+
|===
144+

0 commit comments

Comments
 (0)