MilvusClientExample.java 10 KB

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