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

Don't load global ordinals with the map execution_hint #37833

Merged
merged 5 commits into from
Feb 1, 2019
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 @@ -737,3 +737,74 @@ setup:
- is_false: aggregations.str_terms.buckets.1.key_as_string

- match: { aggregations.str_terms.buckets.1.doc_count: 2 }

---
"Global ordinals are not loaded with the map execution hint":

- skip:
version: " - 6.99.99"
reason: bug fixed in 7.0

- do:
index:
refresh: true
index: test_1
id: 1
routing: 1
body: { "str": "abc" }

- do:
index:
refresh: true
index: test_1
id: 2
routing: 1
body: { "str": "abc" }

- do:
index:
refresh: true
index: test_1
id: 3
routing: 1
body: { "str": "bcd" }

- do:
indices.refresh: {}

- do:
search:
index: test_1
body: { "size" : 0, "aggs" : { "str_terms" : { "terms" : { "field" : "str", "execution_hint" : "map" } } } }

- match: { hits.total.value: 3}
- length: { aggregations.str_terms.buckets: 2 }

- do:
indices.stats:
index: test_1
metric: fielddata
fielddata_fields: str

- match: { indices.test_1.total.fielddata.memory_size_in_bytes: 0}

- do:
search:
index: test_1
body: { "size" : 0, "aggs" : { "str_terms" : { "terms" : { "field" : "str", "execution_hint" : "global_ordinals" } } } }

- match: { hits.total.value: 3}
- length: { aggregations.str_terms.buckets: 2 }

- do:
indices.stats:
index: test_1
metric: fielddata
fielddata_fields: str

- gt: { indices.test_1.total.fielddata.memory_size_in_bytes: 0}





Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.search.aggregations.bucket.terms;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.IndexSearcher;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.logging.DeprecationLogger;
Expand Down Expand Up @@ -133,7 +134,7 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, Aggregator pare
if (valuesSource instanceof ValuesSource.Bytes.WithOrdinals == false) {
execution = ExecutionMode.MAP;
}
final long maxOrd = getMaxOrd(valuesSource, context.searcher());
final long maxOrd = getMaxOrd(context.searcher(), valuesSource, execution);
if (execution == null) {
execution = ExecutionMode.GLOBAL_ORDINALS;
}
Expand Down Expand Up @@ -207,13 +208,23 @@ static SubAggCollectionMode subAggCollectionMode(int expectedSize, long maxOrd)
}

/**
* Get the maximum global ordinal value for the provided {@link ValuesSource} or -1
* Get the maximum ordinal value for the provided {@link ValuesSource} or -1
* if the values source is not an instance of {@link ValuesSource.Bytes.WithOrdinals}.
*/
static long getMaxOrd(ValuesSource source, IndexSearcher searcher) throws IOException {
static long getMaxOrd(IndexSearcher searcher, ValuesSource source, ExecutionMode executionMode) throws IOException {
if (source instanceof ValuesSource.Bytes.WithOrdinals) {
ValuesSource.Bytes.WithOrdinals valueSourceWithOrdinals = (ValuesSource.Bytes.WithOrdinals) source;
return valueSourceWithOrdinals.globalMaxOrd(searcher);
if (executionMode == ExecutionMode.MAP) {
// global ordinals are not requested so we don't load them
// and return the biggest cardinality per segment instead.
long maxOrd = -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed below there's an assert for -1 down below, and in production with assertions off that'd fall through to a ratio that is under the threshold and triggers a LowCardinality strategy (potentially).

Is this just a sanity assertion, or is there an actual chance of having "no cardinality" in each segment and actually returning -1? What I'm basically asking is if we should handle -1 explicitly below or not :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The assertion is only for the global_ordinals execution and in such case we always call ValuesSource.Bytes.WithOrdinals#globalMaxOrd that returns values greater or equals than 0 so I think we're good here.

for (LeafReaderContext leaf : searcher.getIndexReader().leaves()) {
maxOrd = Math.max(maxOrd, valueSourceWithOrdinals.ordinalsValues(leaf).getValueCount());
}
return maxOrd;
} else {
return valueSourceWithOrdinals.globalMaxOrd(searcher);
}
} else {
return -1;
}
Expand Down Expand Up @@ -258,7 +269,7 @@ Aggregator create(String name,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {

final long maxOrd = getMaxOrd(valuesSource, context.searcher());
final long maxOrd = getMaxOrd(context.searcher(), valuesSource, ExecutionMode.GLOBAL_ORDINALS);
assert maxOrd != -1;
final double ratio = maxOrd / ((double) context.searcher().getIndexReader().numDocs());

Expand Down