Skip to content

improve schema initialization logic and update deprecated code for Milvus vectorstore #3705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 @@ -38,6 +38,7 @@
import io.milvus.param.R;
import io.milvus.param.R.Status;
import io.milvus.param.RpcStatus;
import io.milvus.param.collection.CollectionSchemaParam;
import io.milvus.param.collection.CreateCollectionParam;
import io.milvus.param.collection.DropCollectionParam;
import io.milvus.param.collection.FieldType;
Expand Down Expand Up @@ -443,6 +444,8 @@ void createCollection() {
if (!isDatabaseCollectionExists()) {
createCollection(this.databaseName, this.collectionName, this.idFieldName, this.isAutoId,
this.contentFieldName, this.metadataFieldName, this.embeddingFieldName);
createIndex(this.databaseName, this.collectionName, this.embeddingFieldName, this.indexType,
this.metricType, this.indexParameters);
}

R<DescribeIndexResponse> indexDescriptionResponse = this.milvusClient
Expand All @@ -452,19 +455,8 @@ void createCollection() {
.build());

if (indexDescriptionResponse.getData() == null) {
R<RpcStatus> indexStatus = this.milvusClient.createIndex(CreateIndexParam.newBuilder()
.withDatabaseName(this.databaseName)
.withCollectionName(this.collectionName)
.withFieldName(this.embeddingFieldName)
.withIndexType(this.indexType)
.withMetricType(this.metricType)
.withExtraParam(this.indexParameters)
.withSyncMode(Boolean.FALSE)
.build());

if (indexStatus.getException() != null) {
throw new RuntimeException("Failed to create Index", indexStatus.getException());
}
createIndex(this.databaseName, this.collectionName, this.embeddingFieldName, this.indexType,
this.metricType, this.indexParameters);
}

R<RpcStatus> loadCollectionStatus = this.milvusClient.loadCollection(LoadCollectionParam.newBuilder()
Expand Down Expand Up @@ -507,10 +499,12 @@ void createCollection(String databaseName, String collectionName, String idField
.withDescription("Spring AI Vector Store")
.withConsistencyLevel(ConsistencyLevelEnum.STRONG)
.withShardsNum(2)
.addFieldType(docIdFieldType)
.addFieldType(contentFieldType)
.addFieldType(metadataFieldType)
.addFieldType(embeddingFieldType)
.withSchema(CollectionSchemaParam.newBuilder()
.addFieldType(docIdFieldType)
.addFieldType(contentFieldType)
.addFieldType(metadataFieldType)
.addFieldType(embeddingFieldType)
.build())
.build();

R<RpcStatus> collectionStatus = this.milvusClient.createCollection(createCollectionReq);
Expand All @@ -520,6 +514,23 @@ void createCollection(String databaseName, String collectionName, String idField

}

void createIndex(String databaseName, String collectionName, String embeddingFieldName, IndexType indexType,
MetricType metricType, String indexParameters) {
R<RpcStatus> indexStatus = this.milvusClient.createIndex(CreateIndexParam.newBuilder()
.withDatabaseName(databaseName)
.withCollectionName(collectionName)
.withFieldName(embeddingFieldName)
.withIndexType(indexType)
.withMetricType(metricType)
.withExtraParam(indexParameters)
.withSyncMode(Boolean.FALSE)
.build());

if (indexStatus.getException() != null) {
throw new RuntimeException("Failed to create Index", indexStatus.getException());
}
}

int embeddingDimensions() {
if (this.embeddingDimension != INVALID_EMBEDDING_DIMENSION) {
return this.embeddingDimension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -26,6 +27,10 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import io.milvus.client.AbstractMilvusGrpcClient;
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
import io.milvus.param.IndexType;
Expand All @@ -34,6 +39,7 @@
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.LoggerFactory;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.milvus.MilvusContainer;
Expand Down Expand Up @@ -323,6 +329,37 @@ public void deleteWithComplexFilterExpression() {
});
}

@Test
void initializeSchema() {
this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.milvus.metricType=COSINE").run(context -> {
VectorStore vectorStore = context.getBean(VectorStore.class);

Logger logger = (Logger) LoggerFactory.getLogger(AbstractMilvusGrpcClient.class);
LogAppender logAppender = new LogAppender();
logger.addAppender(logAppender);
logAppender.start();

resetCollection(vectorStore);

assertThat(logAppender.capturedLogs).isEmpty();
});
}

static class LogAppender extends AppenderBase<ILoggingEvent> {

private final List<String> capturedLogs = new ArrayList<>();

@Override
protected void append(ILoggingEvent eventObject) {
capturedLogs.add(eventObject.getFormattedMessage());
}

public List<String> getCapturedLogs() {
return capturedLogs;
}

}

@Test
void getNativeClientTest() {
this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.milvus.metricType=COSINE").run(context -> {
Expand Down