Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand All @@ -100,7 +100,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Get rescoring scores - hit ordering may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ setup:
k: 3
num_candidates: 3
"rescore_vector":
"num_candidates_factor": 2.0
"oversample": 2.0

# We expect the knn search ops + rescoring num_cnaidates (for rescoring) per shard
# We expect the knn search ops + rescoring k * oversample (for rescoring) per shard
- match: { profile.shards.0.dfs.knn.0.vector_operations_count: 6 }

# Search with similarity to check number of operations are propagated correctly
Expand All @@ -131,7 +131,7 @@ setup:
num_candidates: 3
similarity: 100000
"rescore_vector":
"num_candidates_factor": 2.0
"oversample": 2.0

# We expect the knn search ops + rescoring num_cnaidates (for rescoring) per shard
# We expect the knn search ops + rescoring k * oversample (for rescoring) per shard
- match: { profile.shards.0.dfs.knn.0.vector_operations_count: 6 }
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand Down Expand Up @@ -589,7 +589,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Compare scores as hit IDs may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand All @@ -140,7 +140,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Get rescoring scores - hit ordering may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand All @@ -398,7 +398,7 @@ setup:
field: vector
query_vector: [0.5, 111.3, -13.0, 14.8, -156.0]
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Get rescoring scores - hit ordering may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand All @@ -575,7 +575,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Get rescoring scores - hit ordering may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand All @@ -139,7 +139,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Get rescoring scores - hit ordering may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand Down Expand Up @@ -304,7 +304,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Compare scores as hit IDs may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand All @@ -371,7 +371,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Get rescoring scores - hit ordering may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand All @@ -288,7 +288,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Get rescoring scores - hit ordering may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand Down Expand Up @@ -454,7 +454,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Compare scores as hit IDs may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand Down Expand Up @@ -270,7 +270,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Compare scores as hit IDs may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ setup:
capabilities:
- method: GET
path: /_search
capabilities: [knn_quantized_vector_rescore]
capabilities: [knn_quantized_vector_rescore_oversample]
- skip:
features: "headers"

Expand Down Expand Up @@ -303,7 +303,7 @@ setup:
k: 3
num_candidates: 3
rescore_vector:
num_candidates_factor: 1.5
oversample: 1.5

