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