Skip to content

Commit 374125e

Browse files
Milvus-doc-botMilvus-doc-bot
Milvus-doc-bot
authored and
Milvus-doc-bot
committed
Release new docs
1 parent 2573919 commit 374125e

File tree

4 files changed

+250
-572
lines changed

4 files changed

+250
-572
lines changed

v2.4.x/site/en/userGuide/enable-dynamic-field.md

+60-70
Original file line numberDiff line numberDiff line change
@@ -99,65 +99,72 @@ print(res)
9999
```
100100

101101
```java
102-
import io.milvus.v2.client.ConnectConfig;
102+
import com.google.gson.Gson;
103+
import com.google.gson.JsonObject;
103104
import io.milvus.v2.client.MilvusClientV2;
105+
import io.milvus.v2.client.ConnectConfig;
104106
import io.milvus.v2.common.DataType;
105107
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.*;
109114

110115
String CLUSTER_ENDPOINT = "http://localhost:19530";
111116

112117
// 1. Connect to Milvus server
113118
ConnectConfig connectConfig = ConnectConfig.builder()
114-
.uri(CLUSTER_ENDPOINT)
115-
.build();
119+
.uri(CLUSTER_ENDPOINT)
120+
.build();
116121

117122
MilvusClientV2 client = new MilvusClientV2(connectConfig);
118123

119124
// 2. Create a collection in customized setup mode
120125

121126
// 2.1 Create schema
122127
CreateCollectionReq.CollectionSchema schema = client.createSchema();
128+
schema.setEnableDynamicField(true);
123129

124130
// 2.2 Add fields to schema
125131
schema.addField(AddFieldReq.builder().fieldName("id").dataType(DataType.Int64).isPrimaryKey(true).autoID(false).build());
126132
schema.addField(AddFieldReq.builder().fieldName("vector").dataType(DataType.FloatVector).dimension(5).build());
127133

128134
// 2.3 Prepare index parameters
129135
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();
133139

140+
Map<String, Object> params = new HashMap<>();
141+
params.put("nlist", 1024);
134142
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();
140148

141149
List<IndexParam> indexParams = new ArrayList<>();
142150
indexParams.add(indexParamForIdField);
143151
indexParams.add(indexParamForVectorField);
144152

145153
// 2.4 Create a collection with schema and index parameters
146154
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();
152159

153160
client.createCollection(customizedSetupReq);
154161

155162
Thread.sleep(5000);
156163

157164
// 2.5 Get load state of the collection
158165
GetLoadStateReq customSetupLoadStateReq1 = GetLoadStateReq.builder()
159-
.collectionName("customized_setup")
160-
.build();
166+
.collectionName("customized_setup")
167+
.build();
161168

162169
boolean res = client.getLoadState(customSetupLoadStateReq1);
163170

@@ -264,22 +271,22 @@ print(data[0])
264271
265272
```java
266273
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();
269277
for (int i=0; i<1000; i++) {
270-
Random rand = new Random();
271278
String current_color = colors.get(rand.nextInt(colors.size()-1));
272279
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));
279286
data.add(row);
280287
}
281288

282-
System.out.println(JSONObject.toJSON(data.get(0)));
289+
System.out.println(data.get(0).toString());
283290
```
284291
285292
```javascript
@@ -380,16 +387,16 @@ time.sleep(5)
380387
```java
381388
// 3.1 Insert data into the collection
382389
InsertReq insertReq = InsertReq.builder()
383-
.collectionName("customized_setup")
384-
.data(data)
385-
.build();
390+
.collectionName("customized_setup")
391+
.data(data)
392+
.build();
386393

387394
InsertResp insertResp = client.insert(insertReq);
388395

389-
System.out.println(JSONObject.toJSON(insertResp));
396+
System.out.println(insertResp.getInsertCnt());
390397

391398
// Output:
392-
// {"insertCnt": 1000}
399+
// 1000
393400

394401
Thread.sleep(5000);
395402
```
@@ -486,47 +493,30 @@ print(res)
486493
487494
```java
488495
// 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}));
490497

491498
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();
498505

499506
SearchResp searchResp = client.search(searchReq);
500507

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+
}
502515

503516
// 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)
530520
```
531521
532522
```javascript

0 commit comments

Comments
 (0)