MilvusClientExample.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import com.google.common.util.concurrent.ListenableFuture;
  20. import com.google.gson.JsonObject;
  21. import io.milvus.client.*;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.SplittableRandom;
  25. import java.util.concurrent.ExecutionException;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.DoubleStream;
  28. // This is a simple example demonstrating how to use Milvus Java SDK.
  29. // For detailed API document, please refer to
  30. // https://milvus-io.github.io/milvus-sdk-java/javadoc/io/milvus/client/package-summary.html
  31. // You can also find more information on https://milvus.io/
  32. public class MilvusClientExample {
  33. // Helper function that generates random vectors
  34. static List<List<Float>> generateVectors(int vectorCount, int dimension) {
  35. SplittableRandom splitcollectionRandom = new SplittableRandom();
  36. List<List<Float>> vectors = new ArrayList<>(vectorCount);
  37. for (int i = 0; i < vectorCount; ++i) {
  38. splitcollectionRandom = splitcollectionRandom.split();
  39. DoubleStream doubleStream = splitcollectionRandom.doubles(dimension);
  40. List<Float> vector =
  41. doubleStream.boxed().map(Double::floatValue).collect(Collectors.toList());
  42. vectors.add(vector);
  43. }
  44. return vectors;
  45. }
  46. // Helper function that normalizes a vector if you are using IP (Inner Product) as your metric
  47. // type
  48. static List<Float> normalizeVector(List<Float> vector) {
  49. float squareSum = vector.stream().map(x -> x * x).reduce((float) 0, Float::sum);
  50. final float norm = (float) Math.sqrt(squareSum);
  51. vector = vector.stream().map(x -> x / norm).collect(Collectors.toList());
  52. return vector;
  53. }
  54. public static void main(String[] args) throws InterruptedException {
  55. // You may need to change the following to the host and port of your Milvus server
  56. String host = "localhost";
  57. int port = 19530;
  58. if (args.length >= 2) {
  59. host = args[0];
  60. port = Integer.parseInt(args[1]);
  61. }
  62. ConnectParam connectParam = new ConnectParam.Builder().withHost(host).withPort(port).build();
  63. MilvusClient client = new MilvusGrpcClient(connectParam);
  64. // Create a collection with the following collection mapping
  65. final String collectionName = "example"; // collection name
  66. final int dimension = 128; // dimension of each vector
  67. final int indexFileSize = 1024; // maximum size (in MB) of each index file
  68. final MetricType metricType = MetricType.IP; // we choose IP (Inner Product) as our metric type
  69. CollectionMapping collectionMapping =
  70. new CollectionMapping.Builder(collectionName, dimension)
  71. .withIndexFileSize(indexFileSize)
  72. .withMetricType(metricType)
  73. .build();
  74. Response createCollectionResponse = client.createCollection(collectionMapping);
  75. // Check whether the collection exists
  76. HasCollectionResponse hasCollectionResponse = client.hasCollection(collectionName);
  77. // Get collection info
  78. GetCollectionInfoResponse getCollectionInfoResponse = client.getCollectionInfo(collectionName);
  79. // Insert randomly generated vectors to collection
  80. final int vectorCount = 100000;
  81. List<List<Float>> vectors = generateVectors(vectorCount, dimension);
  82. vectors =
  83. vectors.stream().map(MilvusClientExample::normalizeVector).collect(Collectors.toList());
  84. InsertParam insertParam =
  85. new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
  86. InsertResponse insertResponse = client.insert(insertParam);
  87. // Insert returns a list of vector ids that you will be using (if you did not supply them
  88. // yourself) to reference the vectors you just inserted
  89. List<Long> vectorIds = insertResponse.getVectorIds();
  90. // Flush data in collection
  91. Response flushResponse = client.flush(collectionName);
  92. // Get current entity count of collection
  93. CountEntitiesResponse ountEntitiesResponse = client.countEntities(collectionName);
  94. // Create index for the collection
  95. // We choose IVF_SQ8 as our index type here. Refer to IndexType javadoc for a
  96. // complete explanation of different index types
  97. final IndexType indexType = IndexType.IVF_SQ8;
  98. // Each index type has its optional parameters you can set. Refer to the Milvus documentation
  99. // for how to set the optimal parameters based on your needs.
  100. JsonObject indexParamsJson = new JsonObject();
  101. indexParamsJson.addProperty("nlist", 16384);
  102. Index index =
  103. new Index.Builder(collectionName, indexType)
  104. .withParamsInJson(indexParamsJson.toString())
  105. .build();
  106. Response createIndexResponse = client.createIndex(index);
  107. // Get index info for your collection
  108. GetIndexInfoResponse getIndexInfoResponse = client.getIndexInfo(collectionName);
  109. System.out.format("Index Info: %s\n", getIndexInfoResponse.getIndex().get().toString());
  110. // Get collection info
  111. Response getCollectionStatsResponse = client.getCollectionStats(collectionName);
  112. if (getCollectionStatsResponse.ok()) {
  113. // Collection info is sent back with JSON type string
  114. String jsonString = getCollectionStatsResponse.getMessage();
  115. System.out.format("Collection Stats: %s\n", jsonString);
  116. }
  117. // Check whether a partition exists in collection
  118. // Obviously we do not have partition "tag" now
  119. HasPartitionResponse testHasPartition = client.hasPartition(collectionName, "tag");
  120. if (testHasPartition.ok() && testHasPartition.hasPartition()) {
  121. throw new AssertionError("Wrong results!");
  122. }
  123. // Search vectors
  124. // Searching the first 5 vectors of the vectors we just inserted
  125. final int searchBatchSize = 5;
  126. List<List<Float>> vectorsToSearch = vectors.subList(0, searchBatchSize);
  127. final long topK = 10;
  128. // Based on the index you created, the available search parameters will be different. Refer to
  129. // the Milvus documentation for how to set the optimal parameters based on your needs.
  130. JsonObject searchParamsJson = new JsonObject();
  131. searchParamsJson.addProperty("nprobe", 20);
  132. SearchParam searchParam =
  133. new SearchParam.Builder(collectionName)
  134. .withFloatVectors(vectorsToSearch)
  135. .withTopK(topK)
  136. .withParamsInJson(searchParamsJson.toString())
  137. .build();
  138. SearchResponse searchResponse = client.search(searchParam);
  139. if (searchResponse.ok()) {
  140. List<List<SearchResponse.QueryResult>> queryResultsList =
  141. searchResponse.getQueryResultsList();
  142. final double epsilon = 0.001;
  143. for (int i = 0; i < searchBatchSize; i++) {
  144. // Since we are searching for vector that is already present in the collection,
  145. // the first result vector should be itself and the distance (inner product) should be
  146. // very close to 1 (some precision is lost during the process)
  147. SearchResponse.QueryResult firstQueryResult = queryResultsList.get(i).get(0);
  148. if (firstQueryResult.getVectorId() != vectorIds.get(i)
  149. || Math.abs(1 - firstQueryResult.getDistance()) > epsilon) {
  150. throw new AssertionError("Wrong results!");
  151. }
  152. }
  153. }
  154. // You can also get result ids and distances separately
  155. List<List<Long>> resultIds = searchResponse.getResultIdsList();
  156. List<List<Float>> resultDistances = searchResponse.getResultDistancesList();
  157. // You can send search request asynchronously, which returns a ListenableFuture object
  158. ListenableFuture<SearchResponse> searchResponseFuture = client.searchAsync(searchParam);
  159. try {
  160. // Get search response immediately. Obviously you will want to do more complicated stuff with
  161. // ListenableFuture
  162. searchResponseFuture.get();
  163. } catch (ExecutionException e) {
  164. e.printStackTrace();
  165. }
  166. // Delete the first 5 vectors you just searched
  167. Response deleteByIdsResponse =
  168. client.deleteEntityByID(collectionName, "", vectorIds.subList(0, searchBatchSize));
  169. flushResponse = client.flush(collectionName);
  170. // Try to get the corresponding vector of the first id you just deleted.
  171. GetEntityByIDResponse getEntityByIDResponse =
  172. client.getEntityByID(collectionName, "", vectorIds.subList(0, searchBatchSize));
  173. // Obviously you won't get anything
  174. if (!getEntityByIDResponse.getFloatVectors().get(0).isEmpty()) {
  175. throw new AssertionError("This can never happen!");
  176. }
  177. // Compact the collection, erase deleted data from disk and rebuild index in background (if
  178. // the data size after compaction is still larger than indexFileSize). Data was only
  179. // soft-deleted until you call compact.
  180. Response compactResponse = client.compact(collectionName);
  181. // Drop index for the collection
  182. Response dropIndexResponse = client.dropIndex(collectionName);
  183. // Drop collection
  184. Response dropCollectionResponse = client.dropCollection(collectionName);
  185. // Disconnect from Milvus server
  186. client.close();
  187. }
  188. }