Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise multiterms aggregation for single value fields #107937

Merged
merged 2 commits into from
May 1, 2024
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
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