20250411 创建milvus连接配置类、创建milvus相似性索引

This commit is contained in:
liangjinglin 2025-04-11 18:26:40 +08:00
parent eb9c01e74e
commit c6e0cab442
4 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.ai.config;
import io.milvus.client.MilvusClient;
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MilvusConfig {
@Bean
public MilvusClient milvusClient() {
ConnectParam connectParam = ConnectParam.newBuilder()
.withHost("localhost")
.withPort(19530)
.build();
return new MilvusServiceClient(connectParam);
}
}

View File

@ -0,0 +1,20 @@
package com.ai.controller;
import com.ai.service.SimilarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/milvus")
public class MilvusController {
@Autowired
private SimilarService similarService;
@RequestMapping("/create")
public String create() {
similarService.createCollection();
return "milvus collection create";
}
}

View File

@ -0,0 +1,15 @@
package com.ai.model;
import lombok.Data;
@Data
public class Product {
private Long spuId;
private String spuName;
private String description;
private String type;
}

View File

@ -0,0 +1,38 @@
package com.ai.service;
import io.milvus.client.MilvusClient;
import io.milvus.grpc.DataType;
import io.milvus.param.collection.CreateCollectionParam;
import io.milvus.param.collection.FieldType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SimilarService {
@Autowired
private MilvusClient milvusClient;
public void createCollection(){
//定义字段
FieldType spuId = FieldType.newBuilder()
.withName("spu_id")
.withDataType(DataType.Int64)
.withPrimaryKey(true)
.build();
FieldType vectorField = FieldType.newBuilder()
.withName("type_vector")
.withDataType(DataType.FloatVector)
.withDimension(1536)
.build();
CreateCollectionParam createParam = CreateCollectionParam.newBuilder()
.withCollectionName("similar_collection")
.addFieldType(spuId)
.addFieldType(vectorField)
.build();
milvusClient.createCollection(createParam);
}
}