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

Adds metadata to rewritten aggregations #28185

Merged
merged 2 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -101,7 +101,7 @@ public final AggregationBuilder rewrite(QueryRewriteContext context) throws IOEx
if (rewritten == this) {
return rewritten;
}
if (getMetaData() != null && rewritten.getMetaData() == null) {
if (getMetaData() != null && (rewritten.getMetaData() == null || rewritten.getMetaData().isEmpty())) {
rewritten.setMetaData(getMetaData());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

what prevents us from always blindly setting the metadata? Rewriting should never modify it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point. I'll change it to do this since it simplifies things

AggregatorFactories.Builder rewrittenSubAggs = factoriesBuilder.rewrite(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FiltersAggsRewriteIT extends ESSingleNodeTestCase {

Expand All @@ -58,10 +60,14 @@ public void testWrapperQueryIsRewritten() throws IOException {
}
FiltersAggregationBuilder builder = new FiltersAggregationBuilder("titles", new FiltersAggregator.KeyedFilter("titleterms",
new WrapperQueryBuilder(bytesReference)));
Map<String, Object> metadata = new HashMap<>();
metadata.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
builder.setMetaData(metadata);
SearchResponse searchResponse = client().prepareSearch("test").setSize(0).addAggregation(builder).get();
assertEquals(3, searchResponse.getHits().getTotalHits());
InternalFilters filters = searchResponse.getAggregations().get("titles");
assertEquals(1, filters.getBuckets().size());
assertEquals(2, filters.getBuckets().get(0).getDocCount());
assertEquals(metadata, filters.getMetaData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter;

import java.io.IOException;
import java.util.Collections;

import static org.hamcrest.Matchers.instanceOf;

Expand Down Expand Up @@ -123,6 +124,7 @@ public void testOtherBucket() throws IOException {
public void testRewrite() throws IOException {
// test non-keyed filter that doesn't rewrite
AggregationBuilder original = new FiltersAggregationBuilder("my-agg", new MatchAllQueryBuilder());
original.setMetaData(Collections.singletonMap(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)));
AggregationBuilder rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertSame(original, rewritten);

Expand Down