Browse Source

Fix example error (#443)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
groot 2 years ago
parent
commit
96609be4fc
1 changed files with 28 additions and 1 deletions
  1. 28 1
      examples/main/io/milvus/GeneralExample.java

+ 28 - 1
examples/main/io/milvus/GeneralExample.java

@@ -169,6 +169,10 @@ public class GeneralExample {
     }
     }
 
 
     private R<GetCollectionStatisticsResponse> getCollectionStatistics() {
     private R<GetCollectionStatisticsResponse> getCollectionStatistics() {
+        // call flush() to flush the insert buffer to storage,
+        // so that the getCollectionStatistics() can get correct number
+        milvusClient.flush(FlushParam.newBuilder().addCollectionName(COLLECTION_NAME).build());
+        
         System.out.println("========== getCollectionStatistics() ==========");
         System.out.println("========== getCollectionStatistics() ==========");
         R<GetCollectionStatisticsResponse> response = milvusClient.getCollectionStatistics(
         R<GetCollectionStatisticsResponse> response = milvusClient.getCollectionStatistics(
                 GetCollectionStatisticsParam.newBuilder()
                 GetCollectionStatisticsParam.newBuilder()
@@ -499,15 +503,38 @@ public class GeneralExample {
         }
         }
         example.getCollectionStatistics();
         example.getCollectionStatistics();
 
 
+        // Must create an index before load(), FLAT is brute-force search(no index)
+        milvusClient.createIndex(CreateIndexParam.newBuilder()
+                .withCollectionName(COLLECTION_NAME)
+                .withFieldName(VECTOR_FIELD)
+                .withIndexName(INDEX_NAME)
+                .withIndexType(IndexType.FLAT)
+                .withMetricType(MetricType.L2)
+                .withExtraParam(INDEX_PARAM)
+                .withSyncMode(Boolean.TRUE)
+                .build());
+
+        // loadCollection() must be called before search()
+        example.loadCollection();
+
+        System.out.println("Search without index");
+        String searchExpr = AGE_FIELD + " > 50";
+        example.searchFace(searchExpr);
+
+        // must call releaseCollection() before re-create index
+        example.releaseCollection();
+
+        // re-create another index
+        example.dropIndex();
         example.createIndex();
         example.createIndex();
         example.describeIndex();
         example.describeIndex();
         example.getIndexBuildProgress();
         example.getIndexBuildProgress();
         example.getIndexState();
         example.getIndexState();
 
 
+        // loadCollection() must be called before search()
         example.loadCollection();
         example.loadCollection();
 
 
         System.out.println("Search with index");
         System.out.println("Search with index");
-        String searchExpr = AGE_FIELD + " > 50";
         example.searchFace(searchExpr);
         example.searchFace(searchExpr);
 
 
         String deleteExpr = ID_FIELD + " in " + deleteIds.toString();
         String deleteExpr = ID_FIELD + " in " + deleteIds.toString();