Przeglądaj źródła

optim queryIterator&SearchIterator (#1077)

Signed-off-by: lentitude2tk <xushuang.hu@zilliz.com>
xushuang.hu 7 miesięcy temu
rodzic
commit
9038c634cb

+ 4 - 1
src/main/java/io/milvus/orm/iterator/QueryIterator.java

@@ -97,7 +97,8 @@ public class QueryIterator {
         }
 
         List<QueryResultsWrapper.RowRecord> res = getQueryResultsWrapper(expr, 0L, offset);
-        updateCursor(res.subList(0, (int) offset));
+        int resultIndex = Math.min(res.size(), (int) offset);
+        updateCursor(res.subList(0, resultIndex));
         offset = 0;
     }
 
@@ -184,6 +185,8 @@ public class QueryIterator {
                 .withOffset(offset)
                 .withLimit(limit)
                 .withIgnoreGrowing(queryIteratorParam.isIgnoreGrowing())
+                .withReduceStopForBest(queryIteratorParam.isReduceStopForBest())
+                .withIterator(Boolean.TRUE)
                 .build();
 
         QueryRequest queryRequest = ParamUtils.convertQueryParam(queryParam);

+ 3 - 1
src/main/java/io/milvus/orm/iterator/SearchIterator.java

@@ -248,7 +248,9 @@ public class SearchIterator {
                 .withRoundDecimal(searchIteratorParam.getRoundDecimal())
                 .withParams(JacksonUtils.toJsonString(params))
                 .withMetricType(MetricType.valueOf(searchIteratorParam.getMetricType()))
-                .withIgnoreGrowing(searchIteratorParam.isIgnoreGrowing());
+                .withIgnoreGrowing(searchIteratorParam.isIgnoreGrowing())
+                .withIterator(Boolean.TRUE)
+                ;
 
         if (!StringUtils.isNullOrEmpty(searchIteratorParam.getGroupByFieldName())) {
             searchParamBuilder.withGroupByFieldName(searchIteratorParam.getGroupByFieldName());

+ 2 - 0
src/main/java/io/milvus/param/Constant.java

@@ -31,6 +31,8 @@ public class Constant {
     public static final String ARRAY_MAX_CAPACITY = "max_capacity";
     public static final String TOP_K = "topk";
     public static final String IGNORE_GROWING = "ignore_growing";
+    public static final String REDUCE_STOP_FOR_BEST = "reduce_stop_for_best";
+    public static final String ITERATOR_FIELD = "iterator";
     public static final String GROUP_BY_FIELD = "group_by_field";
     public static final String INDEX_TYPE = "index_type";
     public static final String METRIC_TYPE = "metric_type";

+ 17 - 0
src/main/java/io/milvus/param/ParamUtils.java

@@ -716,6 +716,11 @@ public class ParamUtils {
                         KeyValuePair.newBuilder()
                                 .setKey(Constant.IGNORE_GROWING)
                                 .setValue(String.valueOf(requestParam.isIgnoreGrowing()))
+                                .build())
+                .addSearchParams(
+                        KeyValuePair.newBuilder()
+                                .setKey(Constant.ITERATOR_FIELD)
+                                .setValue(String.valueOf(requestParam.isIterator()))
                                 .build());
 
         if (!Objects.equals(requestParam.getMetricType(), MetricType.None.name())) {
@@ -915,6 +920,18 @@ public class ParamUtils {
                 .setValue(String.valueOf(requestParam.isIgnoreGrowing()))
                 .build());
 
+        // reduce stop for best
+        builder.addQueryParams(KeyValuePair.newBuilder()
+                        .setKey(Constant.REDUCE_STOP_FOR_BEST)
+                        .setValue(String.valueOf(requestParam.isReduceStopForBest()))
+                .build());
+
+        // iterator
+        builder.addQueryParams(KeyValuePair.newBuilder()
+                        .setKey(Constant.ITERATOR_FIELD)
+                        .setValue(String.valueOf(requestParam.isIterator()))
+                .build());
+
         return builder.build();
     }
 

+ 14 - 0
src/main/java/io/milvus/param/dml/QueryIteratorParam.java

@@ -53,6 +53,7 @@ public class QueryIteratorParam {
     private final long offset;
     private final long limit;
     private final boolean ignoreGrowing;
+    private final boolean reduceStopForBest;
 
     private final long batchSize;
 
@@ -69,6 +70,7 @@ public class QueryIteratorParam {
         this.offset = builder.offset;
         this.limit = builder.limit;
         this.ignoreGrowing = builder.ignoreGrowing;
+        this.reduceStopForBest = builder.reduceStopForBest;
 
         this.batchSize = builder.batchSize;
     }
@@ -94,6 +96,7 @@ public class QueryIteratorParam {
         private Long limit = (long) UNLIMITED;
         private Boolean ignoreGrowing = Boolean.FALSE;
         private Long batchSize = 1000L;
+        private Boolean reduceStopForBest = Boolean.TRUE;
 
         private Builder() {
         }
@@ -239,6 +242,17 @@ public class QueryIteratorParam {
             return this;
         }
 
+        /**
+         * Adjust the query using iterators to handle offsets more efficiently during the Reduce step. Default is True.
+         *
+         * @param reduceStopForBest <code>Boolean.TRUE</code> ignore, Boolean.FALSE is not
+         * @return <code>Builder</code>
+         */
+        public Builder withReduceStopForBest(@NonNull Boolean reduceStopForBest) {
+            this.reduceStopForBest = reduceStopForBest;
+            return this;
+        }
+
         /**
          * Verifies parameters and creates a new {@link QueryIteratorParam} instance.
          *

+ 28 - 0
src/main/java/io/milvus/param/dml/QueryParam.java

@@ -49,6 +49,8 @@ public class QueryParam {
     private final long offset;
     private final long limit;
     private final boolean ignoreGrowing;
+    private final boolean reduceStopForBest;
+    private final boolean iterator;
 
     private QueryParam(@NonNull Builder builder) {
         this.databaseName = builder.databaseName;
@@ -63,6 +65,8 @@ public class QueryParam {
         this.offset = builder.offset;
         this.limit = builder.limit;
         this.ignoreGrowing = builder.ignoreGrowing;
+        this.reduceStopForBest = builder.reduceStopForBest;
+        this.iterator = builder.iterator;
     }
 
     public static Builder newBuilder() {
@@ -85,6 +89,8 @@ public class QueryParam {
         private Long offset = 0L;
         private Long limit = 0L;
         private Boolean ignoreGrowing = Boolean.FALSE;
+        private Boolean reduceStopForBest = Boolean.FALSE;
+        private Boolean iterator = Boolean.FALSE;
 
         private Builder() {
         }
@@ -218,6 +224,28 @@ public class QueryParam {
             return this;
         }
 
+        /**
+         * Adjust the query using iterators to handle offsets more efficiently during the Reduce step. Default is False.
+         *
+         * @param reduceStopForBest <code>Boolean.TRUE</code> ignore, Boolean.FALSE is not
+         * @return <code>Builder</code>
+         */
+        public Builder withReduceStopForBest(@NonNull Boolean reduceStopForBest) {
+            this.reduceStopForBest = reduceStopForBest;
+            return this;
+        }
+
+        /**
+         * Optimizing specifically for iterators can yield correct data results. Default is False.
+         *
+         * @param iterator <code>Boolean.TRUE</code> ignore, Boolean.FALSE is not
+         * @return <code>Builder</code>
+         */
+        public Builder withIterator(@NonNull Boolean iterator) {
+            this.iterator = iterator;
+            return this;
+        }
+
         /**
          * Verifies parameters and creates a new {@link QueryParam} instance.
          *

+ 14 - 2
src/main/java/io/milvus/param/dml/SearchParam.java

@@ -26,10 +26,8 @@ import io.milvus.grpc.PlaceholderType;
 import io.milvus.param.Constant;
 import io.milvus.param.MetricType;
 import io.milvus.param.ParamUtils;
-
 import lombok.Getter;
 import lombok.NonNull;
-import lombok.ToString;
 
 import java.nio.ByteBuffer;
 import java.util.List;
@@ -59,6 +57,7 @@ public class SearchParam {
     private final boolean ignoreGrowing;
     private final String groupByFieldName;
     private final PlaceholderType plType;
+    private final boolean iterator;
 
     private SearchParam(@NonNull Builder builder) {
         this.databaseName = builder.databaseName;
@@ -80,6 +79,7 @@ public class SearchParam {
         this.ignoreGrowing = builder.ignoreGrowing;
         this.groupByFieldName = builder.groupByFieldName;
         this.plType = builder.plType;
+        this.iterator = builder.iterator;
     }
 
     public static Builder newBuilder() {
@@ -108,6 +108,7 @@ public class SearchParam {
         private ConsistencyLevelEnum consistencyLevel = null;
         private Boolean ignoreGrowing = Boolean.FALSE;
         private String groupByFieldName;
+        private Boolean iterator = Boolean.FALSE;
 
         // plType is used to distinct vector type
         // for Float16Vector/BFloat16Vector and BinaryVector, user inputs ByteBuffer
@@ -376,6 +377,17 @@ public class SearchParam {
             return this;
         }
 
+        /**
+         * Optimizing specifically for iterators can yield correct data results. Default is False.
+         *
+         * @param iterator <code>Boolean.TRUE</code> ignore, Boolean.FALSE is not
+         * @return <code>Builder</code>
+         */
+        public Builder withIterator(@NonNull Boolean iterator) {
+            this.iterator = iterator;
+            return this;
+        }
+
         /**
          * Verifies parameters and creates a new {@link SearchParam} instance.
          *