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

Added getProperty method to Aggregations #8421

Merged
merged 1 commit into from Nov 25, 2014
Merged

Added getProperty method to Aggregations #8421

merged 1 commit into from Nov 25, 2014

Conversation

colings86
Copy link
Contributor

This allows arbitrary properties to be retrieved from an aggregation tree. The property is specified using the same syntax as the
order parameter in the terms aggregation. If a property path contians a multi-bucket aggregation the property values from each bucket will be returned in an array.

} else if (aggName.equals("_key")) {
return getKey();
}
InternalAggregation aggregation = aggregations.get(aggName);
Copy link
Contributor

Choose a reason for hiding this comment

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

I am wondering if it makes sense to implement getProperty() for Aggregations as well and not just for Aggregation. For example in a test I would write something like

Aggregations agg = searchResponse.getAggregations();
Object o =agg.get("aggname").getProperty("path");

but if Aggregations also implemented getProperty() I would save another line and it is needed anyway here internally.

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 like this idea, and I can't think of a reason why it wouldn't work or not to do it. So I'll give it a go and add it to the PR.

@brwe
Copy link
Contributor

brwe commented Nov 10, 2014

Not sure if this is related to this pull requests, but when I name my aggregations with for example "a.b" then getProperty will fail:

 @Test
    public void testNestedBucketsWithGetProperty() throws IOException, ExecutionException, InterruptedException {
        indexDocs();
        String firstAgg = "a.b";
        String secondAgg = "c.d";
        SearchResponse searchResponse = client().prepareSearch("index").setQuery(matchAllQuery())
                .addAggregation(terms("class").field("class").subAggregation(histogram(firstAgg).field("num").interval(1).subAggregation(avg(secondAgg).field("num")).subAggregation(avg("max").field("num"))))
                .get();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(30l));

        Aggregations agg = searchResponse.getAggregations();
        Object o = agg.get("class").getProperty(firstAgg + "." + secondAgg);
    }

    private void indexDocs() throws IOException, ExecutionException, InterruptedException {
        List<IndexRequestBuilder> indexRequests = new ArrayList<>();
        for (int i = 0; i< 3; i++) {
            for (int j = 0; j< 10; j++) {
                indexRequests.add(client().prepareIndex().setSource(jsonBuilder().startObject().field("class", Integer.toString(i)).field("num", j).endObject()).setIndex("index").setType("type"));
            }
        }
        indexRandom(true, indexRequests);
        ensureGreen("index");
    }

@colings86
Copy link
Contributor Author

@brwe I was talking to @clintongormley about this the other day. This will need to be fixed but outside of this PR as it also affects sort order for aggregations. For example, currently you cannot order a terms aggregation using the 99.9th percentile from the percentiles aggregation as you would need to specify your order as percents.99.9 which would be interpreted as an aggregation named percents.99 and a value named 9.

@@ -30,6 +30,8 @@
*/
String getName();

Object getProperty(String path);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some javadocs since it's a public API?

@jpountz
Copy link
Contributor

jpountz commented Nov 10, 2014

I like this new API as it is very convenient. I left a couple of comments/questions.

I know it's outside of the scope of this PR but I'm wondering if we should make the path syntax compatible with some standard like json path for the next major release, in a way that it would be compatible with calling the same json path expression on the json response from elasticsearch. In addition to making it easier to approach to new users, it would also allow to address data that would be stored in a specific bucket, which is not supported today (although I could imagine sorting or reducers make use of it)?

@colings86
Copy link
Contributor Author

I like the idea of supporting JSON path expressions. From looking at the spec for JSON path, we are not that far away in what we do already, although we need to replace the '>' character with '.' in our current syntax. One thing we would need to sort out is what happens if the aggregation name contains a dot. This currently causes issues and we need to come up with a resolution order for resolving names and values with a dot, or remove support for agg names with a dot.

@colings86
Copy link
Contributor Author

Added a new issue for the JSONPath suggest here: #8434

@colings86
Copy link
Contributor Author

@brwe @jpountz thanks for reviewing, I have addressed/replied to your comments and pushed a new commit. Would you mind taking another look?

@@ -392,6 +396,9 @@ public void singleValuedField_WithSubAggregation() throws Exception {
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
assertThat(range.getBuckets().size(), equalTo(3));
Object[] propertiesKeys = (Object[]) range.getProperty("_key");
Object[] propertiesDocCounts = (Object[]) range.getProperty("_count");
Object[] propertiesCounts = (Object[]) range.getProperty("sum");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be Object[] propertiesCounts = (Object[]) histo.getProperty("sum.value"); else the tests don't pass.

} else {
String aggName = path.get(0);
if (aggName.equals("_count")) {
return getDocCount();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you check that there are no more elements in the path before returning?

@jpountz
Copy link
Contributor

jpountz commented Nov 25, 2014

LGTM

@colings86 colings86 removed the review label Nov 25, 2014
This allows arbitrary properties to be retrieved from an aggregation tree. The property is specified using the same syntax as the
order parameter in the terms aggregation. If a property path contians a multi-bucket aggregation the property values from each bucket will be returned in an array.
@colings86 colings86 merged commit c420a17 into elastic:master Nov 25, 2014
@colings86 colings86 deleted the feature/aggsGetProperty branch November 25, 2014 10:56
@clintongormley clintongormley changed the title Aggregations: Added getProperty method to Aggregations Added getProperty method to Aggregations Jun 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants