Skip to content

Commit 25cf81b

Browse files
keljpountz
kel
authored andcommitted
Remove UnsortedNumericDoubleValues (#26817)
Closes #24086
1 parent a9e0f06 commit 25cf81b

File tree

3 files changed

+33
-228
lines changed

3 files changed

+33
-228
lines changed

core/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java

+32-29
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
4444
import org.elasticsearch.index.fielddata.NumericDoubleValues;
4545
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
46-
import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType;
46+
import org.elasticsearch.index.fielddata.SortingNumericDoubleValues;
4747
import org.elasticsearch.index.mapper.DateFieldMapper;
48+
import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType;
4849
import org.elasticsearch.index.mapper.MappedFieldType;
4950
import org.elasticsearch.index.mapper.NumberFieldMapper;
5051
import org.elasticsearch.index.query.QueryShardContext;
@@ -346,22 +347,23 @@ public boolean needsScores() {
346347
@Override
347348
protected NumericDoubleValues distance(LeafReaderContext context) {
348349
final MultiGeoPointValues geoPointValues = fieldData.load(context).getGeoPointValues();
349-
return mode.select(new MultiValueMode.UnsortedNumericDoubleValues() {
350-
@Override
351-
public int docValueCount() {
352-
return geoPointValues.docValueCount();
353-
}
354-
350+
return mode.select(new SortingNumericDoubleValues() {
355351
@Override
356352
public boolean advanceExact(int docId) throws IOException {
357-
return geoPointValues.advanceExact(docId);
358-
}
359-
360-
@Override
361-
public double nextValue() throws IOException {
362-
GeoPoint other = geoPointValues.nextValue();
363-
return Math.max(0.0d,
364-
distFunction.calculate(origin.lat(), origin.lon(), other.lat(), other.lon(), DistanceUnit.METERS) - offset);
353+
if (geoPointValues.advanceExact(docId)) {
354+
int n = geoPointValues.docValueCount();
355+
resize(n);
356+
for (int i = 0; i < n; i++) {
357+
GeoPoint other = geoPointValues.nextValue();
358+
double distance = distFunction.calculate(
359+
origin.lat(), origin.lon(), other.lat(), other.lon(), DistanceUnit.METERS);
360+
values[i] = Math.max(0.0d, distance - offset);
361+
}
362+
sort();
363+
return true;
364+
} else {
365+
return false;
366+
}
365367
}
366368
}, 0.0);
367369
}
@@ -427,20 +429,20 @@ public boolean needsScores() {
427429
@Override
428430
protected NumericDoubleValues distance(LeafReaderContext context) {
429431
final SortedNumericDoubleValues doubleValues = fieldData.load(context).getDoubleValues();
430-
return mode.select(new MultiValueMode.UnsortedNumericDoubleValues() {
432+
return mode.select(new SortingNumericDoubleValues() {
431433
@Override
432-
public int docValueCount() {
433-
return doubleValues.docValueCount();
434-
}
435-
436-
@Override
437-
public boolean advanceExact(int doc) throws IOException {
438-
return doubleValues.advanceExact(doc);
439-
}
440-
441-
@Override
442-
public double nextValue() throws IOException {
443-
return Math.max(0.0d, Math.abs(doubleValues.nextValue() - origin) - offset);
434+
public boolean advanceExact(int docId) throws IOException {
435+
if (doubleValues.advanceExact(docId)) {
436+
int n = doubleValues.docValueCount();
437+
resize(n);
438+
for (int i = 0; i < n; i++) {
439+
values[i] = Math.max(0.0d, Math.abs(doubleValues.nextValue() - origin) - offset);
440+
}
441+
sort();
442+
return true;
443+
} else {
444+
return false;
445+
}
444446
}
445447
}, 0.0);
446448
}
@@ -542,10 +544,11 @@ public Explanation explainScore(int docId, Explanation subQueryScore) throws IOE
542544
if (distance.advanceExact(docId) == false) {
543545
return Explanation.noMatch("No value for the distance");
544546
}
547+
double value = distance.doubleValue();
545548
return Explanation.match(
546549
(float) score(docId, subQueryScore.getValue()),
547550
"Function for field " + getFieldName() + ":",
548-
func.explainFunction(getDistanceString(ctx, docId), distance.doubleValue(), scale));
551+
func.explainFunction(getDistanceString(ctx, docId), value, scale));
549552
}
550553
};
551554
}

core/src/main/java/org/elasticsearch/search/MultiValueMode.java

-77
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@ protected double pick(SortedNumericDoubleValues values, double missingValue, Doc
104104
}
105105
return totalCount > 0 ? totalValue : missingValue;
106106
}
107-
108-
@Override
109-
protected double pick(UnsortedNumericDoubleValues values) throws IOException {
110-
final int count = values.docValueCount();
111-
double total = 0;
112-
for (int index = 0; index < count; ++index) {
113-
total += values.nextValue();
114-
}
115-
return total;
116-
}
117107
},
118108

119109
/**
@@ -177,16 +167,6 @@ protected double pick(SortedNumericDoubleValues values, double missingValue, Doc
177167
}
178168
return totalValue/totalCount;
179169
}
180-
181-
@Override
182-
protected double pick(UnsortedNumericDoubleValues values) throws IOException {
183-
final int count = values.docValueCount();
184-
double total = 0;
185-
for (int index = 0; index < count; ++index) {
186-
total += values.nextValue();
187-
}
188-
return total/count;
189-
}
190170
},
191171

192172
/**
@@ -303,16 +283,6 @@ protected int pick(SortedDocValues values, DocIdSetIterator docItr, int startDoc
303283
}
304284
return hasValue ? ord : -1;
305285
}
306-
307-
@Override
308-
protected double pick(UnsortedNumericDoubleValues values) throws IOException {
309-
int count = values.docValueCount();
310-
double min = Double.POSITIVE_INFINITY;
311-
for (int index = 0; index < count; ++index) {
312-
min = Math.min(values.nextValue(), min);
313-
}
314-
return min;
315-
}
316286
},
317287

318288
/**
@@ -419,16 +389,6 @@ protected int pick(SortedDocValues values, DocIdSetIterator docItr, int startDoc
419389
}
420390
return ord;
421391
}
422-
423-
@Override
424-
protected double pick(UnsortedNumericDoubleValues values) throws IOException {
425-
int count = values.docValueCount();
426-
double max = Double.NEGATIVE_INFINITY;
427-
for (int index = 0; index < count; ++index) {
428-
max = Math.max(values.nextValue(), max);
429-
}
430-
return max;
431-
}
432392
};
433393

434394
/**
@@ -905,43 +865,6 @@ protected int pick(SortedDocValues values, DocIdSetIterator docItr, int startDoc
905865
throw new IllegalArgumentException("Unsupported sort mode: " + this);
906866
}
907867

908-
/**
909-
* Return a {@link NumericDoubleValues} instance that can be used to sort documents
910-
* with this mode and the provided values. When a document has no value,
911-
* <code>missingValue</code> is returned.
912-
*
913-
* Allowed Modes: SUM, AVG, MIN, MAX
914-
*/
915-
public NumericDoubleValues select(final UnsortedNumericDoubleValues values, final double missingValue) {
916-
return new NumericDoubleValues() {
917-
private boolean hasValue;
918-
919-
@Override
920-
public boolean advanceExact(int doc) throws IOException {
921-
hasValue = values.advanceExact(doc);
922-
return true;
923-
}
924-
@Override
925-
public double doubleValue() throws IOException {
926-
return hasValue ? pick(values) : missingValue;
927-
}
928-
};
929-
}
930-
931-
protected double pick(UnsortedNumericDoubleValues values) throws IOException {
932-
throw new IllegalArgumentException("Unsupported sort mode: " + this);
933-
}
934-
935-
/**
936-
* Interface allowing custom value generators to be used in MultiValueMode.
937-
*/
938-
// TODO: why do we need it???
939-
public interface UnsortedNumericDoubleValues {
940-
boolean advanceExact(int doc) throws IOException;
941-
int docValueCount() throws IOException;
942-
double nextValue() throws IOException;
943-
}
944-
945868
@Override
946869
public void writeTo(StreamOutput out) throws IOException {
947870
out.writeEnum(this);

core/src/test/java/org/elasticsearch/search/MultiValueModeTests.java

+1-122
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.elasticsearch.index.fielddata.NumericDoubleValues;
4242
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
4343
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
44-
import org.elasticsearch.search.MultiValueMode.UnsortedNumericDoubleValues;
4544
import org.elasticsearch.test.ESTestCase;
4645

4746
import java.io.IOException;
@@ -92,7 +91,7 @@ public void testSingleValuedLongs() throws Exception {
9291
docsWithValue.set(i);
9392
}
9493
}
95-
94+
9695
final Supplier<SortedNumericDocValues> multiValues = () -> DocValues.singleton(new AbstractNumericDocValues() {
9796
int docId = -1;
9897
@Override
@@ -711,126 +710,6 @@ private void verifySortedSet(Supplier<SortedSetDocValues> supplier, int maxDoc,
711710
}
712711
}
713712

714-
public void testUnsortedSingleValuedDoubles() throws Exception {
715-
final int numDocs = scaledRandomIntBetween(1, 100);
716-
final double[] array = new double[numDocs];
717-
final FixedBitSet docsWithValue = randomBoolean() ? null : new FixedBitSet(numDocs);
718-
for (int i = 0; i < array.length; ++i) {
719-
if (randomBoolean()) {
720-
array[i] = randomDouble();
721-
if (docsWithValue != null) {
722-
docsWithValue.set(i);
723-
}
724-
} else if (docsWithValue != null && randomBoolean()) {
725-
docsWithValue.set(i);
726-
}
727-
}
728-
final NumericDoubleValues singleValues = new NumericDoubleValues() {
729-
private int docID;
730-
@Override
731-
public boolean advanceExact(int doc) throws IOException {
732-
docID = doc;
733-
return docsWithValue == null || docsWithValue.get(docID);
734-
}
735-
@Override
736-
public double doubleValue() {
737-
return array[docID];
738-
}
739-
};
740-
final SortedNumericDoubleValues singletonValues = FieldData.singleton(singleValues);
741-
final MultiValueMode.UnsortedNumericDoubleValues multiValues = new MultiValueMode.UnsortedNumericDoubleValues() {
742-
743-
@Override
744-
public int docValueCount() {
745-
return singletonValues.docValueCount();
746-
}
747-
748-
@Override
749-
public boolean advanceExact(int doc) throws IOException {
750-
return singletonValues.advanceExact(doc);
751-
}
752-
753-
@Override
754-
public double nextValue() throws IOException {
755-
return Math.cos(singletonValues.nextValue());
756-
}
757-
};
758-
verifyUnsortedNumeric(() -> multiValues, numDocs);
759-
}
760-
761-
public void testUnsortedMultiValuedDoubles() throws Exception {
762-
final int numDocs = scaledRandomIntBetween(1, 100);
763-
final double[][] array = new double[numDocs][];
764-
for (int i = 0; i < numDocs; ++i) {
765-
final double[] values = new double[randomInt(4)];
766-
for (int j = 0; j < values.length; ++j) {
767-
values[j] = randomDouble();
768-
}
769-
Arrays.sort(values);
770-
array[i] = values;
771-
}
772-
final MultiValueMode.UnsortedNumericDoubleValues multiValues = new MultiValueMode.UnsortedNumericDoubleValues() {
773-
int doc;
774-
int i;
775-
776-
@Override
777-
public int docValueCount() {
778-
return array[doc].length;
779-
}
780-
781-
@Override
782-
public boolean advanceExact(int doc) {
783-
this.doc = doc;
784-
i = 0;
785-
return array[doc].length > 0;
786-
}
787-
788-
@Override
789-
public double nextValue() {
790-
return Math.sin(array[doc][i++]);
791-
}
792-
};
793-
verifyUnsortedNumeric(() -> multiValues, numDocs);
794-
}
795-
796-
private void verifyUnsortedNumeric(Supplier<MultiValueMode.UnsortedNumericDoubleValues> supplier, int maxDoc) throws IOException {
797-
for (double missingValue : new double[] { 0, randomDouble() }) {
798-
for (MultiValueMode mode : new MultiValueMode[] {MultiValueMode.MIN, MultiValueMode.MAX, MultiValueMode.SUM, MultiValueMode.AVG}) {
799-
UnsortedNumericDoubleValues values = supplier.get();
800-
final NumericDoubleValues selected = mode.select(values, missingValue);
801-
for (int i = 0; i < maxDoc; ++i) {
802-
assertTrue(selected.advanceExact(i));
803-
final double actual = selected.doubleValue();
804-
double expected = 0.0;
805-
if (values.advanceExact(i) == false) {
806-
expected = missingValue;
807-
} else {
808-
int numValues = values.docValueCount();
809-
if (mode == MultiValueMode.MAX) {
810-
expected = Long.MIN_VALUE;
811-
} else if (mode == MultiValueMode.MIN) {
812-
expected = Long.MAX_VALUE;
813-
}
814-
for (int j = 0; j < numValues; ++j) {
815-
if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
816-
expected += values.nextValue();
817-
} else if (mode == MultiValueMode.MIN) {
818-
expected = Math.min(expected, values.nextValue());
819-
} else if (mode == MultiValueMode.MAX) {
820-
expected = Math.max(expected, values.nextValue());
821-
}
822-
}
823-
if (mode == MultiValueMode.AVG) {
824-
expected = expected/numValues;
825-
}
826-
}
827-
828-
assertEquals(mode.toString() + " docId=" + i, expected, actual, 0.1);
829-
}
830-
}
831-
}
832-
}
833-
834713
public void testValidOrdinals() {
835714
assertThat(MultiValueMode.SUM.ordinal(), equalTo(0));
836715
assertThat(MultiValueMode.AVG.ordinal(), equalTo(1));

0 commit comments

Comments
 (0)