@@ -172,6 +172,65 @@ private static ByteString convertPlaceholder(List<Object> data, PlaceholderType
172172 }
173173 }
174174
175+ private static void convertSearchTarget (SearchReq request , SearchRequest .Builder builder ) {
176+ // prepare target, the input could be:
177+ // 1. vectors or string list for doc-in-doc-out
178+ // 2. ids list for search by primary keys
179+ List <BaseVector > vectors = request .getData ();
180+ List <Object > ids = request .getIds ();
181+ boolean vectorsIsEmpty = (vectors == null || vectors .isEmpty ());
182+ boolean idsIsEmpty = (ids == null || ids .isEmpty ());
183+ if (vectorsIsEmpty && idsIsEmpty ) {
184+ throw new MilvusClientException (ErrorCode .INVALID_PARAMS , "Require either ids or vectors, but both are empty" );
185+ }
186+ if (!vectorsIsEmpty && !idsIsEmpty ) {
187+ throw new MilvusClientException (ErrorCode .INVALID_PARAMS , "Require either ids or vectors, but both are provided" );
188+ }
189+
190+ if (!vectorsIsEmpty ) {
191+ // the elements must be all-vector or all-string
192+ PlaceholderType plType = vectors .get (0 ).getPlaceholderType ();
193+ List <Object > data = new ArrayList <>();
194+ for (BaseVector vector : vectors ) {
195+ if (vector .getPlaceholderType () != plType ) {
196+ throw new MilvusClientException (ErrorCode .INVALID_PARAMS ,
197+ "Different types of target vectors in a search request is not allowed." );
198+ }
199+ data .add (vector .getData ());
200+ }
201+
202+ ByteString byteStr = convertPlaceholder (data , plType );
203+ builder .setPlaceholderGroup (byteStr );
204+ builder .setNq (vectors .size ());
205+ } else {
206+ Object val = ids .get (0 );
207+ if (val instanceof String ) {
208+ StringArray .Builder strBuilder = StringArray .newBuilder ();
209+ for (Object obj : ids ) {
210+ if (!(obj instanceof String )) {
211+ throw new MilvusClientException (ErrorCode .INVALID_PARAMS ,
212+ "All IDs must be of type String if the first ID is a String." );
213+ }
214+ strBuilder .addData ((String ) obj );
215+ }
216+ builder .setIds (IDs .newBuilder ().setStrId (strBuilder .build ()).build ());
217+ } else if (val instanceof Long ) {
218+ LongArray .Builder longBuilder = LongArray .newBuilder ();
219+ for (Object obj : ids ) {
220+ if (!(obj instanceof Long )) {
221+ throw new MilvusClientException (ErrorCode .INVALID_PARAMS ,
222+ "All IDs must be of type Long if the first ID is a Long." );
223+ }
224+ longBuilder .addData ((Long ) obj );
225+ }
226+ builder .setIds (IDs .newBuilder ().setIntId (longBuilder .build ()).build ());
227+ } else {
228+ throw new MilvusClientException (ErrorCode .INVALID_PARAMS , "ID type must be String or Long." );
229+ }
230+ builder .setNq (ids .size ());
231+ }
232+ }
233+
175234 public SearchRequest ConvertToGrpcSearchRequest (SearchReq request ) {
176235 String dbName = request .getDatabaseName ();
177236 String collectionName = request .getCollectionName ();
@@ -185,26 +244,8 @@ public SearchRequest ConvertToGrpcSearchRequest(SearchReq request) {
185244 builder .setDbName (dbName );
186245 }
187246
188- // prepare target, the input could be vectors or string list for doc-in-doc-out
189- List <BaseVector > vectors = request .getData ();
190- if (vectors == null || vectors .isEmpty ()) {
191- throw new MilvusClientException (ErrorCode .INVALID_PARAMS , "Target data list of search request is empty." );
192- }
193-
194- // the elements must be all-vector or all-string
195- PlaceholderType plType = vectors .get (0 ).getPlaceholderType ();
196- List <Object > data = new ArrayList <>();
197- for (BaseVector vector : vectors ) {
198- if (vector .getPlaceholderType () != plType ) {
199- throw new MilvusClientException (ErrorCode .INVALID_PARAMS ,
200- "Different types of target vectors in a search request is not allowed." );
201- }
202- data .add (vector .getData ());
203- }
204-
205- ByteString byteStr = convertPlaceholder (data , plType );
206- builder .setPlaceholderGroup (byteStr );
207- builder .setNq (vectors .size ());
247+ // target vectors or ids
248+ convertSearchTarget (request , builder );
208249
209250 // search parameters
210251 // tries to fit the compatibility between v2.5.1 and older versions
0 commit comments