2
0

MilvusClientExample.java 10.0 KB

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