|
|
|
@ -2,22 +2,18 @@ package com.dsideal.Res.Milvus.Controller;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.Config.PropKit;
|
|
|
|
|
import com.dsideal.Res.Plugin.MilvusPlugin;
|
|
|
|
|
import com.dsideal.Res.ResApplication;
|
|
|
|
|
import com.jfinal.core.Controller;
|
|
|
|
|
import com.jfinal.kit.Ret;
|
|
|
|
|
import io.milvus.client.MilvusServiceClient;
|
|
|
|
|
import io.milvus.common.clientenum.ConsistencyLevelEnum;
|
|
|
|
|
import io.milvus.grpc.SearchResults;
|
|
|
|
|
import io.milvus.param.MetricType;
|
|
|
|
|
import io.milvus.grpc.QueryResults;
|
|
|
|
|
import io.milvus.param.R;
|
|
|
|
|
import io.milvus.param.collection.LoadCollectionParam;
|
|
|
|
|
import io.milvus.param.dml.SearchParam;
|
|
|
|
|
import io.milvus.response.SearchResultsWrapper;
|
|
|
|
|
import io.milvus.param.dml.QueryParam;
|
|
|
|
|
import io.milvus.response.QueryResultsWrapper;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class MilvusDemoController extends Controller {
|
|
|
|
|
private final MilvusPlugin milvusPlugin = MilvusPlugin.getInstance();
|
|
|
|
@ -35,30 +31,37 @@ public class MilvusDemoController extends Controller {
|
|
|
|
|
.build()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 示例:搜索向量
|
|
|
|
|
List<List<Float>> vectors = Arrays.asList(
|
|
|
|
|
Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f) // 示例向量,需要根据实际维度调整
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
SearchParam searchParam = SearchParam.newBuilder()
|
|
|
|
|
// 查询所有数据
|
|
|
|
|
QueryParam queryParam = QueryParam.newBuilder()
|
|
|
|
|
.withCollectionName(collectionName)
|
|
|
|
|
.withConsistencyLevel(ConsistencyLevelEnum.STRONG)
|
|
|
|
|
.withMetricType(MetricType.L2)
|
|
|
|
|
.withOutFields(Arrays.asList("field1", "field2"))
|
|
|
|
|
.withTopK(5)
|
|
|
|
|
.withVectors(vectors)
|
|
|
|
|
.withVectorFieldName("vector_field")
|
|
|
|
|
.withOutFields(Arrays.asList("id", "person_id", "user_input", "model_response", "timestamp", "embedding"))
|
|
|
|
|
.withExpr("") // 空表达式查询所有数据
|
|
|
|
|
.withLimit(1000L) // 设置最大返回记录数
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 修改这部分代码
|
|
|
|
|
R<SearchResults> searchResponse = client.search(searchParam);
|
|
|
|
|
if (searchResponse.getStatus() != R.Status.Success.getCode()) {
|
|
|
|
|
throw new RuntimeException("Milvus search failed: " + searchResponse.getMessage());
|
|
|
|
|
R<QueryResults> response = client.query(queryParam);
|
|
|
|
|
if (response.getStatus() != R.Status.Success.getCode()) {
|
|
|
|
|
throw new RuntimeException("Milvus query failed: " + response.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> results = new ArrayList<>();
|
|
|
|
|
QueryResultsWrapper wrapper = new QueryResultsWrapper(response.getData());
|
|
|
|
|
|
|
|
|
|
// 处理查询结果
|
|
|
|
|
List<QueryResultsWrapper.RowRecord> records = wrapper.getRowRecords();
|
|
|
|
|
for (QueryResultsWrapper.RowRecord record : records) {
|
|
|
|
|
Map<String, Object> row = new HashMap<>();
|
|
|
|
|
row.put("id", record.get("id"));
|
|
|
|
|
row.put("person_id", record.get("person_id"));
|
|
|
|
|
row.put("user_input", record.get("user_input"));
|
|
|
|
|
row.put("model_response", record.get("model_response"));
|
|
|
|
|
row.put("timestamp", record.get("timestamp"));
|
|
|
|
|
row.put("embedding", record.get("embedding"));
|
|
|
|
|
results.add(row);
|
|
|
|
|
}
|
|
|
|
|
SearchResultsWrapper wrapper = new SearchResultsWrapper(searchResponse.getData().getResults());
|
|
|
|
|
|
|
|
|
|
// 返回搜索结果
|
|
|
|
|
renderJson(wrapper.getIDScore(0));
|
|
|
|
|
// 返回查询结果
|
|
|
|
|
renderJson(Ret.ok("data", results));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Milvus查询失败", e);
|
|
|
|
|
renderJson(Ret.fail("msg", "查询失败:" + e.getMessage()));
|
|
|
|
|