-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathSimpleExample.java
More file actions
139 lines (124 loc) · 5.97 KB
/
SimpleExample.java
File metadata and controls
139 lines (124 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.milvus.v2;
import com.google.gson.*;
import io.milvus.v2.client.*;
import io.milvus.v2.common.ConsistencyLevel;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.DropCollectionReq;
import io.milvus.v2.service.vector.request.*;
import io.milvus.v2.service.vector.request.data.FloatVec;
import io.milvus.v2.service.vector.response.*;
import java.util.*;
public class SimpleExample {
public static void main(String[] args) {
ConnectConfig config = ConnectConfig.builder()
.uri("http://localhost:19530")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
String collectionName = "java_sdk_example_simple_v2";
// Drop collection if exists
client.dropCollection(DropCollectionReq.builder()
.collectionName(collectionName)
.build());
// Quickly create a collection with "id" field and "vector" field
client.createCollection(CreateCollectionReq.builder()
.collectionName(collectionName)
.dimension(4)
.build());
System.out.printf("Collection '%s' created\n", collectionName);
// Insert some data
List<JsonObject> rows = new ArrayList<>();
Gson gson = new Gson();
for (int i = 0; i < 100; i++) {
JsonObject row = new JsonObject();
row.addProperty("id", i);
row.add("vector", gson.toJsonTree(new float[]{i, (float) i /2, (float) i /3, (float) i /4}));
row.addProperty(String.format("dynamic_%d", i), "this is dynamic value"); // this value is stored in dynamic field
rows.add(row);
}
InsertResp insertR = client.insert(InsertReq.builder()
.collectionName(collectionName)
.data(rows)
.build());
System.out.printf("%d rows inserted\n", insertR.getInsertCnt());
// Get row count, set ConsistencyLevel.STRONG to sync the data to query node so that data is visible
QueryResp countR = client.query(QueryReq.builder()
.collectionName(collectionName)
.filter("")
.outputFields(Collections.singletonList("count(*)"))
.consistencyLevel(ConsistencyLevel.STRONG)
.build());
System.out.printf("%d rows persisted\n", (long)countR.getQueryResults().get(0).getEntity().get("count(*)"));
// Retrieve
List<Object> ids = Arrays.asList(1L, 50L);
GetResp getR = client.get(GetReq.builder()
.collectionName(collectionName)
.ids(ids)
.outputFields(Collections.singletonList("*"))
.build());
System.out.println("\nRetrieve results:");
for (QueryResp.QueryResult result : getR.getGetResults()) {
System.out.println(result.getEntity());
}
// Search
SearchResp searchR = client.search(SearchReq.builder()
.collectionName(collectionName)
.data(Collections.singletonList(new FloatVec(new float[]{1.0f, 1.0f, 1.0f, 1.0f})))
.filter("id < 100")
.topK(10)
.outputFields(Collections.singletonList("*"))
.build());
List<List<SearchResp.SearchResult>> searchResults = searchR.getSearchResults();
System.out.println("\nSearch results:");
for (List<SearchResp.SearchResult> results : searchResults) {
for (SearchResp.SearchResult result : results) {
System.out.printf("ID: %d, Score: %f, %s\n", (long)result.getId(), result.getScore(), result.getEntity().toString());
}
}
// search with template expression
Map<String, Map<String, Object>> expressionTemplateValues = new HashMap<>();
Map<String, Object> params = new HashMap<>();
params.put("max", 10);
expressionTemplateValues.put("id < {max}", params);
List<Object> list = Arrays.asList(1, 2, 3);
Map<String, Object> params2 = new HashMap<>();
params2.put("list", list);
expressionTemplateValues.put("id in {list}", params2);
expressionTemplateValues.forEach((key, value) -> {
SearchReq request = SearchReq.builder()
.collectionName(collectionName)
.data(Collections.singletonList(new FloatVec(new float[]{1.0f, 1.0f, 1.0f, 1.0f})))
.topK(10)
.filter(key)
.filterTemplateValues(value)
.outputFields(Collections.singletonList("*"))
.build();
SearchResp statusR = client.search(request);
List<List<SearchResp.SearchResult>> searchResults2 = statusR.getSearchResults();
System.out.println("\nSearch with template results:");
for (List<SearchResp.SearchResult> results : searchResults2) {
for (SearchResp.SearchResult result : results) {
System.out.printf("ID: %d, Score: %f, %s\n", (long)result.getId(), result.getScore(), result.getEntity().toString());
}
}
});
client.close();
}
}