Browse Source

Merge pull request #143 from sahuang/0.9.1-fix

Polish code
Xiaohai Xu 4 years ago
parent
commit
e2ff9e5e66

+ 2 - 3
README.md

@@ -15,8 +15,7 @@ The following table shows compatibilities between Milvus and Java SDK.
 
 | Milvus version | Java SDK version |
 | :------------: | :--------------: |
-|     0.11.1     |    0.9.1         |
-|     0.11.0     |    0.9.0         |
+|     0.11.0     |    0.9.1         |
 |     0.10.3     |    0.8.5         |
 |     0.10.2     |    0.8.4         |
 |     0.10.1     |    0.8.3         |
@@ -48,7 +47,7 @@ You can use **Apache Maven** or **Gradle**/**Grails** to download the SDK.
 
 ### Examples
 
-Please refer to [examples](https://github.com/milvus-io/milvus-sdk-java/tree/master/examples) folder for Java SDK examples.
+Please refer to [examples](https://github.com/milvus-io/milvus-sdk-java/tree/0.9.1/examples) folder for Java SDK examples.
 
 ### Documentation
 

+ 1 - 1
examples/pom.xml

@@ -63,7 +63,7 @@
         <dependency>
             <groupId>io.milvus</groupId>
             <artifactId>milvus-sdk-java</artifactId>
-            <version>0.9.1-SNAPSHOT</version>
+            <version>0.9.1</version>
         </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>

+ 51 - 39
examples/src/main/java/MilvusBasicExample.java

@@ -19,7 +19,15 @@
 
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
-import io.milvus.client.*;
+import io.milvus.client.CollectionMapping;
+import io.milvus.client.CompactParam;
+import io.milvus.client.ConnectParam;
+import io.milvus.client.DataType;
+import io.milvus.client.InsertParam;
+import io.milvus.client.MilvusClient;
+import io.milvus.client.MilvusGrpcClient;
+import io.milvus.client.SearchParam;
+import io.milvus.client.SearchResult;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -30,10 +38,11 @@ import java.util.stream.Collectors;
 import java.util.stream.DoubleStream;
 import org.json.JSONObject;
 
-/** This is a simple example demonstrating how to use Milvus Java SDK v0.9.1.
- * For detailed API documentation, please refer to
- * https://milvus-io.github.io/milvus-sdk-java/javadoc/io/milvus/client/package-summary.html
- * You can also find more information on https://milvus.io/docs/overview.md
+/**
+ * This is a simple example demonstrating how to use Milvus Java SDK v0.9.1. For detailed API
+ * documentation, please refer to
+ * https://milvus-io.github.io/milvus-sdk-java/javadoc/io/milvus/client/package-summary.html You can
+ * also find more information on https://milvus.io/docs/overview.md
  */
 public class MilvusBasicExample {
 
@@ -68,7 +77,8 @@ public class MilvusBasicExample {
      *
      *   You can use `withLogging()` for `client` to enable logging framework.
      */
-    ConnectParam connectParam = new ConnectParam.Builder().withHost("127.0.0.1").withPort(19530).build();
+    ConnectParam connectParam =
+        new ConnectParam.Builder().withHost("127.0.0.1").withPort(19530).build();
     MilvusClient client = new MilvusGrpcClient(connectParam);
 
     /*
@@ -91,12 +101,12 @@ public class MilvusBasicExample {
      *   dimension must be specified. `auto_id` is set to false so we can provide custom ids.
      */
     final int dimension = 8;
-    CollectionMapping collectionMapping = CollectionMapping
-        .create(collectionName)
-        .addField("duration", DataType.INT32)
-        .addField("release_year", DataType.INT64)
-        .addVectorField("embedding", DataType.VECTOR_FLOAT, dimension)
-        .setParamsInJson("{\"segment_row_limit\": 4096, \"auto_id\": false}");
+    CollectionMapping collectionMapping =
+        CollectionMapping.create(collectionName)
+            .addField("duration", DataType.INT32)
+            .addField("release_year", DataType.INT64)
+            .addVectorField("embedding", DataType.VECTOR_FLOAT, dimension)
+            .setParamsInJson("{\"segment_row_limit\": 4096, \"auto_id\": false}");
 
     client.createCollection(collectionMapping);
     // Check the existence of collection
@@ -134,18 +144,19 @@ public class MilvusBasicExample {
      *   The titles and relative film properties are listed below for your reference.
      */
     List<Long> ids = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
-    List<String> titles = Arrays.asList("The_Fellowship_of_the_Ring", "The_Two_Towers", "The_Return_of_the_King");
+    List<String> titles =
+        Arrays.asList("The_Fellowship_of_the_Ring", "The_Two_Towers", "The_Return_of_the_King");
     List<Integer> durations = new ArrayList<>(Arrays.asList(208, 226, 252));
     List<Long> releaseYears = new ArrayList<>(Arrays.asList(2001L, 2002L, 2003L));
     List<List<Float>> embeddings = randomFloatVectors(3, dimension);
 
-    InsertParam insertParam = InsertParam
-        .create(collectionName)
-        .addField("duration", DataType.INT32, durations)
-        .addField("release_year", DataType.INT64, releaseYears)
-        .addVectorField("embedding", DataType.VECTOR_FLOAT, embeddings)
-        .setEntityIds(ids)
-        .setPartitionTag(partitionTag);
+    InsertParam insertParam =
+        InsertParam.create(collectionName)
+            .addField("duration", DataType.INT32, durations)
+            .addField("release_year", DataType.INT64, releaseYears)
+            .addVectorField("embedding", DataType.VECTOR_FLOAT, embeddings)
+            .setEntityIds(ids)
+            .setPartitionTag(partitionTag);
 
     System.out.println("\n--------Insert Entities--------");
     List<Long> entityIds = client.insert(insertParam);
@@ -203,27 +214,28 @@ public class MilvusBasicExample {
      */
     List<List<Float>> queryEmbedding = randomFloatVectors(1, dimension);
     final long topK = 3;
-    String dsl = String.format(
-        "{\"bool\": {"
-            + "\"must\": [{"
-            + "    \"range\": {"
-            + "        \"duration\": {\"GT\": 250}" // "GT" for greater than
-            + "    }},{"
-            + "    \"term\": {"
-            + "        \"release_year\": %s" // "term" is a list
-            + "    }},{"
-            + "    \"vector\": {"
-            + "        \"embedding\": {"
-            + "            \"topk\": %d, \"metric_type\": \"L2\", \"type\": \"float\", \"query\": %s"
-            + "    }}}]}}",
-        releaseYears.subList(1, 3).toString(), topK, queryEmbedding.toString());
+    String dsl =
+        String.format(
+            "{\"bool\": {"
+                + "\"must\": [{"
+                + "    \"range\": {"
+                + "        \"duration\": {\"GT\": 250}" // "GT" for greater than
+                + "    }},{"
+                + "    \"term\": {"
+                + "        \"release_year\": %s" // "term" is a list
+                + "    }},{"
+                + "    \"vector\": {"
+                + "        \"embedding\": {"
+                + "            \"topk\": %d, \"metric_type\": \"L2\", \"type\": \"float\", \"query\": %s"
+                + "    }}}]}}",
+            releaseYears.subList(1, 3).toString(), topK, queryEmbedding.toString());
 
     // Only specified fields in `setParamsInJson` will be returned from search request.
     // If not set, all fields will be returned.
-    SearchParam searchParam = SearchParam
-        .create(collectionName)
-        .setDsl(dsl)
-        .setParamsInJson("{\"fields\": [\"duration\", \"release_year\", \"embedding\"]}");
+    SearchParam searchParam =
+        SearchParam.create(collectionName)
+            .setDsl(dsl)
+            .setParamsInJson("{\"fields\": [\"duration\", \"release_year\", \"embedding\"]}");
     System.out.println("\n--------Search Result--------");
     SearchResult searchResult = client.search(searchParam);
     System.out.println("- ids: " + searchResult.getResultIdsList().toString());
@@ -283,4 +295,4 @@ public class MilvusBasicExample {
     // Close connection
     client.close();
   }
-}
+}

+ 59 - 44
examples/src/main/java/MilvusIndexExample.java

@@ -17,7 +17,18 @@
  * under the License.
  */
 
-import io.milvus.client.*;
+import io.milvus.client.CollectionMapping;
+import io.milvus.client.ConnectParam;
+import io.milvus.client.DataType;
+import io.milvus.client.Index;
+import io.milvus.client.IndexType;
+import io.milvus.client.InsertParam;
+import io.milvus.client.JsonBuilder;
+import io.milvus.client.MetricType;
+import io.milvus.client.MilvusClient;
+import io.milvus.client.MilvusGrpcClient;
+import io.milvus.client.SearchParam;
+import io.milvus.client.SearchResult;
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
@@ -29,15 +40,16 @@ import java.util.stream.Collectors;
 import java.util.stream.DoubleStream;
 import org.json.JSONObject;
 
-/** This is an example of Milvus Java SDK v0.9.1. In particular, we demonstrate how we can build
- * and search by index in Milvus.
+/**
+ * This is an example of Milvus Java SDK v0.9.1. In particular, we demonstrate how we can build and
+ * search by index in Milvus.
  *
- * We will be using `films.csv` as our dataset. There are 4 columns in the file, namely
- * `id`, `title`, `release_year` and `embedding`. The dataset comes from MovieLens `ml-latest-small`,
- * with id and embedding being fake values.
+ * <p>We will be using `films.csv` as our dataset. There are 4 columns in the file, namely `id`,
+ * `title`, `release_year` and `embedding`. The dataset comes from MovieLens `ml-latest-small`, with
+ * id and embedding being fake values.
  *
- * We assume that you have walked through `MilvusBasicExample.java` and understand basic operations
- * in Milvus. For detailed API documentation, please refer to
+ * <p>We assume that you have walked through `MilvusBasicExample.java` and understand basic
+ * operations in Milvus. For detailed API documentation, please refer to
  * https://milvus-io.github.io/milvus-sdk-java/javadoc/io/milvus/client/package-summary.html
  */
 public class MilvusIndexExample {
@@ -77,11 +89,11 @@ public class MilvusIndexExample {
 
     // Create collection
     final int dimension = 8;
-    CollectionMapping collectionMapping = CollectionMapping
-        .create(collectionName)
-        .addField("release_year", DataType.INT64)
-        .addVectorField("embedding", DataType.VECTOR_FLOAT, dimension)
-        .setParamsInJson("{\"segment_row_limit\": 4096, \"auto_id\": false}");
+    CollectionMapping collectionMapping =
+        CollectionMapping.create(collectionName)
+            .addField("release_year", DataType.INT64)
+            .addVectorField("embedding", DataType.VECTOR_FLOAT, dimension)
+            .setParamsInJson("{\"segment_row_limit\": 4096, \"auto_id\": false}");
 
     client.createCollection(collectionMapping);
 
@@ -123,15 +135,16 @@ public class MilvusIndexExample {
     csvReader.close();
 
     // Now we can insert entities, the total row count should be 8657.
-    InsertParam insertParam = InsertParam
-        .create(collectionName)
-        .addField("release_year", DataType.INT64, releaseYears)
-        .addVectorField("embedding", DataType.VECTOR_FLOAT, embeddings)
-        .setEntityIds(ids);
+    InsertParam insertParam =
+        InsertParam.create(collectionName)
+            .addField("release_year", DataType.INT64, releaseYears)
+            .addVectorField("embedding", DataType.VECTOR_FLOAT, embeddings)
+            .setEntityIds(ids);
 
     client.insert(insertParam);
     client.flush(collectionName);
-    System.out.printf("There are %d films in the collection.\n", client.countEntities(collectionName));
+    System.out.printf(
+        "There are %d films in the collection.\n", client.countEntities(collectionName));
 
     /*
      * Basic create index:
@@ -143,11 +156,11 @@ public class MilvusIndexExample {
      *   Note that if there is already an index and create index is called again, the previous index
      *   will be replaced.
      */
-    Index index = Index
-        .create(collectionName, "embedding")
-        .setIndexType(IndexType.IVF_FLAT)
-        .setMetricType(MetricType.L2)
-        .setParamsInJson(new JsonBuilder().param("nlist", 100).build());
+    Index index =
+        Index.create(collectionName, "embedding")
+            .setIndexType(IndexType.IVF_FLAT)
+            .setMetricType(MetricType.L2)
+            .setParamsInJson(new JsonBuilder().param("nlist", 100).build());
 
     client.createIndex(index);
 
@@ -166,23 +179,24 @@ public class MilvusIndexExample {
      */
     List<List<Float>> queryEmbedding = randomFloatVectors(1, dimension);
     final long topK = 3;
-    String dsl = String.format(
-        "{\"bool\": {"
-            + "\"must\": [{"
-            + "    \"term\": {"
-            + "        \"release_year\": [2002, 1995]"
-            + "    }},{"
-            + "    \"vector\": {"
-            + "        \"embedding\": {"
-            + "            \"topk\": %d, \"metric_type\": \"L2\", \"type\": \"float\", \"query\": "
-            + "%s, \"params\": {\"nprobe\": 8}"
-            + "    }}}]}}",
-        topK, queryEmbedding.toString());
-
-    SearchParam searchParam = SearchParam
-        .create(collectionName)
-        .setDsl(dsl)
-        .setParamsInJson("{\"fields\": [\"release_year\", \"embedding\"]}");
+    String dsl =
+        String.format(
+            "{\"bool\": {"
+                + "\"must\": [{"
+                + "    \"term\": {"
+                + "        \"release_year\": [2002, 1995]"
+                + "    }},{"
+                + "    \"vector\": {"
+                + "        \"embedding\": {"
+                + "            \"topk\": %d, \"metric_type\": \"L2\", \"type\": \"float\", \"query\": "
+                + "%s, \"params\": {\"nprobe\": 8}"
+                + "    }}}]}}",
+            topK, queryEmbedding.toString());
+
+    SearchParam searchParam =
+        SearchParam.create(collectionName)
+            .setDsl(dsl)
+            .setParamsInJson("{\"fields\": [\"release_year\", \"embedding\"]}");
     System.out.println("\n--------Search Result--------");
     SearchResult searchResult = client.search(searchParam);
     System.out.println("- ids: " + searchResult.getResultIdsList().toString());
@@ -191,8 +205,9 @@ public class MilvusIndexExample {
       for (int i = 0; i < singleQueryResult.size(); i++) {
         Map<String, Object> res = singleQueryResult.get(i);
         System.out.println("==");
-        System.out.println("- title: " + titles.get(
-            Math.toIntExact(searchResult.getResultIdsList().get(0).get(i))));
+        System.out.println(
+            "- title: "
+                + titles.get(Math.toIntExact(searchResult.getResultIdsList().get(0).get(i))));
         System.out.println("- release_year: " + res.get("release_year"));
         System.out.println("- embedding: " + res.get("embedding"));
       }
@@ -211,4 +226,4 @@ public class MilvusIndexExample {
     // Close connection
     client.close();
   }
-}
+}

+ 4 - 1
pom.xml

@@ -25,7 +25,7 @@
 
     <groupId>io.milvus</groupId>
     <artifactId>milvus-sdk-java</artifactId>
-    <version>0.9.1-SNAPSHOT</version>
+    <version>0.9.1</version>
     <packaging>jar</packaging>
 
     <name>io.milvus:milvus-sdk-java</name>
@@ -289,6 +289,9 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>2.22.2</version>
+                <configuration>
+                    <skipTests>true</skipTests>
+                </configuration>
                 <dependencies>
                     <dependency>
                         <groupId>org.junit.jupiter</groupId>