-
Notifications
You must be signed in to change notification settings - Fork 128
/
KNNQueryBuilder.java
680 lines (615 loc) · 27.6 KB
/
KNNQueryBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.knn.index.query;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.opensearch.common.ValidationException;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.query.AbstractQueryBuilder;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryRewriteContext;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.knn.index.engine.KNNMethodConfigContext;
import org.opensearch.knn.index.engine.model.QueryContext;
import org.opensearch.knn.index.engine.qframe.QuantizationConfig;
import org.opensearch.knn.index.mapper.KNNMappingConfig;
import org.opensearch.knn.index.mapper.KNNVectorFieldType;
import org.opensearch.knn.index.query.parser.RescoreParser;
import org.opensearch.knn.index.query.rescore.RescoreContext;
import org.opensearch.knn.index.util.IndexUtil;
import org.opensearch.knn.index.engine.MethodComponentContext;
import org.opensearch.knn.index.SpaceType;
import org.opensearch.knn.index.VectorDataType;
import org.opensearch.knn.index.VectorQueryType;
import org.opensearch.knn.index.query.parser.KNNQueryBuilderParser;
import org.opensearch.knn.index.engine.KNNLibrarySearchContext;
import org.opensearch.knn.index.engine.KNNEngine;
import org.opensearch.knn.indices.ModelDao;
import org.opensearch.knn.indices.ModelMetadata;
import org.opensearch.knn.indices.ModelUtil;
import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import static org.opensearch.knn.common.KNNConstants.EXPAND_NESTED;
import static org.opensearch.knn.common.KNNConstants.MAX_DISTANCE;
import static org.opensearch.knn.common.KNNConstants.METHOD_PARAMETER;
import static org.opensearch.knn.common.KNNConstants.METHOD_PARAMETER_EF_SEARCH;
import static org.opensearch.knn.common.KNNConstants.METHOD_PARAMETER_NPROBES;
import static org.opensearch.knn.common.KNNConstants.MIN_SCORE;
import static org.opensearch.knn.common.KNNValidationUtil.validateByteVectorValue;
import static org.opensearch.knn.index.query.parser.MethodParametersParser.validateMethodParameters;
import static org.opensearch.knn.index.engine.KNNEngine.ENGINES_SUPPORTING_RADIAL_SEARCH;
import static org.opensearch.knn.index.engine.validation.ParameterValidator.validateParameters;
import static org.opensearch.knn.index.query.parser.RescoreParser.RESCORE_OVERSAMPLE_PARAMETER;
import static org.opensearch.knn.index.query.parser.RescoreParser.RESCORE_PARAMETER;
/**
* Helper class to build the KNN query
*/
// The builder validates the member variables so access to the constructor is prohibited to not accidentally bypass validations
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Log4j2
public class KNNQueryBuilder extends AbstractQueryBuilder<KNNQueryBuilder> {
private static ModelDao modelDao;
public static final ParseField VECTOR_FIELD = new ParseField("vector");
public static final ParseField K_FIELD = new ParseField("k");
public static final ParseField FILTER_FIELD = new ParseField("filter");
public static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped");
public static final ParseField EXPAND_NESTED_FIELD = new ParseField(EXPAND_NESTED);
public static final ParseField MAX_DISTANCE_FIELD = new ParseField(MAX_DISTANCE);
public static final ParseField MIN_SCORE_FIELD = new ParseField(MIN_SCORE);
public static final ParseField EF_SEARCH_FIELD = new ParseField(METHOD_PARAMETER_EF_SEARCH);
public static final ParseField NPROBE_FIELD = new ParseField(METHOD_PARAMETER_NPROBES);
public static final ParseField METHOD_PARAMS_FIELD = new ParseField(METHOD_PARAMETER);
public static final ParseField RESCORE_FIELD = new ParseField(RESCORE_PARAMETER);
public static final ParseField RESCORE_OVERSAMPLE_FIELD = new ParseField(RESCORE_OVERSAMPLE_PARAMETER);
public static final int K_MAX = 10000;
/**
* The name for the knn query
*/
public static final String NAME = "knn";
/**
* The default mode terms are combined in a match query
*/
private final String fieldName;
private final float[] vector;
@Getter
private int k;
@Getter
private Float maxDistance;
@Getter
private Float minScore;
@Getter
private Map<String, ?> methodParameters;
@Getter
private QueryBuilder filter;
@Getter
private boolean ignoreUnmapped;
@Getter
private RescoreContext rescoreContext;
@Getter
private Boolean expandNested;
/**
* Constructs a new query with the given field name and vector
*
* @param fieldName Name of the field
* @param vector Array of floating points
* @deprecated Use {@code {@link KNNQueryBuilder.Builder}} instead
*/
@Deprecated
public KNNQueryBuilder(String fieldName, float[] vector) {
if (Strings.isNullOrEmpty(fieldName)) {
throw new IllegalArgumentException(String.format("[%s] requires fieldName", NAME));
}
if (vector == null) {
throw new IllegalArgumentException(String.format("[%s] requires query vector", NAME));
}
if (vector.length == 0) {
throw new IllegalArgumentException(String.format("[%s] query vector is empty", NAME));
}
this.fieldName = fieldName;
this.vector = vector;
}
/**
* lombok SuperBuilder annotation requires a builder annotation on parent class to work well
* {@link AbstractQueryBuilder#boost()} and {@link AbstractQueryBuilder#queryName()} both need to be called
* A custom builder helps with the calls to the parent class, simultaneously addressing the problem of telescoping
* constructors in this class.
*/
public static class Builder {
private String fieldName;
private float[] vector;
private Integer k;
private Map<String, ?> methodParameters;
private Float maxDistance;
private Float minScore;
private QueryBuilder filter;
private boolean ignoreUnmapped;
private String queryName;
private float boost = DEFAULT_BOOST;
private RescoreContext rescoreContext;
private Boolean expandNested;
public Builder() {}
public Builder fieldName(String fieldName) {
this.fieldName = fieldName;
return this;
}
public Builder vector(float[] vector) {
this.vector = vector;
return this;
}
public Builder k(Integer k) {
this.k = k;
return this;
}
public Builder methodParameters(Map<String, ?> methodParameters) {
this.methodParameters = methodParameters;
return this;
}
public Builder maxDistance(Float maxDistance) {
this.maxDistance = maxDistance;
return this;
}
public Builder minScore(Float minScore) {
this.minScore = minScore;
return this;
}
public Builder ignoreUnmapped(boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
return this;
}
public Builder filter(QueryBuilder filter) {
this.filter = filter;
return this;
}
public Builder queryName(String queryName) {
this.queryName = queryName;
return this;
}
public Builder boost(float boost) {
this.boost = boost;
return this;
}
public Builder rescoreContext(RescoreContext rescoreContext) {
this.rescoreContext = rescoreContext;
return this;
}
public Builder expandNested(Boolean expandNested) {
this.expandNested = expandNested;
return this;
}
public KNNQueryBuilder build() {
validate();
int k = this.k == null ? 0 : this.k;
return new KNNQueryBuilder(
fieldName,
vector,
k,
maxDistance,
minScore,
methodParameters,
filter,
ignoreUnmapped,
rescoreContext,
expandNested
).boost(boost).queryName(queryName);
}
private void validate() {
if (Strings.isNullOrEmpty(fieldName)) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires fieldName", NAME));
}
if (vector == null) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires query vector", NAME));
} else if (vector.length == 0) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] query vector is empty", NAME));
}
if (k == null && minScore == null && maxDistance == null) {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "[%s] requires exactly one of k, distance or score to be set", NAME)
);
}
if ((k != null && maxDistance != null) || (maxDistance != null && minScore != null) || (k != null && minScore != null)) {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "[%s] requires exactly one of k, distance or score to be set", NAME)
);
}
if (k != null) {
if (k <= 0 || k > K_MAX) {
final String errorMessage = "[" + NAME + "] requires k to be in the range (0, " + K_MAX + "]";
throw new IllegalArgumentException(errorMessage);
}
}
if (minScore != null) {
if (minScore <= 0) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires minScore to be greater than 0", NAME));
}
}
if (methodParameters != null) {
ValidationException validationException = validateMethodParameters(methodParameters);
if (validationException != null) {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "[%s] errors in method parameter [%s]", NAME, validationException.getMessage())
);
}
}
if (rescoreContext != null) {
ValidationException validationException = RescoreParser.validate(rescoreContext);
if (validationException != null) {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "[%s] errors in rescore parameter [%s]", NAME, validationException.getMessage())
);
}
}
}
}
public static KNNQueryBuilder.Builder builder() {
return new KNNQueryBuilder.Builder();
}
/**
* Constructs a new query for top k search
*
* @param fieldName Name of the filed
* @param vector Array of floating points
* @param k K nearest neighbours for the given vector
*/
@Deprecated
public KNNQueryBuilder(String fieldName, float[] vector, int k) {
this(fieldName, vector, k, null);
}
@Deprecated
public KNNQueryBuilder(String fieldName, float[] vector, int k, QueryBuilder filter) {
if (Strings.isNullOrEmpty(fieldName)) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires fieldName", NAME));
}
if (vector == null) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires query vector", NAME));
}
if (vector.length == 0) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] query vector is empty", NAME));
}
if (k <= 0) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires k > 0", NAME));
}
if (k > K_MAX) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires k <= %d", NAME, K_MAX));
}
this.fieldName = fieldName;
this.vector = vector;
this.k = k;
this.filter = filter;
this.ignoreUnmapped = false;
this.maxDistance = null;
this.minScore = null;
this.rescoreContext = null;
this.expandNested = null;
}
public static void initialize(ModelDao modelDao) {
KNNQueryBuilder.modelDao = modelDao;
}
/**
* @param in Reads from stream
* @throws IOException Throws IO Exception
*/
public KNNQueryBuilder(StreamInput in) throws IOException {
super(in);
KNNQueryBuilder.Builder builder = KNNQueryBuilderParser.streamInput(in, IndexUtil::isClusterOnOrAfterMinRequiredVersion);
fieldName = builder.fieldName;
vector = builder.vector;
k = builder.k;
filter = builder.filter;
ignoreUnmapped = builder.ignoreUnmapped;
maxDistance = builder.maxDistance;
minScore = builder.minScore;
methodParameters = builder.methodParameters;
rescoreContext = builder.rescoreContext;
expandNested = builder.expandNested;
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
KNNQueryBuilderParser.streamOutput(out, this, IndexUtil::isClusterOnOrAfterMinRequiredVersion);
}
/**
* @return The field name used in this query
*/
public String fieldName() {
return this.fieldName;
}
/**
* @return Returns the vector used in this query.
*/
public Object vector() {
return this.vector;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
KNNQueryBuilderParser.toXContent(builder, params, this);
}
@Override
protected Query doToQuery(QueryShardContext context) {
MappedFieldType mappedFieldType = context.fieldMapper(this.fieldName);
if (mappedFieldType == null && ignoreUnmapped) {
return new MatchNoDocsQuery();
}
if (!(mappedFieldType instanceof KNNVectorFieldType)) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "Field '%s' is not knn_vector type.", this.fieldName));
}
KNNVectorFieldType knnVectorFieldType = (KNNVectorFieldType) mappedFieldType;
KNNMappingConfig knnMappingConfig = knnVectorFieldType.getKnnMappingConfig();
final AtomicReference<QueryConfigFromMapping> queryConfigFromMapping = new AtomicReference<>();
int fieldDimension = knnMappingConfig.getDimension();
knnMappingConfig.getKnnMethodContext()
.ifPresentOrElse(
knnMethodContext -> queryConfigFromMapping.set(
new QueryConfigFromMapping(
knnMethodContext.getKnnEngine(),
knnMethodContext.getMethodComponentContext(),
knnMethodContext.getSpaceType(),
knnVectorFieldType.getVectorDataType()
)
),
() -> knnMappingConfig.getModelId().ifPresentOrElse(modelId -> {
ModelMetadata modelMetadata = getModelMetadataForField(modelId);
queryConfigFromMapping.set(
new QueryConfigFromMapping(
modelMetadata.getKnnEngine(),
modelMetadata.getMethodComponentContext(),
modelMetadata.getSpaceType(),
modelMetadata.getVectorDataType()
)
);
},
() -> {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "Field '%s' is not built for ANN search.", this.fieldName)
);
}
)
);
KNNEngine knnEngine = queryConfigFromMapping.get().getKnnEngine();
MethodComponentContext methodComponentContext = queryConfigFromMapping.get().getMethodComponentContext();
SpaceType spaceType = queryConfigFromMapping.get().getSpaceType();
VectorDataType vectorDataType = queryConfigFromMapping.get().getVectorDataType();
RescoreContext processedRescoreContext = knnVectorFieldType.resolveRescoreContext(rescoreContext);
VectorQueryType vectorQueryType = getVectorQueryType(k, maxDistance, minScore);
updateQueryStats(vectorQueryType);
// This could be null in the case of when a model did not have serialized methodComponent information
final String method = methodComponentContext != null ? methodComponentContext.getName() : null;
if (StringUtils.isNotBlank(method)) {
final KNNLibrarySearchContext engineSpecificMethodContext = knnEngine.getKNNLibrarySearchContext(method);
QueryContext queryContext = new QueryContext(vectorQueryType);
ValidationException validationException = validateParameters(
engineSpecificMethodContext.supportedMethodParameters(queryContext),
(Map<String, Object>) methodParameters,
KNNMethodConfigContext.EMPTY
);
if (validationException != null) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"Parameters not valid for [%s]:[%s]:[%s] combination: [%s]",
knnEngine,
method,
vectorQueryType.getQueryTypeName(),
validationException.getMessage()
)
);
}
}
if (this.maxDistance != null || this.minScore != null) {
if (!ENGINES_SUPPORTING_RADIAL_SEARCH.contains(knnEngine)) {
throw new UnsupportedOperationException(
String.format(Locale.ROOT, "Engine [%s] does not support radial search", knnEngine)
);
}
if (vectorDataType == VectorDataType.BINARY) {
throw new UnsupportedOperationException(String.format(Locale.ROOT, "Binary data type does not support radial search"));
}
if (knnMappingConfig.getQuantizationConfig() != QuantizationConfig.EMPTY) {
throw new UnsupportedOperationException("Radial search is not supported for indices which have quantization enabled");
}
}
// Currently, k-NN supports distance and score types radial search
// We need transform distance/score to right type of engine required radius.
Float radius = null;
if (this.maxDistance != null) {
if (this.maxDistance < 0 && SpaceType.INNER_PRODUCT.equals(spaceType) == false) {
throw new IllegalArgumentException(
String.format("[" + NAME + "] requires distance to be non-negative for space type: %s", spaceType)
);
}
radius = knnEngine.distanceToRadialThreshold(this.maxDistance, spaceType);
}
if (this.minScore != null) {
if (this.minScore > 1 && SpaceType.INNER_PRODUCT.equals(spaceType) == false) {
throw new IllegalArgumentException(
String.format("[" + NAME + "] requires score to be in the range [0, 1] for space type: %s", spaceType)
);
}
radius = knnEngine.scoreToRadialThreshold(this.minScore, spaceType);
}
int vectorLength = VectorDataType.BINARY == vectorDataType ? vector.length * Byte.SIZE : vector.length;
if (fieldDimension != vectorLength) {
throw new IllegalArgumentException(
String.format("Query vector has invalid dimension: %d. Dimension should be: %d", vectorLength, fieldDimension)
);
}
byte[] byteVector = new byte[0];
switch (vectorDataType) {
case BINARY:
byteVector = new byte[vector.length];
for (int i = 0; i < vector.length; i++) {
validateByteVectorValue(vector[i], knnVectorFieldType.getVectorDataType());
byteVector[i] = (byte) vector[i];
}
spaceType.validateVector(byteVector);
break;
case BYTE:
if (KNNEngine.LUCENE == knnEngine) {
byteVector = new byte[vector.length];
for (int i = 0; i < vector.length; i++) {
validateByteVectorValue(vector[i], knnVectorFieldType.getVectorDataType());
byteVector[i] = (byte) vector[i];
}
spaceType.validateVector(byteVector);
} else {
for (float v : vector) {
validateByteVectorValue(v, knnVectorFieldType.getVectorDataType());
}
spaceType.validateVector(vector);
}
break;
default:
spaceType.validateVector(vector);
}
if (KNNEngine.getEnginesThatCreateCustomSegmentFiles().contains(knnEngine)
&& filter != null
&& !KNNEngine.getEnginesThatSupportsFilters().contains(knnEngine)) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "Engine [%s] does not support filters", knnEngine));
}
String indexName = context.index().getName();
if (k != 0) {
KNNQueryFactory.CreateQueryRequest createQueryRequest = KNNQueryFactory.CreateQueryRequest.builder()
.knnEngine(knnEngine)
.indexName(indexName)
.fieldName(this.fieldName)
.vector(getVectorForCreatingQueryRequest(vectorDataType, knnEngine))
.byteVector(getVectorForCreatingQueryRequest(vectorDataType, knnEngine, byteVector))
.vectorDataType(vectorDataType)
.k(this.k)
.methodParameters(this.methodParameters)
.filter(this.filter)
.context(context)
.rescoreContext(processedRescoreContext)
.expandNested(expandNested)
.build();
return KNNQueryFactory.create(createQueryRequest);
}
if (radius != null) {
RNNQueryFactory.CreateQueryRequest createQueryRequest = RNNQueryFactory.CreateQueryRequest.builder()
.knnEngine(knnEngine)
.indexName(indexName)
.fieldName(this.fieldName)
.vector(VectorDataType.FLOAT == vectorDataType ? this.vector : null)
.byteVector(VectorDataType.BYTE == vectorDataType ? byteVector : null)
.vectorDataType(vectorDataType)
.radius(radius)
.methodParameters(this.methodParameters)
.filter(this.filter)
.context(context)
.build();
return RNNQueryFactory.create(createQueryRequest);
}
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires k or distance or score to be set", NAME));
}
private ModelMetadata getModelMetadataForField(String modelId) {
ModelMetadata modelMetadata = modelDao.getMetadata(modelId);
if (!ModelUtil.isModelCreated(modelMetadata)) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "Model ID '%s' is not created.", modelId));
}
return modelMetadata;
}
/**
* Function to get the vector query type based on the valid query parameter.
*
* @param k K nearest neighbours for the given vector, if k is set, then the query type is K
* @param maxDistance Maximum distance for the given vector, if maxDistance is set, then the query type is MAX_DISTANCE
* @param minScore Minimum score for the given vector, if minScore is set, then the query type is MIN_SCORE
*/
private VectorQueryType getVectorQueryType(int k, Float maxDistance, Float minScore) {
if (maxDistance != null) {
return VectorQueryType.MAX_DISTANCE;
}
if (minScore != null) {
return VectorQueryType.MIN_SCORE;
}
if (k != 0) {
return VectorQueryType.K;
}
throw new IllegalArgumentException(String.format(Locale.ROOT, "[%s] requires exactly one of k, distance or score to be set", NAME));
}
/**
* Function to update query stats.
*
* @param vectorQueryType The type of query to be executed
*/
private void updateQueryStats(VectorQueryType vectorQueryType) {
vectorQueryType.getQueryStatCounter().increment();
if (filter != null) {
vectorQueryType.getQueryWithFilterStatCounter().increment();
}
}
private float[] getVectorForCreatingQueryRequest(VectorDataType vectorDataType, KNNEngine knnEngine) {
if ((VectorDataType.FLOAT == vectorDataType) || (VectorDataType.BYTE == vectorDataType && KNNEngine.FAISS == knnEngine)) {
return this.vector;
}
return null;
}
private byte[] getVectorForCreatingQueryRequest(VectorDataType vectorDataType, KNNEngine knnEngine, byte[] byteVector) {
if (VectorDataType.BINARY == vectorDataType || (VectorDataType.BYTE == vectorDataType && KNNEngine.LUCENE == knnEngine)) {
return byteVector;
}
return null;
}
@Override
protected boolean doEquals(KNNQueryBuilder other) {
return Objects.equals(fieldName, other.fieldName)
&& Arrays.equals(vector, other.vector)
&& Objects.equals(k, other.k)
&& Objects.equals(minScore, other.minScore)
&& Objects.equals(maxDistance, other.maxDistance)
&& Objects.equals(methodParameters, other.methodParameters)
&& Objects.equals(filter, other.filter)
&& Objects.equals(ignoreUnmapped, other.ignoreUnmapped)
&& Objects.equals(rescoreContext, other.rescoreContext)
&& Objects.equals(expandNested, other.expandNested);
}
@Override
protected int doHashCode() {
return Objects.hash(
fieldName,
Arrays.hashCode(vector),
k,
methodParameters,
filter,
ignoreUnmapped,
maxDistance,
minScore,
rescoreContext,
expandNested
);
}
@Override
public String getWriteableName() {
return NAME;
}
@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
// rewrite filter query if it exists to avoid runtime errors in next steps of query phase
if (Objects.nonNull(filter)) {
filter = filter.rewrite(queryShardContext);
}
return super.doRewrite(queryShardContext);
}
@Getter
@AllArgsConstructor
private static class QueryConfigFromMapping {
private final KNNEngine knnEngine;
private final MethodComponentContext methodComponentContext;
private final SpaceType spaceType;
private final VectorDataType vectorDataType;
}
}