瀏覽代碼

Fix example (#329)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
groot 3 年之前
父節點
當前提交
5e1c49c971
共有 1 個文件被更改,包括 47 次插入65 次删除
  1. 47 65
      examples/main/io/milvus/GeneralExample.java

+ 47 - 65
examples/main/io/milvus/GeneralExample.java

@@ -47,7 +47,6 @@ public class GeneralExample {
                 .withHost("localhost")
                 .withHost("localhost")
                 .withPort(19530)
                 .withPort(19530)
                 .withAuthorization("root","Milvus")
                 .withAuthorization("root","Milvus")
-                .withSecure(true)
                 .build();
                 .build();
         milvusClient = new MilvusServiceClient(connectParam);
         milvusClient = new MilvusServiceClient(connectParam);
     }
     }
@@ -60,6 +59,7 @@ public class GeneralExample {
 //    private static final String PROFILE_FIELD = "userProfile";
 //    private static final String PROFILE_FIELD = "userProfile";
 //    private static final Integer BINARY_DIM = 128;
 //    private static final Integer BINARY_DIM = 128;
 
 
+    private static final String INDEX_NAME = "userFaceIndex";
     private static final IndexType INDEX_TYPE = IndexType.IVF_FLAT;
     private static final IndexType INDEX_TYPE = IndexType.IVF_FLAT;
     private static final String INDEX_PARAM = "{\"nlist\":128}";
     private static final String INDEX_PARAM = "{\"nlist\":128}";
 
 
@@ -127,14 +127,14 @@ public class GeneralExample {
         return response;
         return response;
     }
     }
 
 
