|
| 1 | +package io.milvus.v2; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import io.milvus.v1.CommonUtils; |
| 6 | +import io.milvus.v2.client.ConnectConfig; |
| 7 | +import io.milvus.v2.client.MilvusClientV2; |
| 8 | +import io.milvus.v2.common.ConsistencyLevel; |
| 9 | +import io.milvus.v2.common.DataType; |
| 10 | +import io.milvus.v2.common.IndexParam; |
| 11 | +import io.milvus.v2.service.collection.request.AddFieldReq; |
| 12 | +import io.milvus.v2.service.collection.request.CreateCollectionReq; |
| 13 | +import io.milvus.v2.service.collection.request.DropCollectionReq; |
| 14 | +import io.milvus.v2.service.vector.request.InsertReq; |
| 15 | +import io.milvus.v2.service.vector.request.QueryReq; |
| 16 | +import io.milvus.v2.service.vector.request.SearchReq; |
| 17 | +import io.milvus.v2.service.vector.request.data.BinaryVec; |
| 18 | +import io.milvus.v2.service.vector.request.data.Int8Vec; |
| 19 | +import io.milvus.v2.service.vector.response.QueryResp; |
| 20 | +import io.milvus.v2.service.vector.response.SearchResp; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.util.*; |
| 24 | + |
| 25 | +public class Int8VectorExample { |
| 26 | + private static final String COLLECTION_NAME = "java_sdk_example_int8_vector_v2"; |
| 27 | + private static final String ID_FIELD = "id"; |
| 28 | + private static final String VECTOR_FIELD = "vector"; |
| 29 | + |
| 30 | + private static final Integer VECTOR_DIM = 128; |
| 31 | + |
| 32 | + private static List<ByteBuffer> generateInt8Vectors(int count) { |
| 33 | + Random RANDOM = new Random(); |
| 34 | + List<ByteBuffer> vectors = new ArrayList<>(); |
| 35 | + for (int i = 0; i < count; i++) { |
| 36 | + ByteBuffer vector = ByteBuffer.allocate(VECTOR_DIM); |
| 37 | + for (int k = 0; k < VECTOR_DIM; ++k) { |
| 38 | + vector.put((byte) (RANDOM.nextInt(256) - 128)); |
| 39 | + } |
| 40 | + vectors.add(vector); |
| 41 | + } |
| 42 | + |
| 43 | + return vectors; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + ConnectConfig config = ConnectConfig.builder() |
| 49 | + .uri("http://localhost:19530") |
| 50 | + .build(); |
| 51 | + MilvusClientV2 client = new MilvusClientV2(config); |
| 52 | + |
| 53 | + // Drop collection if exists |
| 54 | + client.dropCollection(DropCollectionReq.builder() |
| 55 | + .collectionName(COLLECTION_NAME) |
| 56 | + .build()); |
| 57 | + |
| 58 | + // Create collection |
| 59 | + CreateCollectionReq.CollectionSchema collectionSchema = CreateCollectionReq.CollectionSchema.builder() |
| 60 | + .build(); |
| 61 | + collectionSchema.addField(AddFieldReq.builder() |
| 62 | + .fieldName(ID_FIELD) |
| 63 | + .dataType(DataType.Int64) |
| 64 | + .isPrimaryKey(Boolean.TRUE) |
| 65 | + .build()); |
| 66 | + collectionSchema.addField(AddFieldReq.builder() |
| 67 | + .fieldName(VECTOR_FIELD) |
| 68 | + .dataType(DataType.Int8Vector) |
| 69 | + .dimension(VECTOR_DIM) |
| 70 | + .build()); |
| 71 | + |
| 72 | + List<IndexParam> indexes = new ArrayList<>(); |
| 73 | + Map<String,Object> extraParams = new HashMap<>(); |
| 74 | + extraParams.put("M", 64); |
| 75 | + extraParams.put("efConstruction", 200); |
| 76 | + indexes.add(IndexParam.builder() |
| 77 | + .fieldName(VECTOR_FIELD) |
| 78 | + .indexType(IndexParam.IndexType.HNSW) |
| 79 | + .metricType(IndexParam.MetricType.L2) |
| 80 | + .extraParams(extraParams) |
| 81 | + .build()); |
| 82 | + |
| 83 | + CreateCollectionReq requestCreate = CreateCollectionReq.builder() |
| 84 | + .collectionName(COLLECTION_NAME) |
| 85 | + .collectionSchema(collectionSchema) |
| 86 | + .indexParams(indexes) |
| 87 | + .consistencyLevel(ConsistencyLevel.BOUNDED) |
| 88 | + .build(); |
| 89 | + client.createCollection(requestCreate); |
| 90 | + System.out.println("Collection created"); |
| 91 | + |
| 92 | + // Insert entities by rows |
| 93 | + int rowCount = 10000; |
| 94 | + List<ByteBuffer> vectors = generateInt8Vectors(rowCount); |
| 95 | + List<JsonObject> rows = new ArrayList<>(); |
| 96 | + Gson gson = new Gson(); |
| 97 | + for (long i = 0L; i < rowCount; ++i) { |
| 98 | + JsonObject row = new JsonObject(); |
| 99 | + row.addProperty(ID_FIELD, i); |
| 100 | + ByteBuffer vector = vectors.get((int)i); |
| 101 | + row.add(VECTOR_FIELD, gson.toJsonTree(vector.array())); |
| 102 | + rows.add(row); |
| 103 | + } |
| 104 | + |
| 105 | + client.insert(InsertReq.builder() |
| 106 | + .collectionName(COLLECTION_NAME) |
| 107 | + .data(rows) |
| 108 | + .build()); |
| 109 | + |
| 110 | + // Get row count, set ConsistencyLevel.STRONG to sync the data to query node so that data is visible |
| 111 | + QueryResp countR = client.query(QueryReq.builder() |
| 112 | + .collectionName(COLLECTION_NAME) |
| 113 | + .filter("") |
| 114 | + .outputFields(Collections.singletonList("count(*)")) |
| 115 | + .consistencyLevel(ConsistencyLevel.STRONG) |
| 116 | + .build()); |
| 117 | + System.out.printf("%d rows persisted\n", (long)countR.getQueryResults().get(0).getEntity().get("count(*)")); |
| 118 | + |
| 119 | + // Pick some vectors from the inserted vectors to search |
| 120 | + // Ensure the returned top1 item's ID should be equal to target vector's ID |
| 121 | + for (int i = 0; i < 10; i++) { |
| 122 | + Random ran = new Random(); |
| 123 | + int k = ran.nextInt(rowCount); |
| 124 | + ByteBuffer targetVector = vectors.get(k); |
| 125 | + SearchResp searchResp = client.search(SearchReq.builder() |
| 126 | + .collectionName(COLLECTION_NAME) |
| 127 | + .data(Collections.singletonList(new Int8Vec(targetVector))) |
| 128 | + .annsField(VECTOR_FIELD) |
| 129 | + .outputFields(Collections.singletonList(VECTOR_FIELD)) |
| 130 | + .topK(3) |
| 131 | + .build()); |
| 132 | + |
| 133 | + // The search() allows multiple target vectors to search in a batch. |
| 134 | + // Here we only input one vector to search, get the result of No.0 vector to check |
| 135 | + List<List<SearchResp.SearchResult>> searchResults = searchResp.getSearchResults(); |
| 136 | + List<SearchResp.SearchResult> results = searchResults.get(0); |
| 137 | + System.out.printf("\nThe result of No.%d vector %s:\n", k, Arrays.toString(targetVector.array())); |
| 138 | + for (SearchResp.SearchResult result : results) { |
| 139 | + System.out.printf("ID: %d, Score: %f, Vector: ", (long)result.getId(), result.getScore()); |
| 140 | + ByteBuffer vector = (ByteBuffer) result.getEntity().get(VECTOR_FIELD); |
| 141 | + System.out.print(Arrays.toString(vector.array())); |
| 142 | + System.out.println(); |
| 143 | + } |
| 144 | + |
| 145 | + SearchResp.SearchResult firstResult = results.get(0); |
| 146 | + if ((long)firstResult.getId() != k) { |
| 147 | + throw new RuntimeException(String.format("The top1 ID %d is not equal to target vector's ID %d", |
| 148 | + (long)firstResult.getId(), k)); |
| 149 | + } |
| 150 | + } |
| 151 | + System.out.println("Search result is correct"); |
| 152 | + |
| 153 | + // Retrieve some data |
| 154 | + int n = 99; |
| 155 | + QueryResp queryResp = client.query(QueryReq.builder() |
| 156 | + .collectionName(COLLECTION_NAME) |
| 157 | + .filter(String.format("id == %d", n)) |
| 158 | + .outputFields(Collections.singletonList(VECTOR_FIELD)) |
| 159 | + .build()); |
| 160 | + |
| 161 | + List<QueryResp.QueryResult> queryResults = queryResp.getQueryResults(); |
| 162 | + if (queryResults.isEmpty()) { |
| 163 | + throw new RuntimeException("The query result is empty"); |
| 164 | + } else { |
| 165 | + ByteBuffer vector = (ByteBuffer) queryResults.get(0).getEntity().get(VECTOR_FIELD); |
| 166 | + if (vector.compareTo(vectors.get(n)) != 0) { |
| 167 | + throw new RuntimeException("The query result is incorrect"); |
| 168 | + } |
| 169 | + } |
| 170 | + System.out.println("Query result is correct"); |
| 171 | + |
| 172 | + |
| 173 | + // Drop the collection if you don't need the collection anymore |
| 174 | + client.dropCollection(DropCollectionReq.builder() |
| 175 | + .collectionName(COLLECTION_NAME) |
| 176 | + .build()); |
| 177 | + |
| 178 | + client.close(); |
| 179 | + } |
| 180 | +} |
0 commit comments