Skip to content

Commit 757ed9e

Browse files
committed
Add documentation for Azure vector store custom field names
Signed-off-by: Ilayaperumal Gopinathan <[email protected]>
1 parent 798ab17 commit 757ed9e

File tree

1 file changed

+144
-2
lines changed
  • spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs

1 file changed

+144
-2
lines changed

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

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ You can use the following properties in your Spring Boot configuration to custom
112112
|`spring.ai.vectorstore.azure.index-name`|spring_ai_azure_vector_store
113113
|`spring.ai.vectorstore.azure.default-top-k`|4
114114
|`spring.ai.vectorstore.azure.default-similarity-threshold`|0.0
115-
|`spring.ai.vectorstore.azure.embedding-property`|embedding
116-
|`spring.ai.vectorstore.azure.index-name`|spring-ai-document-index
115+
|`spring.ai.vectorstore.azure.content-field-name`|content
116+
|`spring.ai.vectorstore.azure.embedding-field-name`|embedding
117+
|`spring.ai.vectorstore.azure.metadata-field-name`|metadata
117118
|===
118119

119120

@@ -233,6 +234,147 @@ is converted into the following Azure OData link:https://learn.microsoft.com/en-
233234
$filter search.in(meta_country, 'UK,NL', ',') and meta_year ge 2020
234235
----
235236

237+
== Custom Field Names
238+
239+
By default, the Azure Vector Store uses the following field names in the Azure AI Search index:
240+
241+
* `content` - for document text
242+
* `embedding` - for vector embeddings
243+
* `metadata` - for document metadata
244+
245+
However, when working with existing Azure AI Search indexes that use different field names, you can configure custom field names to match your index schema. This allows you to integrate Spring AI with pre-existing indexes without needing to modify them.
246+
247+
=== Use Cases
248+
249+
Custom field names are particularly useful when:
250+
251+
* **Integrating with existing indexes**: Your organization already has Azure AI Search indexes with established field naming conventions (e.g., `chunk_text`, `vector`, `meta_data`).
252+
* **Following naming standards**: Your team follows specific naming conventions that differ from the defaults.
253+
* **Migrating from other systems**: You're migrating from another vector database or search system and want to maintain consistent field names.
254+
255+
=== Configuration via Properties
256+
257+
You can configure custom field names using Spring Boot application properties:
258+
259+
[source,properties]
260+
----
261+
spring.ai.vectorstore.azure.url=${AZURE_AI_SEARCH_ENDPOINT}
262+
spring.ai.vectorstore.azure.api-key=${AZURE_AI_SEARCH_API_KEY}
263+
spring.ai.vectorstore.azure.index-name=my-existing-index
264+
spring.ai.vectorstore.azure.initialize-schema=false
265+
266+
# Custom field names to match existing index schema
267+
spring.ai.vectorstore.azure.content-field-name=chunk_text
268+
spring.ai.vectorstore.azure.embedding-field-name=vector
269+
spring.ai.vectorstore.azure.metadata-field-name=meta_data
270+
----
271+
272+
IMPORTANT: When using an existing index with custom field names, set `initialize-schema=false` to prevent Spring AI from trying to create a new index with the default schema.
273+
274+
=== Configuration via Builder API
275+
276+
Alternatively, you can configure custom field names programmatically using the builder API:
277+
278+
[source,java]
279+
----
280+
@Bean
281+
public VectorStore vectorStore(SearchIndexClient searchIndexClient, EmbeddingModel embeddingModel) {
282+
283+
return AzureVectorStore.builder(searchIndexClient, embeddingModel)
284+
.indexName("my-existing-index")
285+
.initializeSchema(false) // Don't create schema - use existing index
286+
// Configure custom field names to match existing index
287+
.contentFieldName("chunk_text")
288+
.embeddingFieldName("vector")
289+
.metadataFieldName("meta_data")
290+
.filterMetadataFields(List.of(
291+
MetadataField.text("category"),
292+
MetadataField.text("source")))
293+
.build();
294+
}
295+
----
296+
297+
=== Complete Example: Working with Existing Index
298+
299+
Here's a complete example showing how to use Spring AI with an existing Azure AI Search index that has custom field names:
300+
301+
[source,java]
302+
----
303+
@Configuration
304+
public class VectorStoreConfig {
305+
306+
@Bean
307+
public SearchIndexClient searchIndexClient() {
308+
return new SearchIndexClientBuilder()
309+
.endpoint(System.getenv("AZURE_AI_SEARCH_ENDPOINT"))
310+
.credential(new AzureKeyCredential(System.getenv("AZURE_AI_SEARCH_API_KEY")))
311+
.buildClient();
312+
}
313+
314+
@Bean
315+
public VectorStore vectorStore(SearchIndexClient searchIndexClient,
316+
EmbeddingModel embeddingModel) {
317+
318+
return AzureVectorStore.builder(searchIndexClient, embeddingModel)
319+
.indexName("production-documents-index")
320+
.initializeSchema(false) // Use existing index
321+
// Map to existing index field names
322+
.contentFieldName("document_text")
323+
.embeddingFieldName("text_vector")
324+
.metadataFieldName("document_metadata")
325+
// Define filterable metadata fields from existing schema
326+
.filterMetadataFields(List.of(
327+
MetadataField.text("department"),
328+
MetadataField.int64("year"),
329+
MetadataField.date("created_date")))
330+
.defaultTopK(10)
331+
.defaultSimilarityThreshold(0.75)
332+
.build();
333+
}
334+
}
335+
----
336+
337+
You can then use the vector store as normal:
338+
339+
[source,java]
340+
----
341+
// Search using the existing index with custom field names
342+
List<Document> results = vectorStore.similaritySearch(
343+
SearchRequest.builder()
344+
.query("artificial intelligence")
345+
.topK(5)
346+
.filterExpression("department == 'Engineering' && year >= 2023")
347+
.build());
348+
349+
// The results contain documents with text from the 'document_text' field
350+
results.forEach(doc -> System.out.println(doc.getText()));
351+
----
352+
353+
=== Creating New Index with Custom Field Names
354+
355+
You can also create a new index with custom field names by setting `initializeSchema=true`:
356+
357+
[source,java]
358+
----
359+
@Bean
360+
public VectorStore vectorStore(SearchIndexClient searchIndexClient,
361+
EmbeddingModel embeddingModel) {
362+
363+
return AzureVectorStore.builder(searchIndexClient, embeddingModel)
364+
.indexName("new-custom-index")
365+
.initializeSchema(true) // Create new index with custom field names
366+
.contentFieldName("text_content")
367+
.embeddingFieldName("content_vector")
368+
.metadataFieldName("doc_metadata")
369+
.filterMetadataFields(List.of(
370+
MetadataField.text("category"),
371+
MetadataField.text("author")))
372+
.build();
373+
}
374+
----
375+
376+
This will create a new Azure AI Search index with your custom field names, allowing you to establish your own naming conventions from the start.
377+
236378
== Accessing the Native Client
237379

238380
The Azure Vector Store implementation provides access to the underlying native Azure Search client (`SearchClient`) through the `getNativeClient()` method:

0 commit comments

Comments
 (0)