Skip to content

value_count Aggregation optimization #54854

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

Merged
merged 2 commits into from
Apr 10, 2020
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 @@ -19,9 +19,11 @@
package org.elasticsearch.search.aggregations.metrics;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.LongArray;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.InternalAggregation;
Expand Down Expand Up @@ -62,6 +64,34 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
return LeafBucketCollector.NO_OP_COLLECTOR;
}
final BigArrays bigArrays = context.bigArrays();

if (valuesSource instanceof ValuesSource.Numeric) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be nice to have a "value counting" member in ValuesSource but I'd be happy to merge this as is and open up a follow up myself to do that. Or you can, if you want @xjtushilei.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As much as it is annoying to put something just for one agg in ValuesSource, I think it'd be nice to have a something in there just so it is more obvious that we do these sorts of things to the values.

Another option is to use the values source refactor to plug in different count implementations. That'd probably be more in line with the direction we're going now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, I'm happy to take this as is and do the twisting around myself in a follow up change and ping you for review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be nice to have a "value counting" member in ValuesSource but I'd be happy to merge this as is and open up a follow up myself to do that. Or you can, if you want @xjtushilei.

yes, you can do it.

final SortedNumericDocValues values = ((ValuesSource.Numeric)valuesSource).longValues(ctx);
return new LeafBucketCollectorBase(sub, values) {

@Override
public void collect(int doc, long bucket) throws IOException {
counts = bigArrays.grow(counts, bucket + 1);
if (values.advanceExact(doc)) {
counts.increment(bucket, values.docValueCount());
}
}
};
}
if (valuesSource instanceof ValuesSource.Bytes.GeoPoint) {
MultiGeoPointValues values = ((ValuesSource.GeoPoint)valuesSource).geoPointValues(ctx);
return new LeafBucketCollectorBase(sub, null) {

@Override
public void collect(int doc, long bucket) throws IOException {
counts = bigArrays.grow(counts, bucket + 1);
if (values.advanceExact(doc)) {
counts.increment(bucket, values.docValueCount());
}
}
};
}
// The following is default collector. Including the keyword FieldType
final SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
return new LeafBucketCollectorBase(sub, values) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import org.apache.lucene.document.BinaryDocValuesField;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoubleDocValuesField;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LatLonDocValuesField;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField;
Expand Down Expand Up @@ -76,8 +78,8 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {

private static final String FIELD_NAME = "field";

/** Script to return the {@code _value} provided by aggs framework. */
private static final String VALUE_SCRIPT = "_value";
private static final String STRING_VALUE_SCRIPT = "string_value";
private static final String NUMBER_VALUE_SCRIPT = "number_value";
private static final String SINGLE_SCRIPT = "single";

@Override
Expand All @@ -99,7 +101,8 @@ protected List<ValuesSourceType> getSupportedValuesSourceTypes() {
protected ScriptService getMockScriptService() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();

scripts.put(VALUE_SCRIPT, vars -> (Double.valueOf((String) vars.get("_value")) + 1));
scripts.put(STRING_VALUE_SCRIPT, vars -> (Double.valueOf((String) vars.get("_value")) + 1));
scripts.put(NUMBER_VALUE_SCRIPT, vars -> (((Number) vars.get("_value")).doubleValue() + 1));
scripts.put(SINGLE_SCRIPT, vars -> 1);

MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME,
Expand All @@ -110,6 +113,38 @@ protected ScriptService getMockScriptService() {
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}


public void testGeoField() throws IOException {
testCase(new MatchAllDocsQuery(), ValueType.GEOPOINT, iw -> {
for (int i = 0; i < 10; i++) {
Document document = new Document();
document.add(new LatLonDocValuesField("field", 10, 10));
iw.addDocument(document);
}
}, count -> assertEquals(10L, count.getValue()));
}

public void testDoubleField() throws IOException {
testCase(new MatchAllDocsQuery(), ValueType.DOUBLE, iw -> {
for (int i = 0; i < 15; i++) {
Document document = new Document();
document.add(new DoubleDocValuesField(FIELD_NAME, 23D));
iw.addDocument(document);
}
}, count -> assertEquals(15L, count.getValue()));
}

public void testKeyWordField() throws IOException {
testCase(new MatchAllDocsQuery(), ValueType.STRING, iw -> {
for (int i = 0; i < 20; i++) {
Document document = new Document();
document.add(new SortedSetDocValuesField(FIELD_NAME, new BytesRef("stringValue")));
document.add(new SortedSetDocValuesField(FIELD_NAME, new BytesRef("string11Value")));
iw.addDocument(document);
}
}, count -> assertEquals(40L, count.getValue()));
}

public void testNoDocs() throws IOException {
for (ValueType valueType : ValueType.values()) {
testCase(new MatchAllDocsQuery(), valueType, iw -> {
Expand Down Expand Up @@ -239,7 +274,7 @@ public void testRangeFieldValues() throws IOException {
public void testValueScriptNumber() throws IOException {
ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name")
.field(FIELD_NAME)
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, NUMBER_VALUE_SCRIPT, Collections.emptyMap()));

MappedFieldType fieldType = createMappedFieldType(ValueType.NUMERIC);
fieldType.setName(FIELD_NAME);
Expand Down Expand Up @@ -288,7 +323,7 @@ public void testSingleScriptNumber() throws IOException {
public void testValueScriptString() throws IOException {
ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name")
.field(FIELD_NAME)
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, STRING_VALUE_SCRIPT, Collections.emptyMap()));

MappedFieldType fieldType = createMappedFieldType(ValueType.STRING);
fieldType.setName(FIELD_NAME);
Expand Down