Skip to content

Commit

Permalink
Optimise multiterms aggregation for single value fields (#107937)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase authored May 1, 2024
1 parent 098f1fd commit 0bc7b49
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/107937.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 107937
summary: Optimise multiterms aggregation for single value fields
area: Aggregations
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

package org.elasticsearch.xpack.analytics.multiterms;

import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.util.BytesRef;
Expand All @@ -20,6 +23,8 @@
import org.elasticsearch.common.util.ObjectArrayPriorityQueue;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.core.Releasables;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.search.DocValueFormat;
Expand Down Expand Up @@ -376,14 +381,19 @@ static class LongTermValuesSource implements TermValuesSource {

@Override
public TermValues getValues(LeafReaderContext ctx) throws IOException {
SortedNumericDocValues values = source.longValues(ctx);
final SortedNumericDocValues values = source.longValues(ctx);
final NumericDocValues singleton = DocValues.unwrapSingleton(values);
return singleton != null ? getValues(singleton) : getValues(values);
}

public TermValues getValues(SortedNumericDocValues values) {
return doc -> {
if (values.advanceExact(doc)) {
List<Object> objects = new ArrayList<>();
int valuesCount = values.docValueCount();
final List<Object> objects = new ArrayList<>();
final int valuesCount = values.docValueCount();
long previous = Long.MAX_VALUE;
for (int i = 0; i < valuesCount; ++i) {
long val = values.nextValue();
final long val = values.nextValue();
if (previous != val || i == 0) {
objects.add(val);
previous = val;
Expand All @@ -396,6 +406,16 @@ public TermValues getValues(LeafReaderContext ctx) throws IOException {
};
}

public TermValues getValues(NumericDocValues values) {
return doc -> {
if (values.advanceExact(doc)) {
return List.of(values.longValue());
} else {
return null;
}
};
}

@Override
public InternalMultiTerms.KeyConverter keyConverter() {
return converter;
Expand All @@ -414,14 +434,19 @@ static class DoubleTermValuesSource implements TermValuesSource {

@Override
public TermValues getValues(LeafReaderContext ctx) throws IOException {
SortedNumericDoubleValues values = source.doubleValues(ctx);
final SortedNumericDoubleValues values = source.doubleValues(ctx);
final NumericDoubleValues singleton = FieldData.unwrapSingleton(values);
return singleton != null ? getValues(singleton) : getValues(values);
}

public TermValues getValues(SortedNumericDoubleValues values) {
return doc -> {
if (values.advanceExact(doc)) {
List<Object> objects = new ArrayList<>();
int valuesCount = values.docValueCount();
final List<Object> objects = new ArrayList<>();
final int valuesCount = values.docValueCount();
double previous = Double.MAX_VALUE;
for (int i = 0; i < valuesCount; ++i) {
double val = values.nextValue();
final double val = values.nextValue();
if (previous != val || i == 0) {
objects.add(val);
previous = val;
Expand All @@ -434,6 +459,16 @@ public TermValues getValues(LeafReaderContext ctx) throws IOException {
};
}

public TermValues getValues(NumericDoubleValues values) {
return doc -> {
if (values.advanceExact(doc)) {
return List.of(values.doubleValue());
} else {
return null;
}
};
}

@Override
public InternalMultiTerms.KeyConverter keyConverter() {
return InternalMultiTerms.KeyConverter.DOUBLE;
Expand All @@ -453,16 +488,21 @@ abstract static class BinaryTermValuesSource implements TermValuesSource {

@Override
public TermValues getValues(LeafReaderContext ctx) throws IOException {
SortedBinaryDocValues values = source.bytesValues(ctx);
final SortedBinaryDocValues values = source.bytesValues(ctx);
final BinaryDocValues singleton = FieldData.unwrapSingleton(values);
return singleton != null ? getValues(singleton) : getValues(values);
}

private TermValues getValues(SortedBinaryDocValues values) {
return doc -> {
if (values.advanceExact(doc)) {
int valuesCount = values.docValueCount();
List<Object> objects = new ArrayList<>(valuesCount);
final int valuesCount = values.docValueCount();
final List<Object> objects = new ArrayList<>(valuesCount);
// SortedBinaryDocValues don't guarantee uniqueness so we
// need to take care of dups
previous.clear();
for (int i = 0; i < valuesCount; ++i) {
BytesRef bytes = values.nextValue();
final BytesRef bytes = values.nextValue();
if (i > 0 && previous.get().equals(bytes)) {
continue;
}
Expand All @@ -475,6 +515,16 @@ public TermValues getValues(LeafReaderContext ctx) throws IOException {
}
};
}

private TermValues getValues(BinaryDocValues values) {
return doc -> {
if (values.advanceExact(doc)) {
return List.of(BytesRef.deepCopyOf(values.binaryValue()));
} else {
return null;
}
};
}
}

/**
Expand Down

0 comments on commit 0bc7b49

Please sign in to comment.