Skip to content

Commit

Permalink
Merge pull request #143 from sahuang/0.9.1-fix
Browse files Browse the repository at this point in the history
Polish code
  • Loading branch information
Xiaohai Xu authored Oct 29, 2020
2 parents 4c10f98 + 7c8cbdb commit e2ff9e5
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 88 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
90 changes: 51 additions & 39 deletions examples/src/main/java/MilvusBasicExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,10 +38,11 @@
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 {

Expand Down Expand Up @@ -68,7 +77,8 @@ public static void run() {
*
* 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);

/*
Expand All @@ -91,12 +101,12 @@ public static void run() {
* 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
Expand Down Expand Up @@ -134,18 +144,19 @@ public static void run() {
* 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);
Expand Down Expand Up @@ -203,27 +214,28 @@ public static void run() {
*/
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());
Expand Down Expand Up @@ -283,4 +295,4 @@ public static void run() {
// Close connection
client.close();
}
}
}
103 changes: 59 additions & 44 deletions examples/src/main/java/MilvusIndexExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,15 +40,16 @@
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 {
Expand Down Expand Up @@ -77,11 +89,11 @@ public static void run() throws IOException {

// 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);

Expand Down Expand Up @@ -123,15 +135,16 @@ public static void run() throws IOException {
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:
Expand All @@ -143,11 +156,11 @@ public static void run() throws IOException {
* 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);

Expand All @@ -166,23 +179,24 @@ public static void run() throws IOException {
*/
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());
Expand All @@ -191,8 +205,9 @@ public static void run() throws IOException {
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"));
}
Expand All @@ -211,4 +226,4 @@ public static void run() throws IOException {
// Close connection
client.close();
}
}
}
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit e2ff9e5

Please sign in to comment.