SimpleExample.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. package io.milvus.v2;
  20. import com.google.gson.*;
  21. import io.milvus.v2.client.*;
  22. import io.milvus.v2.common.ConsistencyLevel;
  23. import io.milvus.v2.service.collection.request.CreateCollectionReq;
  24. import io.milvus.v2.service.collection.request.DropCollectionReq;
  25. import io.milvus.v2.service.vector.request.*;
  26. import io.milvus.v2.service.vector.request.data.FloatVec;
  27. import io.milvus.v2.service.vector.response.*;
  28. import java.util.*;
  29. public class SimpleExample {
  30. public static void main(String[] args) {
  31. ConnectConfig config = ConnectConfig.builder()
  32. .uri("http://localhost:19530")
  33. .build();
  34. MilvusClientV2 client = new MilvusClientV2(config);
  35. String collectionName = "java_sdk_example_simple_v2";
  36. // Drop collection if exists
  37. client.dropCollection(DropCollectionReq.builder()
  38. .collectionName(collectionName)
  39. .build());
  40. // Quickly create a collection with "id" field and "vector" field
  41. client.createCollection(CreateCollectionReq.builder()
  42. .collectionName(collectionName)
  43. .dimension(4)
  44. .build());
  45. System.out.printf("Collection '%s' created\n", collectionName);
  46. // Insert some data
  47. List<JsonObject> rows = new ArrayList<>();
  48. Gson gson = new Gson();
  49. for (int i = 0; i < 100; i++) {
  50. JsonObject row = new JsonObject();
  51. row.addProperty("id", i);
  52. row.add("vector", gson.toJsonTree(new float[]{i, (float) i /2, (float) i /3, (float) i /4}));
  53. row.addProperty(String.format("dynamic_%d", i), "this is dynamic value"); // this value is stored in dynamic field
  54. rows.add(row);
  55. }
  56. InsertResp insertR = client.insert(InsertReq.builder()
  57. .collectionName(collectionName)
  58. .data(rows)
  59. .build());
  60. System.out.printf("%d rows inserted\n", insertR.getInsertCnt());
  61. // Get row count, set ConsistencyLevel.STRONG to sync the data to query node so that data is visible
  62. QueryResp countR = client.query(QueryReq.builder()
  63. .collectionName(collectionName)
  64. .filter("")
  65. .outputFields(Collections.singletonList("count(*)"))
  66. .consistencyLevel(ConsistencyLevel.STRONG)
  67. .build());
  68. System.out.printf("%d rows persisted\n", (long)countR.getQueryResults().get(0).getEntity().get("count(*)"));
  69. // Retrieve
  70. List<Object> ids = Arrays.asList(1L, 50L);
  71. GetResp getR = client.get(GetReq.builder()
  72. .collectionName(collectionName)
  73. .ids(ids)
  74. .outputFields(Collections.singletonList("*"))
  75. .build());
  76. System.out.println("\nRetrieve results:");
  77. for (QueryResp.QueryResult result : getR.getGetResults()) {
  78. System.out.println(result.getEntity());
  79. }
  80. // Search
  81. SearchResp searchR = client.search(SearchReq.builder()
  82. .collectionName(collectionName)
  83. .data(Collections.singletonList(new FloatVec(new float[]{1.0f, 1.0f, 1.0f, 1.0f})))
  84. .filter("id < 100")
  85. .topK(10)
  86. .outputFields(Collections.singletonList("*"))
  87. .build());
  88. List<List<SearchResp.SearchResult>> searchResults = searchR.getSearchResults();
  89. System.out.println("\nSearch results:");
  90. for (List<SearchResp.SearchResult> results : searchResults) {
  91. for (SearchResp.SearchResult result : results) {
  92. System.out.printf("ID: %d, Score: %f, %s\n", (long)result.getId(), result.getScore(), result.getEntity().toString());
  93. }
  94. }
  95. client.close();
  96. }
  97. }