-    private R<Boolean> hasCollection() {
+    private boolean hasCollection() {
         System.out.println("========== hasCollection() ==========");
         System.out.println("========== hasCollection() ==========");
         R<Boolean> response = milvusClient.hasCollection(HasCollectionParam.newBuilder()
         R<Boolean> response = milvusClient.hasCollection(HasCollectionParam.newBuilder()
                 .withCollectionName(COLLECTION_NAME)
                 .withCollectionName(COLLECTION_NAME)
                 .build());
                 .build());
         handleResponseStatus(response);
         handleResponseStatus(response);
         System.out.println(response);
         System.out.println(response);
-        return response;
+        return response.getData().booleanValue();
     }
     }
 
 
     private R<RpcStatus> loadCollection() {
     private R<RpcStatus> loadCollection() {
@@ -248,6 +248,7 @@ public class GeneralExample {
         R<RpcStatus> response = milvusClient.createIndex(CreateIndexParam.newBuilder()
         R<RpcStatus> response = milvusClient.createIndex(CreateIndexParam.newBuilder()
                 .withCollectionName(COLLECTION_NAME)
                 .withCollectionName(COLLECTION_NAME)
                 .withFieldName(VECTOR_FIELD)
                 .withFieldName(VECTOR_FIELD)
+                .withIndexName(INDEX_NAME)
                 .withIndexType(INDEX_TYPE)
                 .withIndexType(INDEX_TYPE)
                 .withMetricType(MetricType.L2)
                 .withMetricType(MetricType.L2)
                 .withExtraParam(INDEX_PARAM)
                 .withExtraParam(INDEX_PARAM)
@@ -262,7 +263,7 @@ public class GeneralExample {
         System.out.println("========== dropIndex() ==========");
         System.out.println("========== dropIndex() ==========");
         R<RpcStatus> response = milvusClient.dropIndex(DropIndexParam.newBuilder()
         R<RpcStatus> response = milvusClient.dropIndex(DropIndexParam.newBuilder()
                 .withCollectionName(COLLECTION_NAME)
                 .withCollectionName(COLLECTION_NAME)
-                .withFieldName(VECTOR_FIELD)
+                .withIndexName(INDEX_NAME)
                 .build());
                 .build());
         handleResponseStatus(response);
         handleResponseStatus(response);
         System.out.println(response);
         System.out.println(response);
@@ -273,7 +274,7 @@ public class GeneralExample {
         System.out.println("========== describeIndex() ==========");
         System.out.println("========== describeIndex() ==========");
         R<DescribeIndexResponse> response = milvusClient.describeIndex(DescribeIndexParam.newBuilder()
         R<DescribeIndexResponse> response = milvusClient.describeIndex(DescribeIndexParam.newBuilder()
                 .withCollectionName(COLLECTION_NAME)
                 .withCollectionName(COLLECTION_NAME)
-                .withFieldName(VECTOR_FIELD)
+                .withIndexName(INDEX_NAME)
                 .build());
                 .build());
         handleResponseStatus(response);
         handleResponseStatus(response);
         System.out.println(response);
         System.out.println(response);
@@ -284,7 +285,7 @@ public class GeneralExample {
         System.out.println("========== getIndexState() ==========");
         System.out.println("========== getIndexState() ==========");
         R<GetIndexStateResponse> response = milvusClient.getIndexState(GetIndexStateParam.newBuilder()
         R<GetIndexStateResponse> response = milvusClient.getIndexState(GetIndexStateParam.newBuilder()
                 .withCollectionName(COLLECTION_NAME)
                 .withCollectionName(COLLECTION_NAME)
-                .withFieldName(VECTOR_FIELD)
+                .withIndexName(INDEX_NAME)
                 .build());
                 .build());
         handleResponseStatus(response);
         handleResponseStatus(response);
         System.out.println(response);
         System.out.println(response);
@@ -296,6 +297,7 @@ public class GeneralExample {
         R<GetIndexBuildProgressResponse> response = milvusClient.getIndexBuildProgress(
         R<GetIndexBuildProgressResponse> response = milvusClient.getIndexBuildProgress(
                 GetIndexBuildProgressParam.newBuilder()
                 GetIndexBuildProgressParam.newBuilder()
                         .withCollectionName(COLLECTION_NAME)
                         .withCollectionName(COLLECTION_NAME)
+                        .withIndexName(INDEX_NAME)
                         .build());
                         .build());
         handleResponseStatus(response);
         handleResponseStatus(response);
         System.out.println(response);
         System.out.println(response);
@@ -352,63 +354,42 @@ public class GeneralExample {
         return response;
         return response;
     }
     }
 
 
-    private R<SearchResults> searchProfile(String expr) {
-        System.out.println("========== searchProfile() ==========");
-        long begin = System.currentTimeMillis();
-
-        List<String> outFields = Collections.singletonList(AGE_FIELD);
-        List<ByteBuffer> vectors = generateBinaryVectors(5);
-
-        SearchParam searchParam = SearchParam.newBuilder()
-                .withCollectionName(COLLECTION_NAME)
-                .withMetricType(MetricType.HAMMING)
-                .withOutFields(outFields)
-                .withTopK(SEARCH_K)
-                .withVectors(vectors)
-                .withVectorFieldName(PROFILE_FIELD)
-                .withExpr(expr)
-                .withParams(SEARCH_PARAM)
-                .build();
-
-
-        R<SearchResults> response = milvusClient.search(searchParam);
-        long end = System.currentTimeMillis();
-        long cost = (end - begin);
-        System.out.println("Search time cost: " + cost + "ms");
-
-        handleResponseStatus(response);
-        SearchResultsWrapper wrapper = new SearchResultsWrapper(response.getData().getResults());
-        for (int i = 0; i < vectors.size(); ++i) {
-            System.out.println("Search result of No." + i);
-            List<SearchResultsWrapper.IDScore> scores = wrapper.getIDScore(i);
-            System.out.println(scores);
-            System.out.println("Output field data for No." + i);
-            System.out.println(wrapper.getFieldData(AGE_FIELD, i));
-        }
-
-        return response;
-    }
-
-    private R<CalcDistanceResults> calDistance() {
-        System.out.println("========== calDistance() ==========");
-        Random ran=new Random();
-        List<Float> vector1 = new ArrayList<>();
-        List<Float> vector2 = new ArrayList<>();
-        for (int d = 0; d < VECTOR_DIM; ++d) {
-            vector1.add(ran.nextFloat());
-            vector2.add(ran.nextFloat());
-        }
-
-        CalcDistanceParam calcDistanceParam = CalcDistanceParam.newBuilder()
-                .withVectorsLeft(Collections.singletonList(vector1))
-                .withVectorsRight(Collections.singletonList(vector2))
-                .withMetricType(MetricType.L2)
-                .build();
-        R<CalcDistanceResults> response = milvusClient.calcDistance(calcDistanceParam);
-        handleResponseStatus(response);
-        System.out.println(response);
-        return response;
-    }
+//    private R<SearchResults> searchProfile(String expr) {
+//        System.out.println("========== searchProfile() ==========");
+//        long begin = System.currentTimeMillis();
+//
+//        List<String> outFields = Collections.singletonList(AGE_FIELD);
+//        List<ByteBuffer> vectors = generateBinaryVectors(5);
+//
+//        SearchParam searchParam = SearchParam.newBuilder()
+//                .withCollectionName(COLLECTION_NAME)
+//                .withMetricType(MetricType.HAMMING)
+//                .withOutFields(outFields)
+//                .withTopK(SEARCH_K)
+//                .withVectors(vectors)
+//                .withVectorFieldName(PROFILE_FIELD)
+//                .withExpr(expr)
+//                .withParams(SEARCH_PARAM)
+//                .build();
+//
+//
+//        R<SearchResults> response = milvusClient.search(searchParam);
+//        long end = System.currentTimeMillis();
+//        long cost = (end - begin);
+//        System.out.println("Search time cost: " + cost + "ms");
+//
+//        handleResponseStatus(response);
+//        SearchResultsWrapper wrapper = new SearchResultsWrapper(response.getData().getResults());
+//        for (int i = 0; i < vectors.size(); ++i) {
+//            System.out.println("Search result of No." + i);
+//            List<SearchResultsWrapper.IDScore> scores = wrapper.getIDScore(i);
+//            System.out.println(scores);
+//            System.out.println("Output field data for No." + i);
+//            System.out.println(wrapper.getFieldData(AGE_FIELD, i));
+//        }
+//
+//        return response;
+//    }
 
 
     private R<QueryResults> query(String expr) {
     private R<QueryResults> query(String expr) {
         System.out.println("========== query() ==========");
         System.out.println("========== query() ==========");
@@ -494,7 +475,9 @@ public class GeneralExample {
     public static void main(String[] args) {
     public static void main(String[] args) {
         GeneralExample example = new GeneralExample();
         GeneralExample example = new GeneralExample();
 
 
-        example.dropCollection();
+        if (example.hasCollection()) {
+            example.dropCollection();
+        }
         example.createCollection(2000);
         example.createCollection(2000);
         example.hasCollection();
         example.hasCollection();
         example.describeCollection();
         example.describeCollection();
@@ -530,7 +513,6 @@ public class GeneralExample {
         example.searchFace(searchExpr);
         example.searchFace(searchExpr);
 //        searchExpr = AGE_FIELD + " <= 30";
 //        searchExpr = AGE_FIELD + " <= 30";
 //        example.searchProfile(searchExpr);
 //        example.searchProfile(searchExpr);
-        example.calDistance();
         example.compact();
         example.compact();
         example.getCollectionStatistics();
         example.getCollectionStatistics();