shikeying
2024-03-26 65c1714039bfe31b748e10ca5fb7c0b78a4829e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.walker.support.milvus.engine;
 
import com.walker.support.milvus.DataSet;
import com.walker.support.milvus.FieldType;
import com.walker.support.milvus.OperateService;
import com.walker.support.milvus.OutData;
import com.walker.support.milvus.Query;
import com.walker.support.milvus.Table;
import com.walker.support.milvus.util.FieldTypeUtils;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.MutationResult;
import io.milvus.grpc.SearchResults;
import io.milvus.param.ConnectParam;
import io.milvus.param.IndexType;
import io.milvus.param.MetricType;
import io.milvus.param.R;
import io.milvus.param.RpcStatus;
import io.milvus.param.collection.CreateCollectionParam;
import io.milvus.param.collection.DropCollectionParam;
import io.milvus.param.collection.LoadCollectionParam;
import io.milvus.param.collection.ReleaseCollectionParam;
import io.milvus.param.dml.InsertParam;
import io.milvus.param.dml.SearchParam;
import io.milvus.param.index.CreateIndexParam;
import io.milvus.param.index.DropIndexParam;
import io.milvus.response.SearchResultsWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
public class DefaultOperateService implements OperateService {
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private MilvusServiceClient client = null;
 
    @Override
    public boolean connect(String ip, int port) {
        if(client != null){
            this.client.close();
            logger.warn("MilvusServiceClient在运行,正在停止,准备创建新对象: " + ip + ", " + port);
        }
        try {
            this.client = new MilvusServiceClient(ConnectParam.newBuilder()
                    .withHost(ip)
                    .withPort(port)
                    .build());
            return true;
        } catch (Exception ex){
            logger.error("创建 MilvusServiceClient 错误:" + ip, ex);
            return false;
        }
    }
 
    @Override
    public void close() {
        if(this.client != null){
            this.client.close();
        }
    }
 
    @Override
    public boolean createTable(Table table) {
        this.checkConnection();
        if(table == null){
            logger.error("table 必须提供");
            return false;
        }
        List<FieldType> fieldList = table.getFieldTypes();
        if(fieldList == null || fieldList.size() == 0){
            logger.error("未找到任何字段信息");
            return false;
        }
        String tableName = table.getCollectionName();
        if(tableName == null || tableName.equals("")){
            logger.error("表名必须提供:tableName");
            return false;
        }
 
        try{
            List<io.milvus.param.collection.FieldType> milvusFieldList = new ArrayList<>();
            for(FieldType ft : fieldList){
                milvusFieldList.add(FieldTypeUtils.toMilvusFieldType(ft, table.getDimension()));
            }
 
            CreateCollectionParam.Builder builder = CreateCollectionParam.newBuilder();
            CreateCollectionParam param = builder.withCollectionName(tableName)
                    .withDescription(table.getDescription())
                    .withShardsNum(table.getShardsNum())
                    .withFieldTypes(milvusFieldList)
                    .build();
            R<RpcStatus> statusR = this.client.createCollection(param);
            return this.checkStatusR(statusR);
        } catch (Exception ex){
            logger.error("创建向量表失败:" + tableName, ex);
          return false;
        }
    }
 
    @Override
    public void dropTable(String tableName){
        this.checkConnection();
        if(tableName == null || tableName.equals("")){
            logger.error("表名必须提供:tableName");
            return;
        }
        this.client.dropCollection(DropCollectionParam.newBuilder()
                .withCollectionName(tableName)
                .build());
    }
 
    @Override
    public boolean insertDataSet(DataSet dataSet){
        this.checkConnection();
        if(dataSet == null){
            return false;
        }
        String tableName = dataSet.getTableName();
        if(tableName == null || tableName.equals("")){
            logger.error("表名必须提供:tableName");
            return false;
        }
        Map<String, List<?>> fields = dataSet.getFields();
        if(fields == null || fields.size() == 0){
            logger.error("数据集合必须提供:fields");
            return false;
        }
 
        List<InsertParam.Field> fieldList = new ArrayList<>();
        for(Map.Entry<String, List<?>> entry : fields.entrySet()){
            fieldList.add(new InsertParam.Field(entry.getKey(), entry.getValue()));
        }
 
        InsertParam.Builder builder = InsertParam.newBuilder();
        builder.withCollectionName(dataSet.getTableName());
        if(dataSet.getPartitionName() != null && !dataSet.getPartitionName().equals("")){
            builder.withPartitionName(dataSet.getPartitionName());
        }
        builder.withFields(fieldList);
 
        InsertParam insertParam = builder.build();
        R<MutationResult> statusR = this.client.insert(insertParam);
        if(statusR == null){
            return false;
        }
        if(statusR.getStatus().intValue() == R.Status.Success.getCode()){
            logger.error("insert 返回值:" + statusR.getStatus().intValue());
            return true;
        }
        return false;
    }
 
    @Override
    public boolean createIndex(String tableName, String fieldName, String indexType, String indexParam
            , com.walker.support.milvus.MetricType myMetricType){
        this.checkConnection();
        IndexType INDEX_TYPE = null;
        if(indexType.equals("IVF_FLAT")){
            INDEX_TYPE = IndexType.IVF_FLAT;
        } else if(indexType.equals("IVF_SQ8")){
            INDEX_TYPE = IndexType.IVF_SQ8;
        } else if(indexType.equals("IVF_PQ")){
            INDEX_TYPE = IndexType.IVF_PQ;
        } else if(indexType.equals("HNSW")){
            INDEX_TYPE = IndexType.HNSW;
        }
        else if(indexType.equals("ANNOY")){
//            INDEX_TYPE = IndexType.ANNOY;
            throw new UnsupportedOperationException("新版本已不支持:ANNOY");
        }
        else if(indexType.equals("FLAT")){
            INDEX_TYPE = IndexType.FLAT;
        } else if(indexType.equals("GPU_IVF_FLAT")){
            INDEX_TYPE = IndexType.GPU_IVF_FLAT;
        } else if(indexType.equals("GPU_IVF_PQ")){
            INDEX_TYPE = IndexType.GPU_IVF_PQ;
        } else if(indexType.equals("SCANN")){
            INDEX_TYPE = IndexType.SCANN;
        } else {
            throw new IllegalArgumentException("暂不支持其他索引类型:" + indexType);
        }
 
        /**
         * ​**欧氏距离 (L2)**​: 主要运用于计算机视觉领域。
         * ​**内积 (IP)**​: 主要运用于自然语言处理(NLP)领域。
         * @date 2024-03-26
         */
        CreateIndexParam.Builder builder = CreateIndexParam.newBuilder();
        builder.withCollectionName(tableName)
                .withFieldName(fieldName)
                .withIndexName(fieldName + "_index")
                .withIndexType(INDEX_TYPE)
//                .withMetricType(MetricType.L2)
                .withExtraParam(indexParam)
                .withSyncMode(false);
        if(myMetricType == com.walker.support.milvus.MetricType.NLP){
            builder.withMetricType(MetricType.IP);
        } else if(myMetricType == com.walker.support.milvus.MetricType.IMAGE){
            builder.withMetricType(MetricType.L2);
        } else {
            throw new UnsupportedOperationException("暂时不支持距离类型:" + myMetricType);
        }
 
        R<RpcStatus> statusR = this.client.createIndex(builder.build());
        return checkStatusR(statusR);
    }
 
    @Override
    public boolean dropIndex(String tableName, String fieldName){
        this.checkConnection();
        R<RpcStatus> statusR = this.client.dropIndex(DropIndexParam.newBuilder()
                .withCollectionName(tableName)
                .withIndexName(fieldName + "_index")
                .build());
        return checkStatusR(statusR);
    }
 
    @Override
    public boolean prepareSearch(String tableName){
        this.checkConnection();
        R<RpcStatus> statusR = this.client.loadCollection(LoadCollectionParam.newBuilder().withCollectionName(tableName).build());
//        if(statusR == null){
//            return false;
//        }
//        if(statusR.getStatus().intValue() == R.Status.Success.getCode()){
//            return true;
//        }
//        return false;
        return checkStatusR(statusR);
    }
 
    @Override
    public OutData searchVector(Query query){
        this.checkConnection();
        List<List<Float>> search_vectors = query.getSearchVectors();
        if(search_vectors == null){
            logger.error("未设置搜索向量条件:search_vectors");
            return null;
        }
        String vectorField = query.getVectorName();
        if(vectorField == null || vectorField.equals("")){
            logger.error("未设置搜索字段名称:vectorField");
            return null;
        }
 
        List<String> outputFieldList = query.getOutFieldList();
        if(outputFieldList == null || outputFieldList.size() == 0){
            logger.error("未设置输出字段名称:OutFieldList");
            return null;
        }
        MetricType metricType = null;
        if(query.getMetricType() == null || query.getMetricType().equals("")){
            metricType = MetricType.L2;
        }
 
        SearchParam searchParam = SearchParam.newBuilder()
                .withCollectionName(query.getTableName())
                .withMetricType(metricType)
                .withOutFields(outputFieldList)
                .withTopK(query.getTopK())
                .withVectors(query.getSearchVectors())
                .withVectorFieldName(query.getVectorName())
                .withParams(query.getSearchParam())
                .build();
 
        R<SearchResults> respSearch = this.client.search(searchParam);
        if(respSearch == null){
            logger.warn("未搜索到相似结果对象:" + query);
            return null;
        }
        SearchResultsWrapper wrapperSearch = new SearchResultsWrapper(respSearch.getData().getResults());
        System.out.println(wrapperSearch.getIDScore(0));
 
        // 设置一个分值,评分过低的结果直接过滤。2022-08-26
        OutData outData = new OutData();
 
        List<SearchResultsWrapper.IDScore> scoreList = wrapperSearch.getIDScore(0);
        if(scoreList != null && scoreList.size() > 0){
            for(SearchResultsWrapper.IDScore idScore : scoreList){
                outData.addIdScore(idScore.getLongID(), idScore.getScore());
            }
        }
        for(String outField : outputFieldList){
            if(outField.equals("id")){
                outData.setKeyList((List<Long>)wrapperSearch.getFieldData("id", 0));
            } else {
                outData.setBusinessIdList((List<Long>)wrapperSearch.getFieldData(outField, 0));
            }
        }
//        System.out.println(wrapperSearch.getFieldData("book_id", 0));
//        return wrapperSearch.getFieldData(query.getFieldPrimaryKey(), 0);
        return outData;
    }
 
    @Override
    public void releaseSearch(String tableName){
        this.checkConnection();
        this.client.releaseCollection(ReleaseCollectionParam.newBuilder()
                .withCollectionName(tableName)
                .build());
    }
 
    private void checkConnection(){
        if(this.client == null){
            throw new RuntimeException("服务未连接,请先连接 milvus 服务");
        }
    }
 
    private boolean checkStatusR(R<RpcStatus> statusR){
        if(statusR == null){
            return false;
        }
        if(statusR.getStatus().intValue() == R.Status.Success.getCode()){
            return true;
        }
        return false;
    }
}