parent
da4761f412
commit
6415820c1d
@ -1,70 +1,55 @@
|
||||
|
||||
package com.dsideal.Res.Milvus.Controller;
|
||||
|
||||
import com.dsideal.Config.PropKit;
|
||||
import com.dsideal.Res.Plugin.MilvusPlugin;
|
||||
import com.jfinal.core.Controller;
|
||||
import com.jfinal.kit.Ret;
|
||||
import io.milvus.client.MilvusServiceClient;
|
||||
import io.milvus.grpc.QueryResults;
|
||||
import io.milvus.param.R;
|
||||
import io.milvus.param.collection.LoadCollectionParam;
|
||||
import io.milvus.param.dml.QueryParam;
|
||||
import io.milvus.response.QueryResultsWrapper;
|
||||
import io.milvus.v2.client.MilvusClientV2;
|
||||
import io.milvus.v2.service.collection.request.LoadCollectionReq;
|
||||
import io.milvus.v2.service.vector.request.QueryReq;
|
||||
import io.milvus.v2.service.vector.response.QueryResp;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MilvusDemoController extends Controller {
|
||||
private final MilvusPlugin milvusPlugin = MilvusPlugin.getInstance();
|
||||
private static final Logger logger = LoggerFactory.getLogger(MilvusDemoController.class);
|
||||
|
||||
public void index() {
|
||||
MilvusClientV2 client = null;
|
||||
try {
|
||||
MilvusServiceClient client = milvusPlugin.getClient();
|
||||
client = MilvusPlugin.getInstance().getClient();
|
||||
String collectionName = PropKit.get("milvus.ms_collection_name");
|
||||
|
||||
// 加载集合
|
||||
client.loadCollection(
|
||||
LoadCollectionParam.newBuilder()
|
||||
.withCollectionName(collectionName)
|
||||
.build()
|
||||
LoadCollectionReq.builder()
|
||||
.collectionName(collectionName)
|
||||
.build()
|
||||
);
|
||||
|
||||
// 查询所有数据
|
||||
QueryParam queryParam = QueryParam.newBuilder()
|
||||
.withCollectionName(collectionName)
|
||||
.withOutFields(Arrays.asList("id", "person_id", "user_input", "model_response", "timestamp", "embedding"))
|
||||
.withExpr("") // 空表达式查询所有数据
|
||||
.withLimit(1000L) // 设置最大返回记录数
|
||||
.build();
|
||||
|
||||
R<QueryResults> response = client.query(queryParam);
|
||||
if (response.getStatus() != R.Status.Success.getCode()) {
|
||||
throw new RuntimeException("Milvus query failed: " + response.getMessage());
|
||||
}
|
||||
// 构建V2版本查询请求
|
||||
QueryReq queryReq = QueryReq.builder()
|
||||
.collectionName(collectionName)
|
||||
.outputFields(Arrays.asList("id", "person_id", "user_input",
|
||||
"model_response", "timestamp", "embedding"))
|
||||
.filter("id >= 0")
|
||||
.limit(1000L)
|
||||
.build();
|
||||
|
||||
List<Map<String, Object>> results = new ArrayList<>();
|
||||
QueryResultsWrapper wrapper = new QueryResultsWrapper(response.getData());
|
||||
// 执行查询
|
||||
QueryResp response = client.query(queryReq);
|
||||
|
||||
// 处理查询结果
|
||||
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);
|
||||
}
|
||||
|
||||
// 返回查询结果
|
||||
renderJson(Ret.ok("data", results));
|
||||
// 处理结果...
|
||||
} catch (Exception e) {
|
||||
logger.error("Milvus查询失败", e);
|
||||
renderJson(Ret.fail("msg", "查询失败:" + e.getMessage()));
|
||||
logger.error("Milvus操作异常", e);
|
||||
renderJson(Ret.fail("msg", "操作失败: " + e.getMessage()));
|
||||
} finally {
|
||||
if (client != null) {
|
||||
MilvusPlugin.getInstance().returnClient(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue