@@ -99,65 +99,72 @@ print(res)
99
99
```
100
100
101
101
``` java
102
- import io.milvus.v2.client.ConnectConfig ;
102
+ import com.google.gson.Gson ;
103
+ import com.google.gson.JsonObject ;
103
104
import io.milvus.v2.client.MilvusClientV2 ;
105
+ import io.milvus.v2.client.ConnectConfig ;
104
106
import io.milvus.v2.common.DataType ;
105
107
import io.milvus.v2.common.IndexParam ;
106
- import io.milvus.v2.service.collection.request.AddFieldReq ;
107
- import io.milvus.v2.service.collection.request.CreateCollectionReq ;
108
- import io.milvus.v2.service.collection.request.GetLoadStateReq ;
108
+ import io.milvus.v2.service.collection.request.* ;
109
+ import io.milvus.v2.service.vector.request.* ;
110
+ import io.milvus.v2.service.vector.request.data.* ;
111
+ import io.milvus.v2.service.vector.response.* ;
112
+
113
+ import java.util.* ;
109
114
110
115
String CLUSTER_ENDPOINT = " http://localhost:19530" ;
111
116
112
117
// 1. Connect to Milvus server
113
118
ConnectConfig connectConfig = ConnectConfig . builder()
114
- .uri(CLUSTER_ENDPOINT )
115
- .build();
119
+ .uri(CLUSTER_ENDPOINT )
120
+ .build();
116
121
117
122
MilvusClientV2 client = new MilvusClientV2 (connectConfig);
118
123
119
124
// 2. Create a collection in customized setup mode
120
125
121
126
// 2.1 Create schema
122
127
CreateCollectionReq . CollectionSchema schema = client. createSchema();
128
+ schema. setEnableDynamicField(true );
123
129
124
130
// 2.2 Add fields to schema
125
131
schema. addField(AddFieldReq . builder(). fieldName(" id" ). dataType(DataType . Int64 ). isPrimaryKey(true ). autoID(false ). build());
126
132
schema. addField(AddFieldReq . builder(). fieldName(" vector" ). dataType(DataType . FloatVector ). dimension(5 ). build());
127
133
128
134
// 2.3 Prepare index parameters
129
135
IndexParam indexParamForIdField = IndexParam . builder()
130
- .fieldName(" id" )
131
- .indexType(IndexParam . IndexType . STL_SORT )
132
- .build();
136
+ .fieldName(" id" )
137
+ .indexType(IndexParam . IndexType . STL_SORT )
138
+ .build();
133
139
140
+ Map<String , Object > params = new HashMap<> ();
141
+ params. put(" nlist" , 1024 );
134
142
IndexParam indexParamForVectorField = IndexParam . builder()
135
- .fieldName(" vector" )
136
- .indexType(IndexParam . IndexType . IVF_FLAT )
137
- .metricType(IndexParam . MetricType . IP )
138
- .extraParams(Map . of( " nlist " , 1024 ) )
139
- .build();
143
+ .fieldName(" vector" )
144
+ .indexType(IndexParam . IndexType . IVF_FLAT )
145
+ .metricType(IndexParam . MetricType . IP )
146
+ .extraParams(params )
147
+ .build();
140
148
141
149
List<IndexParam > indexParams = new ArrayList<> ();
142
150
indexParams. add(indexParamForIdField);
143
151
indexParams. add(indexParamForVectorField);
144
152
145
153
// 2.4 Create a collection with schema and index parameters
146
154
CreateCollectionReq customizedSetupReq = CreateCollectionReq . builder()
147
- .collectionName(" customized_setup" )
148
- .collectionSchema(schema)
149
- .indexParams(indexParams)
150
- .enableDynamicField(true )
151
- .build();
155
+ .collectionName(" customized_setup" )
156
+ .collectionSchema(schema)
157
+ .indexParams(indexParams)
158
+ .build();
152
159
153
160
client. createCollection(customizedSetupReq);
154
161
155
162
Thread . sleep(5000 );
156
163
157
164
// 2.5 Get load state of the collection
158
165
GetLoadStateReq customSetupLoadStateReq1 = GetLoadStateReq . builder()
159
- .collectionName(" customized_setup" )
160
- .build();
166
+ .collectionName(" customized_setup" )
167
+ .build();
161
168
162
169
boolean res = client. getLoadState(customSetupLoadStateReq1);
163
170
@@ -264,22 +271,22 @@ print(data[0])
264
271
265
272
` ` ` java
266
273
List< String > colors = Arrays .asList (" green" , " blue" , " yellow" , " red" , " black" , " white" , " purple" , " pink" , " orange" , " brown" , " grey" );
267
- List< JSONObject> data = new ArrayList <> ();
268
-
274
+ List< JsonObject> data = new ArrayList <> ();
275
+ Gson gson = new Gson ();
276
+ Random rand = new Random ();
269
277
for (int i= 0 ; i< 1000 ; i++ ) {
270
- Random rand = new Random ();
271
278
String current_color = colors .get (rand .nextInt (colors .size ()- 1 ));
272
279
int current_tag = rand .nextInt (8999 ) + 1000 ;
273
- JSONObject row = new JSONObject ();
274
- row .put (" id" , Long . valueOf (i) );
275
- row .put (" vector" , Arrays .asList (rand .nextFloat (), rand .nextFloat (), rand .nextFloat (), rand .nextFloat (), rand .nextFloat ()));
276
- row .put (" color" , current_color);
277
- row .put (" tag" , current_tag);
278
- row .put (" color_tag" , current_color + " _" + String . valueOf (rand .nextInt (8999 ) + 1000 ));
280
+ JsonObject row = new JsonObject ();
281
+ row .addProperty (" id" , (long) i );
282
+ row .add (" vector" , gson . toJsonTree ( Arrays .asList (rand .nextFloat (), rand .nextFloat (), rand .nextFloat (), rand .nextFloat (), rand .nextFloat () )));
283
+ row .addProperty (" color" , current_color);
284
+ row .addProperty (" tag" , current_tag);
285
+ row .addProperty (" color_tag" , current_color + " _" + (rand .nextInt (8999 ) + 1000 ));
279
286
data .add (row);
280
287
}
281
288
282
- System .out .println (JSONObject . toJSON ( data .get (0 )));
289
+ System .out .println (data .get (0 ). toString ( ));
283
290
` ` `
284
291
285
292
` ` ` javascript
@@ -380,16 +387,16 @@ time.sleep(5)
380
387
` ` ` java
381
388
// 3.1 Insert data into the collection
382
389
InsertReq insertReq = InsertReq .builder ()
383
- .collectionName (" customized_setup" )
384
- .data (data)
385
- .build ();
390
+ .collectionName (" customized_setup" )
391
+ .data (data)
392
+ .build ();
386
393
387
394
InsertResp insertResp = client .insert (insertReq);
388
395
389
- System .out .println (JSONObject . toJSON (insertResp ));
396
+ System .out .println (insertResp . getInsertCnt ( ));
390
397
391
398
// Output:
392
- // {"insertCnt": 1000}
399
+ // 1000
393
400
394
401
Thread .sleep (5000 );
395
402
` ` `
@@ -486,47 +493,30 @@ print(res)
486
493
487
494
` ` ` java
488
495
// 4. Search with non-schema-defined fields
489
- List< List < Float >> queryVectors = Arrays . asList ( Arrays . asList ( 0 .3580376395471989f , - 0 .6023495712049978f , 0 .18414012509913835f , - 0 .26286205330961354f , 0 .9029438446296592f ));
496
+ List< BaseVector > queryVectors = Collections . singletonList ( new FloatVec ( new float []{ 0 .3580376395471989f , - 0 .6023495712049978f , 0 .18414012509913835f , - 0 .26286205330961354f , 0 .9029438446296592f } ));
490
497
491
498
SearchReq searchReq = SearchReq .builder ()
492
- .collectionName (" customized_setup" )
493
- .data (queryVectors)
494
- .filter (" $meta[\" color\" ] in [\" red\" , \" green\" ]" )
495
- .outputFields (List . of (" id" , " color_tag" ))
496
- .topK (3 )
497
- .build ();
499
+ .collectionName (" customized_setup" )
500
+ .data (queryVectors)
501
+ .filter (" $meta[\" color\" ] in [\" red\" , \" green\" ]" )
502
+ .outputFields (Arrays . asList (" id" , " color_tag" ))
503
+ .topK (3 )
504
+ .build ();
498
505
499
506
SearchResp searchResp = client .search (searchReq);
500
507
501
- System .out .println (JSONObject .toJSON (searchResp));
508
+ List< List< SearchResp .SearchResult >> searchResults = searchResp .getSearchResults ();
509
+ for (List< SearchResp .SearchResult > results : searchResults) {
510
+ System .out .println (" TopK results:" );
511
+ for (SearchResp .SearchResult result : results) {
512
+ System .out .println (result);
513
+ }
514
+ }
502
515
503
516
// Output:
504
- // {"searchResults": [[
505
- // {
506
- // "distance": 1.3159835,
507
- // "id": 979,
508
- // "entity": {
509
- // "color_tag": "red_7155",
510
- // "id": 979
511
- // }
512
- // },
513
- // {
514
- // "distance": 1.0744804,
515
- // "id": 44,
516
- // "entity": {
517
- // "color_tag": "green_8006",
518
- // "id": 44
519
- // }
520
- // },
521
- // {
522
- // "distance": 1.0060014,
523
- // "id": 617,
524
- // "entity": {
525
- // "color_tag": "red_4056",
526
- // "id": 617
527
- // }
528
- // }
529
- // ]]}
517
+ // SearchResp.SearchResult(entity={color_tag=green_2205, id=556}, score=1.134007, id=556)
518
+ // SearchResp.SearchResult(entity={color_tag=red_2786, id=310}, score=0.9072295, id=310)
519
+ // SearchResp.SearchResult(entity={color_tag=red_9493, id=215}, score=0.8819287, id=215)
530
520
` ` `
531
521
532
522
` ` ` javascript
0 commit comments