|
|
|
@ -1,51 +1,73 @@
|
|
|
|
|
|
|
|
|
|
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.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.*;
|
|
|
|
|
import com.dsideal.Config.PropKit; // 配置文件工具类
|
|
|
|
|
import com.dsideal.Res.Plugin.MilvusPlugin; // Milvus连接池管理类
|
|
|
|
|
import com.jfinal.aop.Before;
|
|
|
|
|
import com.jfinal.core.Controller; // JFinal框架基础控制器
|
|
|
|
|
import com.jfinal.ext.interceptor.GET;
|
|
|
|
|
import com.jfinal.kit.Ret; // JFinal返回结果封装类
|
|
|
|
|
import io.milvus.v2.client.MilvusClientV2; // Milvus V2客户端接口
|
|
|
|
|
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.*; // 集合工具类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Milvus向量数据库操作演示控制器
|
|
|
|
|
* 功能:实现集合加载、向量数据查询等基础操作
|
|
|
|
|
*/
|
|
|
|
|
public class MilvusDemoController extends Controller {
|
|
|
|
|
// 日志记录器
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MilvusDemoController.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 默认请求处理方法
|
|
|
|
|
* 流程:1.获取连接 → 2.加载集合 → 3.执行查询 → 4.返回结果
|
|
|
|
|
*/
|
|
|
|
|
@Before({GET.class})
|
|
|
|
|
public void index() {
|
|
|
|
|
MilvusClientV2 client = null;
|
|
|
|
|
MilvusClientV2 client = null; // Milvus客户端实例
|
|
|
|
|
try {
|
|
|
|
|
// 1. 从连接池获取客户端实例
|
|
|
|
|
client = MilvusPlugin.getInstance().getClient();
|
|
|
|
|
|
|
|
|
|
// 2. 从配置获取集合名称
|
|
|
|
|
String collectionName = PropKit.get("milvus.ms_collection_name");
|
|
|
|
|
|
|
|
|
|
// 加载集合
|
|
|
|
|
// 3. 加载目标集合到内存(查询前必需步骤)
|
|
|
|
|
client.loadCollection(
|
|
|
|
|
LoadCollectionReq.builder()
|
|
|
|
|
.collectionName(collectionName)
|
|
|
|
|
.collectionName(collectionName) // 指定集合名称
|
|
|
|
|
.build()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 构建V2版本查询请求
|
|
|
|
|
// 4. 构建查询请求(V2版本API)
|
|
|
|
|
QueryReq queryReq = QueryReq.builder()
|
|
|
|
|
.collectionName(collectionName)
|
|
|
|
|
.outputFields(Arrays.asList("id", "person_id", "user_input",
|
|
|
|
|
"model_response", "timestamp", "embedding"))
|
|
|
|
|
.filter("id >= 0")
|
|
|
|
|
.limit(1000L)
|
|
|
|
|
.collectionName(collectionName) // 目标集合
|
|
|
|
|
.outputFields(Arrays.asList( // 指定返回字段
|
|
|
|
|
"id",
|
|
|
|
|
"person_id",
|
|
|
|
|
"user_input",
|
|
|
|
|
"model_response",
|
|
|
|
|
"timestamp",
|
|
|
|
|
"embedding"))
|
|
|
|
|
.filter("id >= 0") // 过滤条件(此处查询全部)
|
|
|
|
|
.limit(1000L) // 返回结果数量限制
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 执行查询
|
|
|
|
|
// 5. 执行查询并获取响应
|
|
|
|
|
QueryResp response = client.query(queryReq);
|
|
|
|
|
|
|
|
|
|
// 6. 渲染查询结果为纯文本响应
|
|
|
|
|
renderText(response.toString());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 异常处理:记录日志并返回JSON格式错误信息
|
|
|
|
|
logger.error("Milvus操作异常", e);
|
|
|
|
|
renderJson(Ret.fail("msg", "操作失败: " + e.getMessage()));
|
|
|
|
|
} finally {
|
|
|
|
|
// 7. 确保连接归还到连接池
|
|
|
|
|
if (client != null) {
|
|
|
|
|
MilvusPlugin.getInstance().returnClient(client);
|
|
|
|
|
}
|
|
|
|
|