|
| 1 | +/*Copyright ©2024 APIJSON(https://github.com/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.milvus; |
| 16 | + |
| 17 | +import apijson.NotNull; |
| 18 | +import apijson.orm.SQLConfig; |
| 19 | +import com.alibaba.fastjson.JSONObject; |
| 20 | +import io.milvus.client.MilvusServiceClient; |
| 21 | +import io.milvus.param.ConnectParam; |
| 22 | +import org.datayoo.moql.RecordSet; |
| 23 | +import org.datayoo.moql.querier.milvus.MilvusQuerier; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import static apijson.orm.AbstractSQLExecutor.KEY_RAW_LIST; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Lemon |
| 35 | + * @see DemoSQLExecutor 重写 execute 方法: |
| 36 | + * \@Override |
| 37 | + * public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception { |
| 38 | + * |
| 39 | + * return MilvusUtil.execute(config, unknownType); |
| 40 | + * } |
| 41 | + */ |
| 42 | +public class MilvusUtil { |
| 43 | + public static final String TAG = "MilvusUtil"; |
| 44 | + |
| 45 | + public static JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception { |
| 46 | + // 构建Milvus客户端 |
| 47 | + MilvusServiceClient milvusClient = new MilvusServiceClient( |
| 48 | + ConnectParam.newBuilder().withUri(config.getDBUri()).build() |
| 49 | + ); |
| 50 | + |
| 51 | + // 使用Milvus客户端创建Milvus查询器 |
| 52 | + MilvusQuerier milvusQuerier = new MilvusQuerier(milvusClient); |
| 53 | + |
| 54 | + /* |
| 55 | + 查询语句含义:从book集合中筛选数据,并返回col1,col2两个列。筛选条件为,当数据的col3列值为4,col4列值为'a','b','c'中的任意一 |
| 56 | + 个,且vec向量字段采用'L2'类型匹配,值为'[[1.0, 2.0, 3.0],[1.1,2.1,3.1]]'。另外,采用强一致性级别在10个单元内进行检索,取第11到第15,5条命中记录。 |
| 57 | + */ |
| 58 | + String sql = config.getSQL(false); // |
| 59 | +// String sql = "select id,userId,momentId,content,date from Comment where vMatch(vec, 'L2', '[[1]]') and consistencyLevel('STRONG') limit 1,1"; |
| 60 | + // 使用查询器执行sql语句,并返回查询结果 |
| 61 | + RecordSet recordSet = milvusQuerier.query(sql); |
| 62 | + |
| 63 | +// int count = recordSet == null ? 0 : recordSet.getRecordsCount(); |
| 64 | + List<Map<String, Object>> list = recordSet == null ? null : recordSet.getRecordsAsMaps(); |
| 65 | +// RecordSetDefinition def = recordSet.getRecordSetDefinition(); |
| 66 | +// List<ColumnDefinition> cols = def.getColumns(); |
| 67 | + |
| 68 | +// List<Object[]> list = count <= 0 ? null : recordSet.getRecords(); |
| 69 | + |
| 70 | + if (list == null || list.isEmpty()) { |
| 71 | + return new JSONObject(true); |
| 72 | + } |
| 73 | + |
| 74 | + List<JSONObject> nl = new ArrayList<>(list.size()); |
| 75 | + for (int i = 0; i < list.size(); i++) { |
| 76 | + Map<String, Object> map = list.get(i); |
| 77 | + |
| 78 | + JSONObject obj = new JSONObject(map == null ? new HashMap<>() : map); |
| 79 | + // obj.put(col.getValue(), os[j]); |
| 80 | +// for (int j = 0; j < os.length; j++) { |
| 81 | +// ColumnDefinition col = cols.get(j); |
| 82 | +// obj.put(col.getValue(), os[j]); |
| 83 | +// } |
| 84 | + nl.add(obj); |
| 85 | + } |
| 86 | + |
| 87 | + JSONObject result = nl.get(0); // JSON.parseObject(list.get(0)); |
| 88 | + if (nl.size() > 1) { |
| 89 | + result.put(KEY_RAW_LIST, nl); |
| 90 | + } |
| 91 | + |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments