shikeying
2024-04-03 b77abcbc0f17070a2a970e0c4aa5837e90f28e1f
walker-support-milvus/src/main/java/com/walker/support/milvus/engine/DefaultOperateService.java
@@ -147,14 +147,15 @@
            return false;
        }
        if(statusR.getStatus().intValue() == R.Status.Success.getCode()){
            logger.error("insert 返回值:" + statusR.getStatus().intValue());
            logger.debug("insert 返回值:" + statusR.getStatus().intValue());
            return true;
        }
        return false;
    }
    @Override
    public boolean createIndex(String tableName, String fieldName, String indexType, String indexParam){
    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")){
@@ -165,22 +166,43 @@
            INDEX_TYPE = IndexType.IVF_PQ;
        } else if(indexType.equals("HNSW")){
            INDEX_TYPE = IndexType.HNSW;
        } else if(indexType.equals("ANNOY")){
            INDEX_TYPE = IndexType.ANNOY;
        } else if(indexType.equals("FLAT")){
        }
        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)
//                .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);
@@ -232,6 +254,12 @@
        MetricType metricType = null;
        if(query.getMetricType() == null || query.getMetricType().equals("")){
            metricType = MetricType.L2;
        } else if(query.getMetricType().equals(com.walker.support.milvus.MetricType.INDEX_IMAGE)){
            metricType = MetricType.L2;
        } else if(query.getMetricType().equals(com.walker.support.milvus.MetricType.INDEX_NLP)){
            metricType = MetricType.IP;
        } else {
            throw new UnsupportedOperationException("暂未支持的距离类型:" + query.getMetricType());
        }
        SearchParam searchParam = SearchParam.newBuilder()
@@ -265,7 +293,7 @@
            if(outField.equals("id")){
                outData.setKeyList((List<Long>)wrapperSearch.getFieldData("id", 0));
            } else {
                outData.setBusinessIdList((List<Long>)wrapperSearch.getFieldData(outField, 0));
                outData.setBusinessIdList((List<String>)wrapperSearch.getFieldData(outField, 0));
            }
        }
//        System.out.println(wrapperSearch.getFieldData("book_id", 0));