# Compare scores as hit IDs may change depending on how things are distributed
- match: { hits.total: 3 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static boolean isNotUnitVector(float magnitude) {

public static short MIN_DIMS_FOR_DYNAMIC_FLOAT_MAPPING = 128; // minimum number of dims for floats to be dynamically mapped to vector
public static final int MAGNITUDE_BYTES = 4;
public static final int NUM_CANDS_OVERSAMPLE_LIMIT = 10_000; // Max oversample allowed for k and num_candidates
public static final int OVERSAMPLE_LIMIT = 10_000; // Max oversample allowed

private static DenseVectorFieldMapper toType(FieldMapper in) {
return (DenseVectorFieldMapper) in;
Expand Down Expand Up @@ -2019,7 +2019,7 @@ public Query createKnnQuery(
VectorData queryVector,
int k,
int numCands,
Float numCandsFactor,
Float oversample,
Query filter,
Float similarityThreshold,
BitSetProducer parentFilter
Expand All @@ -2035,7 +2035,7 @@ public Query createKnnQuery(
queryVector.asFloatVector(),
k,
numCands,
numCandsFactor,
oversample,
filter,
similarityThreshold,
parentFilter
Expand All @@ -2045,7 +2045,11 @@ public Query createKnnQuery(
}

private boolean needsRescore(Float rescoreOversample) {
return rescoreOversample != null && (indexOptions != null && indexOptions.type != null && indexOptions.type.isQuantized());
return rescoreOversample != null && isQuantized();
}

private boolean isQuantized() {
return indexOptions != null && indexOptions.type != null && indexOptions.type.isQuantized();
}

private Query createKnnBitQuery(
Expand Down Expand Up @@ -2101,7 +2105,7 @@ private Query createKnnFloatQuery(
float[] queryVector,
int k,
int numCands,
Float numCandsFactor,
Float oversample,
Query filter,
Float similarityThreshold,
BitSetProducer parentFilter
Expand All @@ -2122,18 +2126,17 @@ && isNotUnitVector(squaredMagnitude)) {
}
}

Integer adjustedK = k;
int adjustedNumCands = numCands;
if (needsRescore(numCandsFactor)) {
// Get all candidates, get top k as part of rescoring
adjustedK = null;
// numCands * numCandsFactor <= NUM_CANDS_OVERSAMPLE_LIMIT. Adjust otherwise.
adjustedNumCands = Math.min((int) Math.ceil(numCands * numCandsFactor), NUM_CANDS_OVERSAMPLE_LIMIT);
int adjustedK = k;
boolean rescore = needsRescore(oversample);
if (rescore) {
// Will get k * oversample for rescoring, and get the top k
adjustedK = Math.min((int) Math.ceil(k * oversample), OVERSAMPLE_LIMIT);
numCands = Math.max(adjustedK, numCands);
}
Query knnQuery = parentFilter != null
? new ESDiversifyingChildrenFloatKnnVectorQuery(name(), queryVector, filter, adjustedK, adjustedNumCands, parentFilter)
: new ESKnnFloatVectorQuery(name(), queryVector, adjustedK, adjustedNumCands, filter);
if (needsRescore(numCandsFactor)) {
? new ESDiversifyingChildrenFloatKnnVectorQuery(name(), queryVector, filter, adjustedK, numCands, parentFilter)
: new ESKnnFloatVectorQuery(name(), queryVector, adjustedK, numCands, filter);
if (rescore) {
knnQuery = new RescoreKnnVectorQuery(
name(),
queryVector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private SearchCapabilities() {}

private static final String RANDOM_SAMPLER_WITH_SCORED_SUBAGGS = "random_sampler_with_scored_subaggs";
private static final String OPTIMIZED_SCALAR_QUANTIZATION_BBQ = "optimized_scalar_quantization_bbq";
private static final String KNN_QUANTIZED_VECTOR_RESCORE = "knn_quantized_vector_rescore";
private static final String KNN_QUANTIZED_VECTOR_RESCORE_OVERSAMPLE = "knn_quantized_vector_rescore_oversample";

public static final Set<String> CAPABILITIES;
static {
Expand All @@ -50,7 +50,7 @@ private SearchCapabilities() {}
capabilities.add(NESTED_RETRIEVER_INNER_HITS_SUPPORT);
capabilities.add(RANDOM_SAMPLER_WITH_SCORED_SUBAGGS);
capabilities.add(OPTIMIZED_SCALAR_QUANTIZATION_BBQ);
capabilities.add(KNN_QUANTIZED_VECTOR_RESCORE);
capabilities.add(KNN_QUANTIZED_VECTOR_RESCORE_OVERSAMPLE);
capabilities.add(MOVING_FN_RIGHT_MATH);
capabilities.add(K_DEFAULT_TO_SIZE);
capabilities.add(KQL_QUERY_SUPPORTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {

DenseVectorFieldType vectorFieldType = (DenseVectorFieldType) fieldType;
String parentPath = context.nestedLookup().getNestedParent(fieldName);
Float numCandidatesFactor = rescoreVectorBuilder() == null ? null : rescoreVectorBuilder.numCandidatesFactor();
Float oversample = rescoreVectorBuilder() == null ? null : rescoreVectorBuilder.oversample();

BitSetProducer parentBitSet = null;
if (parentPath != null) {
Expand Down Expand Up @@ -557,15 +557,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
}
}

return vectorFieldType.createKnnQuery(
queryVector,
k,
adjustedNumCands,
numCandidatesFactor,
filterQuery,
vectorSimilarity,
parentBitSet
);
return vectorFieldType.createKnnQuery(queryVector, k, adjustedNumCands, oversample, filterQuery, vectorSimilarity, parentBitSet);
}

@Override
Expand Down
Loading