Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @author Christian Tzolov
* @author Eddú Meléndez
* @author Soby Chacko
* @author Ilayaperumal Gopinathan
*/
@AutoConfiguration
@ConditionalOnClass({ MilvusVectorStore.class, EmbeddingModel.class })
Expand Down Expand Up @@ -75,6 +76,11 @@ public MilvusVectorStore vectorStore(MilvusServiceClient milvusClient, Embedding
.withMetricType(MetricType.valueOf(properties.getMetricType().name()))
.withIndexParameters(properties.getIndexParameters())
.withEmbeddingDimension(properties.getEmbeddingDimension())
.withIDFieldName(properties.getIdFieldName())
.withAutoId(properties.isAutoId())
.withContentFieldName(properties.getContentFieldName())
.withMetadataFieldName(properties.getMetadataFieldName())
.withEmbeddingFieldName(properties.getEmbeddingFieldName())
.build();

return new MilvusVectorStore(milvusClient, embeddingModel, config, properties.isInitializeSchema(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

/**
* @author Christian Tzolov
* @author Ilayaperumal Gopinathan
*/
@ConfigurationProperties(MilvusVectorStoreProperties.CONFIG_PREFIX)
public class MilvusVectorStoreProperties extends CommonVectorStoreProperties {
Expand Down Expand Up @@ -59,6 +60,31 @@ public class MilvusVectorStoreProperties extends CommonVectorStoreProperties {
*/
private String indexParameters = "{\"nlist\":1024}";

/**
* The ID field name for the collection.
*/
private String idFieldName = MilvusVectorStore.DOC_ID_FIELD_NAME;

/**
* Boolean flag to indicate if the auto-id is used.
*/
private boolean isAutoId = false;

/**
* The content field name for the collection.
*/
private String contentFieldName = MilvusVectorStore.CONTENT_FIELD_NAME;

/**
* The metadata field name for the collection.
*/
private String metadataFieldName = MilvusVectorStore.METADATA_FIELD_NAME;

/**
* The embedding field name for the collection.
*/
private String embeddingFieldName = MilvusVectorStore.EMBEDDING_FIELD_NAME;

public String getDatabaseName() {
return this.databaseName;
}
Expand Down Expand Up @@ -113,6 +139,50 @@ public void setIndexParameters(String indexParameters) {
this.indexParameters = indexParameters;
}

public String getIdFieldName() {
return this.idFieldName;
}

public void setIdFieldName(String idFieldName) {
Assert.notNull(idFieldName, "idFieldName can not be null");
this.idFieldName = idFieldName;
}

public boolean isAutoId() {
return this.isAutoId;
}

public void setAutoId(boolean autoId) {
this.isAutoId = autoId;
}

public String getContentFieldName() {
return this.contentFieldName;
}

public void setContentFieldName(String contentFieldName) {
Assert.notNull(contentFieldName, "contentFieldName can not be null");
this.contentFieldName = contentFieldName;
}

public String getMetadataFieldName() {
return this.metadataFieldName;
}

public void setMetadataFieldName(String metadataFieldName) {
Assert.notNull(metadataFieldName, "metadataFieldName can not be null");
this.metadataFieldName = metadataFieldName;
}

public String getEmbeddingFieldName() {
return this.embeddingFieldName;
}

public void setEmbeddingFieldName(String embeddingFieldName) {
Assert.notNull(embeddingFieldName, "embeddingFieldName can not be null");
this.embeddingFieldName = embeddingFieldName;
}

public enum MilvusMetricType {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @author Eddú Meléndez
* @author Soby Chacko
* @author Thomas Vitale
* @author Ilayaperumal Gopinathan
*/
@Testcontainers
public class MilvusVectorStoreAutoConfigurationIT {
Expand Down Expand Up @@ -109,6 +110,57 @@ public void addAndSearch() {
});
}

@Test
public void searchWithCustomFields() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Author tag needs to be updated for this class.

contextRunner
.withPropertyValues("spring.ai.vectorstore.milvus.metricType=COSINE",
"spring.ai.vectorstore.milvus.indexType=IVF_FLAT",
"spring.ai.vectorstore.milvus.embeddingDimension=384",
"spring.ai.vectorstore.milvus.collectionName=myCustomCollection",
"spring.ai.vectorstore.milvus.idFieldName=identity",
"spring.ai.vectorstore.milvus.contentFieldName=text",
"spring.ai.vectorstore.milvus.embeddingFieldName=vectors",
"spring.ai.vectorstore.milvus.metadataFieldName=meta",
"spring.ai.vectorstore.milvus.initializeSchema=true",
"spring.ai.vectorstore.milvus.client.host=" + milvus.getHost(),
"spring.ai.vectorstore.milvus.client.port=" + milvus.getMappedPort(19530))
.run(context -> {
VectorStore vectorStore = context.getBean(VectorStore.class);
TestObservationRegistry observationRegistry = context.getBean(TestObservationRegistry.class);

vectorStore.add(documents);

assertObservationRegistry(observationRegistry, VectorStoreProvider.MILVUS,
VectorStoreObservationContext.Operation.ADD);
observationRegistry.clear();

List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(1));

assertThat(results).hasSize(1);
Document resultDoc = results.get(0);
assertThat(resultDoc.getId()).isEqualTo(documents.get(0).getId());
assertThat(resultDoc.getContent()).contains(
"Spring AI provides abstractions that serve as the foundation for developing AI applications.");
assertThat(resultDoc.getMetadata()).hasSize(2);
assertThat(resultDoc.getMetadata()).containsKeys("spring", "distance");

assertObservationRegistry(observationRegistry, VectorStoreProvider.MILVUS,
VectorStoreObservationContext.Operation.QUERY);
observationRegistry.clear();

// Remove all documents from the store
vectorStore.delete(documents.stream().map(doc -> doc.getId()).toList());

results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(1));
assertThat(results).hasSize(0);

assertObservationRegistry(observationRegistry, VectorStoreProvider.MILVUS,
VectorStoreObservationContext.Operation.DELETE);
observationRegistry.clear();

});
}

@Configuration(proxyBeanMethods = false)
static class Config {

Expand Down
Loading