2626import io .milvus .common .clientenum .ConsistencyLevelEnum ;
2727import io .milvus .grpc .DataType ;
2828import io .milvus .grpc .QueryResults ;
29+ import io .milvus .grpc .SearchResults ;
2930import io .milvus .param .*;
3031import io .milvus .param .collection .*;
3132import io .milvus .param .dml .InsertParam ;
3233import io .milvus .param .dml .QueryParam ;
34+ import io .milvus .param .dml .SearchParam ;
3335import io .milvus .param .index .CreateIndexParam ;
3436import io .milvus .response .QueryResultsWrapper ;
37+ import io .milvus .response .SearchResultsWrapper ;
3538
39+ import java .util .ArrayList ;
3640import java .util .Arrays ;
3741import java .util .Collections ;
3842import java .util .List ;
3943
4044public class JsonFieldExample {
4145 private static final String COLLECTION_NAME = "java_sdk_example_json_v1" ;
42- private static final String ID_FIELD = "id " ;
46+ private static final String ID_FIELD = "key " ;
4347 private static final String VECTOR_FIELD = "vector" ;
4448 private static final String JSON_FIELD = "metadata" ;
4549 private static final Integer VECTOR_DIM = 128 ;
4650
4751 private static void queryWithExpr (MilvusClient client , String expr ) {
52+ System .out .printf ("%n=============================Query with expr: '%s'================================%n" , expr );
4853 R <QueryResults > queryRet = client .query (QueryParam .newBuilder ()
4954 .withCollectionName (COLLECTION_NAME )
5055 .withExpr (expr )
@@ -56,7 +61,6 @@ private static void queryWithExpr(MilvusClient client, String expr) {
5661 for (QueryResultsWrapper .RowRecord record : records ) {
5762 System .out .println (record );
5863 }
59- System .out .println ("=============================================================" );
6064 }
6165
6266 public static void main (String [] args ) {
@@ -123,22 +127,28 @@ public static void main(String[] args) {
123127 System .out .println ("Collection created" );
124128
125129 // insert rows
130+ List <List <Float >> vectors = new ArrayList <>();
131+ List <JsonObject > metadatas = new ArrayList <>();
126132 Gson gson = new Gson ();
127133 for (int i = 0 ; i < 100 ; i ++) {
128134 JsonObject row = new JsonObject ();
129135 row .addProperty (ID_FIELD , i );
130- row .add (VECTOR_FIELD , gson .toJsonTree (CommonUtils .generateFloatVector (VECTOR_DIM )));
136+ List <Float > vector = CommonUtils .generateFloatVector (VECTOR_DIM );
137+ row .add (VECTOR_FIELD , gson .toJsonTree (vector ));
138+ vectors .add (vector );
131139
132140 // Note: for JSON field, always construct a real JsonObject
133141 // don't use row.addProperty(JSON_FIELD, strContent) since the value is treated as a string, not a JsonObject
134142 JsonObject metadata = new JsonObject ();
135- metadata .addProperty ("path" , String .format ("\\ root/abc/path %d" , i ));
143+ metadata .addProperty ("path" , String .format ("\\ root/abc/path_ %d" , i ));
136144 metadata .addProperty ("size" , i );
137145 if (i %7 == 0 ) {
138146 metadata .addProperty ("special" , true );
139147 }
148+
140149 metadata .add ("flags" , gson .toJsonTree (Arrays .asList (i , i + 1 , i + 2 )));
141150 row .add (JSON_FIELD , metadata );
151+ metadatas .add (metadata );
142152// System.out.println(metadata);
143153
144154 // dynamic fields
@@ -165,6 +175,65 @@ public static void main(String[] args) {
165175 long rowCount = (long )queryWrapper .getFieldWrapper ("count(*)" ).getFieldData ().get (0 );
166176 System .out .printf ("%d rows persisted\n " , rowCount );
167177
178+ // search and output JSON field
179+ List <List <Float >> searchVectors = new ArrayList <>();
180+ List <JsonObject > expectedMetadatas = new ArrayList <>();
181+ for (int i = 0 ; i < 10 ; i ++) {
182+ List <Float > targetVector = vectors .get (i );
183+ searchVectors .add (targetVector );
184+ expectedMetadatas .add (metadatas .get (i ));
185+ }
186+ R <SearchResults > searchRet = client .search (SearchParam .newBuilder ()
187+ .withCollectionName (COLLECTION_NAME )
188+ .withLimit (3L )
189+ .withFloatVectors (searchVectors )
190+ .withVectorFieldName (VECTOR_FIELD )
191+ .addOutField (ID_FIELD )
192+ .addOutField (VECTOR_FIELD )
193+ .addOutField (JSON_FIELD )
194+ .build ());
195+ CommonUtils .handleResponseStatus (searchRet );
196+
197+ SearchResultsWrapper resultsWrapper = new SearchResultsWrapper (searchRet .getData ().getResults ());
198+ System .out .println ("\n =============================Search result with IDScore================================" );
199+ for (int i = 0 ; i < 10 ; i ++) {
200+ List <SearchResultsWrapper .IDScore > scores = resultsWrapper .getIDScore (i );
201+ System .out .printf ("\n The result of No.%d target vector:\n " , i );
202+ for (SearchResultsWrapper .IDScore score : scores ) {
203+ System .out .println (score );
204+ }
205+ long pk = scores .get (0 ).getLongID ();
206+ if (pk != i ) {
207+ throw new RuntimeException (String .format ("The top1 ID %d is not equal to target vector's ID %d" , pk , i ));
208+ }
209+ JsonObject metadata = (JsonObject ) scores .get (0 ).get (JSON_FIELD );
210+ if (!metadata .equals (expectedMetadatas .get (i ))) {
211+ throw new RuntimeException (String .format ("The top1 metadata %s is not equal to target metadata %s" ,
212+ metadata , expectedMetadatas .get (i )));
213+ }
214+ List <Float > vector = (List <Float >) scores .get (0 ).get (VECTOR_FIELD );
215+ CommonUtils .compareFloatVectors (vector , searchVectors .get (i ));
216+ }
217+ System .out .println ("\n =============================Search result with RowRecord================================" );
218+ for (int i = 0 ; i < 10 ; i ++) {
219+ List <QueryResultsWrapper .RowRecord > records = resultsWrapper .getRowRecords (i );
220+ System .out .printf ("\n The result of No.%d target vector:\n " , i );
221+ for (QueryResultsWrapper .RowRecord record : records ) {
222+ System .out .println (record );
223+ }
224+ long pk = (long )records .get (0 ).get (ID_FIELD );
225+ if (pk != i ) {
226+ throw new RuntimeException (String .format ("The top1 ID %d is not equal to target vector's ID %d" , pk , i ));
227+ }
228+ JsonObject metadata = (JsonObject ) records .get (0 ).get (JSON_FIELD );
229+ if (!metadata .equals (expectedMetadatas .get (i ))) {
230+ throw new RuntimeException (String .format ("The top1 metadata %s is not equal to target metadata %s" ,
231+ metadata , expectedMetadatas .get (i )));
232+ }
233+ List <Float > vector = (List <Float >) records .get (0 ).get (VECTOR_FIELD );
234+ CommonUtils .compareFloatVectors (vector , searchVectors .get (i ));
235+ }
236+
168237 // query by filtering JSON
169238 queryWithExpr (client , "exists metadata[\" special\" ]" );
170239 queryWithExpr (client , "metadata[\" size\" ] < 5" );
0 commit comments