Skip to content

Commit 98c42d2

Browse files
committed
make early termination disabled by default
1 parent 4b2029c commit 98c42d2

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ public class DenseVectorFieldMapper extends FieldMapper {
117117
public static final String COSINE_MAGNITUDE_FIELD_SUFFIX = "._magnitude";
118118
private static final float EPS = 1e-3f;
119119
public static final int BBQ_MIN_DIMS = 64;
120+
121+
// early termination configuration
120122
private static final String EARLY_EXIT_PARAM_NAME = "early_exit";
123+
private static final boolean DEFAULT_EARLY_EXIT = false;
121124

122125
public static boolean isNotUnitVector(float magnitude) {
123126
return Math.abs(magnitude - 1.0f) > EPS;
@@ -1426,7 +1429,7 @@ public IndexOptions parseIndexOptions(String fieldName, Map<String, ?> indexOpti
14261429
efConstructionNode = Lucene99HnswVectorsFormat.DEFAULT_BEAM_WIDTH;
14271430
}
14281431
if (earlyExitNode == null) {
1429-
earlyExitNode = true;
1432+
earlyExitNode = DEFAULT_EARLY_EXIT;
14301433
}
14311434
int m = XContentMapValues.nodeIntegerValue(mNode);
14321435
int efConstruction = XContentMapValues.nodeIntegerValue(efConstructionNode);
@@ -1704,7 +1707,7 @@ boolean updatableTo(IndexOptions update) {
17041707

17051708
@Override
17061709
boolean earlyExit() {
1707-
return false;
1710+
return DEFAULT_EARLY_EXIT;
17081711
}
17091712
}
17101713

@@ -1737,7 +1740,7 @@ boolean updatableTo(IndexOptions update) {
17371740

17381741
@Override
17391742
boolean earlyExit() {
1740-
return false;
1743+
return DEFAULT_EARLY_EXIT;
17411744
}
17421745

17431746
@Override
@@ -1758,7 +1761,7 @@ static class Int4HnswIndexOptions extends QuantizedIndexOptions {
17581761
private final boolean earlyExit;
17591762

17601763
Int4HnswIndexOptions(int m, int efConstruction, Float confidenceInterval, RescoreVector rescoreVector) {
1761-
this(m, efConstruction, confidenceInterval, rescoreVector, true);
1764+
this(m, efConstruction, confidenceInterval, rescoreVector, DEFAULT_EARLY_EXIT);
17621765
}
17631766

17641767
Int4HnswIndexOptions(int m, int efConstruction, Float confidenceInterval, RescoreVector rescoreVector, boolean earlyExit) {
@@ -1896,7 +1899,7 @@ boolean updatableTo(IndexOptions update) {
18961899

18971900
@Override
18981901
boolean earlyExit() {
1899-
return false;
1902+
return DEFAULT_EARLY_EXIT;
19001903
}
19011904

19021905
}
@@ -1908,7 +1911,7 @@ public static class Int8HnswIndexOptions extends QuantizedIndexOptions {
19081911
private final boolean earlyExit;
19091912

19101913
public Int8HnswIndexOptions(int m, int efConstruction, Float confidenceInterval, RescoreVector rescoreVector) {
1911-
this(m, efConstruction, confidenceInterval, rescoreVector, true);
1914+
this(m, efConstruction, confidenceInterval, rescoreVector, DEFAULT_EARLY_EXIT);
19121915
}
19131916

19141917
public Int8HnswIndexOptions(int m, int efConstruction, Float confidenceInterval, RescoreVector rescoreVector, boolean earlyExit) {
@@ -2002,7 +2005,7 @@ static class HnswIndexOptions extends IndexOptions {
20022005
private final boolean earlyExit;
20032006

20042007
HnswIndexOptions(int m, int efConstruction) {
2005-
this(m, efConstruction, true);
2008+
this(m, efConstruction, DEFAULT_EARLY_EXIT);
20062009
}
20072010

20082011
HnswIndexOptions(int m, int efConstruction, boolean earlyExit) {

server/src/test/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldTypeTests.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.apache.lucene.search.KnnByteVectorQuery;
1313
import org.apache.lucene.search.KnnFloatVectorQuery;
14+
import org.apache.lucene.search.PatienceKnnVectorQuery;
1415
import org.apache.lucene.search.Query;
1516
import org.apache.lucene.search.join.BitSetProducer;
1617
import org.apache.lucene.search.join.DiversifyingChildrenByteKnnVectorQuery;
@@ -478,7 +479,7 @@ public void testCreateKnnQueryMaxDims() {
478479
null,
479480
randomFrom(DenseVectorFieldMapper.FilterHeuristic.values())
480481
);
481-
assertThat(query, instanceOf(KnnByteVectorQuery.class));
482+
assertThat(query, instanceOf(ESKnnByteVectorQuery.class));
482483
}
483484
}
484485

@@ -577,16 +578,20 @@ public void testRescoreOversampleUsedWithoutQuantization() {
577578
);
578579

579580
if (elementType == BYTE) {
580-
KnnByteVectorQuery knnByteVectorQuery = (KnnByteVectorQuery) knnQuery;
581-
assertThat(knnByteVectorQuery.getK(), is(100));
582-
if (knnByteVectorQuery instanceof ESKnnByteVectorQuery esKnnByteVectorQuery) {
583-
assertThat(esKnnByteVectorQuery.kParam(), is(10));
581+
if (knnQuery instanceof PatienceKnnVectorQuery patienceKnnVectorQuery) {
582+
assertThat(patienceKnnVectorQuery.getK(), is(100));
583+
} else {
584+
ESKnnByteVectorQuery knnByteVectorQuery = (ESKnnByteVectorQuery) knnQuery;
585+
assertThat(knnByteVectorQuery.getK(), is(100));
586+
assertThat(knnByteVectorQuery.kParam(), is(10));
584587
}
585588
} else {
586-
KnnFloatVectorQuery knnFloatVectorQuery = (KnnFloatVectorQuery) knnQuery;
587-
assertThat(knnFloatVectorQuery.getK(), is(100));
588-
if (knnFloatVectorQuery instanceof ESKnnFloatVectorQuery esKnnFloatVectorQuery) {
589-
assertThat(esKnnFloatVectorQuery.kParam(), is(10));
589+
if (knnQuery instanceof PatienceKnnVectorQuery patienceKnnVectorQuery) {
590+
assertThat(patienceKnnVectorQuery.getK(), is(100));
591+
} else {
592+
ESKnnFloatVectorQuery knnFloatVectorQuery = (ESKnnFloatVectorQuery) knnQuery;
593+
assertThat(knnFloatVectorQuery.getK(), is(100));
594+
assertThat(knnFloatVectorQuery.kParam(), is(10));
590595
}
591596
}
592597
}
@@ -635,7 +640,7 @@ public void testRescoreOversampleQueryOverrides() {
635640
null,
636641
randomFrom(DenseVectorFieldMapper.FilterHeuristic.values())
637642
);
638-
assertTrue(query instanceof KnnFloatVectorQuery);
643+
assertTrue(query instanceof ESKnnFloatVectorQuery);
639644

640645
// verify we can override a `0` to a positive number
641646
fieldType = new DenseVectorFieldType(
@@ -738,11 +743,14 @@ private static void checkRescoreQueryParameters(
738743
randomFrom(DenseVectorFieldMapper.FilterHeuristic.values())
739744
);
740745
RescoreKnnVectorQuery rescoreQuery = (RescoreKnnVectorQuery) query;
741-
KnnFloatVectorQuery knnQuery = (KnnFloatVectorQuery) rescoreQuery.innerQuery();
742-
assertThat("Unexpected total results", rescoreQuery.k(), equalTo(expectedResults));
743-
assertThat("Unexpected candidates", knnQuery.getK(), equalTo(expectedCandidates));
744-
if (knnQuery instanceof ESKnnFloatVectorQuery esKnnFloatVectorQuery) {
745-
assertThat("Unexpected k parameter", esKnnFloatVectorQuery.kParam(), equalTo(expectedK));
746+
Query innerQuery = rescoreQuery.innerQuery();
747+
if (innerQuery instanceof PatienceKnnVectorQuery patienceKnnVectorQuery) {
748+
assertThat("Unexpected candidates", patienceKnnVectorQuery.getK(), equalTo(expectedCandidates));
749+
} else {
750+
ESKnnFloatVectorQuery knnQuery = (ESKnnFloatVectorQuery) innerQuery;
751+
assertThat("Unexpected total results", rescoreQuery.k(), equalTo(expectedResults));
752+
assertThat("Unexpected candidates", knnQuery.getK(), equalTo(expectedCandidates));
753+
assertThat("Unexpected k parameter", knnQuery.kParam(), equalTo(expectedK));
746754
}
747755
}
748756
}

0 commit comments

Comments
 (0)