|
|
|
@ -28,103 +28,106 @@ public class MilvusDemoServiceImpl implements MilvusDemoService { |
|
|
|
//集合名称
|
|
|
|
private static final String COLLECTION_NAME = "titleCollection_"; |
|
|
|
|
|
|
|
private final MilvusClientV2 client ; |
|
|
|
//创建向量数据库连接
|
|
|
|
public MilvusDemoServiceImpl(MilvusClientV2 client ) { |
|
|
|
this.client = client ; |
|
|
|
} |
|
|
|
// private final MilvusClientV2 client ;
|
|
|
|
// //创建向量数据库连接
|
|
|
|
// public MilvusDemoServiceImpl(MilvusClientV2 client ) {
|
|
|
|
// this.client = client ;
|
|
|
|
// }
|
|
|
|
|
|
|
|
/** |
|
|
|
* 创建一个Collection |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void createCollection() { |
|
|
|
CreateCollectionReq.CollectionSchema schema = client.createSchema(); |
|
|
|
|
|
|
|
schema.addField(AddFieldReq.builder() |
|
|
|
.fieldName("id") |
|
|
|
.dataType(DataType.Int64) |
|
|
|
.isPrimaryKey(true) |
|
|
|
.autoID(false) |
|
|
|
.build()); |
|
|
|
|
|
|
|
schema.addField(AddFieldReq.builder() |
|
|
|
.fieldName("point_ids") |
|
|
|
.dataType(DataType.Array) |
|
|
|
.elementType(DataType.Int64) |
|
|
|
.maxCapacity(100) |
|
|
|
.build()); |
|
|
|
|
|
|
|
schema.addField(AddFieldReq.builder() |
|
|
|
.fieldName("title_vector") |
|
|
|
.dataType(DataType.FloatVector) |
|
|
|
.dimension(1024) |
|
|
|
.build()); |
|
|
|
|
|
|
|
IndexParam indexParam = IndexParam.builder() |
|
|
|
.fieldName("title_vector") |
|
|
|
.metricType(IndexParam.MetricType.COSINE) |
|
|
|
.build(); |
|
|
|
|
|
|
|
CreateCollectionReq createCollectionReq = CreateCollectionReq.builder() |
|
|
|
.collectionName(COLLECTION_NAME) |
|
|
|
.collectionSchema(schema) |
|
|
|
.indexParams(Collections.singletonList(indexParam)) |
|
|
|
.build(); |
|
|
|
|
|
|
|
client.createCollection(createCollectionReq); |
|
|
|
// CreateCollectionReq.CollectionSchema schema = client.createSchema();
|
|
|
|
//
|
|
|
|
// schema.addField(AddFieldReq.builder()
|
|
|
|
// .fieldName("id")
|
|
|
|
// .dataType(DataType.Int64)
|
|
|
|
// .isPrimaryKey(true)
|
|
|
|
// .autoID(false)
|
|
|
|
// .build());
|
|
|
|
//
|
|
|
|
// schema.addField(AddFieldReq.builder()
|
|
|
|
// .fieldName("point_ids")
|
|
|
|
// .dataType(DataType.Array)
|
|
|
|
// .elementType(DataType.Int64)
|
|
|
|
// .maxCapacity(100)
|
|
|
|
// .build());
|
|
|
|
//
|
|
|
|
// schema.addField(AddFieldReq.builder()
|
|
|
|
// .fieldName("title_vector")
|
|
|
|
// .dataType(DataType.FloatVector)
|
|
|
|
// .dimension(1024)
|
|
|
|
// .build());
|
|
|
|
//
|
|
|
|
// IndexParam indexParam = IndexParam.builder()
|
|
|
|
// .fieldName("title_vector")
|
|
|
|
// .metricType(IndexParam.MetricType.COSINE)
|
|
|
|
// .build();
|
|
|
|
//
|
|
|
|
// CreateCollectionReq createCollectionReq = CreateCollectionReq.builder()
|
|
|
|
// .collectionName(COLLECTION_NAME)
|
|
|
|
// .collectionSchema(schema)
|
|
|
|
// .indexParams(Collections.singletonList(indexParam))
|
|
|
|
// .build();
|
|
|
|
//
|
|
|
|
// client.createCollection(createCollectionReq);
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 往collection中插入一条数据 |
|
|
|
*/ |
|
|
|
public void insertRecord(TitleVector title) { |
|
|
|
QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.findByValue(title.getQuestionDetailDTO().getType()); |
|
|
|
|
|
|
|
JsonObject vector = new JsonObject(); |
|
|
|
vector.addProperty("id", title.getId()); |
|
|
|
vector.addProperty("point_ids", title.getTaskKpIdsHash()); |
|
|
|
vector.add("title_vector", title.getTitleVectorJson()); |
|
|
|
|
|
|
|
InsertReq insertReq = InsertReq.builder() |
|
|
|
.collectionName(COLLECTION_NAME+questionTypeEnum.getType()) |
|
|
|
.data(Collections.singletonList(vector)) |
|
|
|
.build(); |
|
|
|
|
|
|
|
client.insert(insertReq); |
|
|
|
// QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.findByValue(title.getQuestionDetailDTO().getType());
|
|
|
|
//
|
|
|
|
// JsonObject vector = new JsonObject();
|
|
|
|
// vector.addProperty("id", title.getId());
|
|
|
|
// vector.addProperty("point_ids", title.getTaskKpIdsHash());
|
|
|
|
// vector.add("title_vector", title.getTitleVectorJson());
|
|
|
|
//
|
|
|
|
// InsertReq insertReq = InsertReq.builder()
|
|
|
|
// .collectionName(COLLECTION_NAME+questionTypeEnum.getType())
|
|
|
|
// .data(Collections.singletonList(vector))
|
|
|
|
// .build();
|
|
|
|
//
|
|
|
|
// client.insert(insertReq);
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 通过ID获取记录 |
|
|
|
*/ |
|
|
|
public GetResp getRecord(String id) { |
|
|
|
GetReq getReq = GetReq.builder() |
|
|
|
.collectionName(COLLECTION_NAME) |
|
|
|
.ids(Collections.singletonList(id)) |
|
|
|
.build(); |
|
|
|
GetResp resp = client.get(getReq); |
|
|
|
// GetReq getReq = GetReq.builder()
|
|
|
|
// .collectionName(COLLECTION_NAME)
|
|
|
|
// .ids(Collections.singletonList(id))
|
|
|
|
// .build();
|
|
|
|
// GetResp resp = client.get(getReq);
|
|
|
|
GetResp resp = null; |
|
|
|
return resp; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 查询关联知识点相似度最大的题目 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public List<List<SearchResp.SearchResult>> query(TitleVector title){ |
|
|
|
QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.findByValue(title.getQuestionDetailDTO().getType()); |
|
|
|
|
|
|
|
String expr = String.format("point_ids == '%s'", title.getTaskKpIdsHash()); |
|
|
|
|
|
|
|
SearchResp searchReq = client.search(SearchReq.builder() |
|
|
|
.collectionName(COLLECTION_NAME+questionTypeEnum.getType()) |
|
|
|
.data(Collections.singletonList(new FloatVec(title.getTitleVectorList()))) |
|
|
|
.topK(1) |
|
|
|
.filter(expr) |
|
|
|
.outputFields(Collections.singletonList("*")) |
|
|
|
.metricType(IndexParam.MetricType.COSINE) // 余弦相似度
|
|
|
|
.build()); |
|
|
|
|
|
|
|
return searchReq.getSearchResults(); |
|
|
|
// QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.findByValue(title.getQuestionDetailDTO().getType());
|
|
|
|
//
|
|
|
|
// String expr = String.format("point_ids == '%s'", title.getTaskKpIdsHash());
|
|
|
|
//
|
|
|
|
// SearchResp searchReq = client.search(SearchReq.builder()
|
|
|
|
// .collectionName(COLLECTION_NAME+questionTypeEnum.getType())
|
|
|
|
// .data(Collections.singletonList(new FloatVec(title.getTitleVectorList())))
|
|
|
|
// .topK(1)
|
|
|
|
// .filter(expr)
|
|
|
|
// .outputFields(Collections.singletonList("*"))
|
|
|
|
// .metricType(IndexParam.MetricType.COSINE) // 余弦相似度
|
|
|
|
// .build());
|
|
|
|
//
|
|
|
|
// return searchReq.getSearchResults();
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|