Skip to content

Meta data support with each aggregation request/response #8279

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 1 commit into from
Nov 3, 2014
Merged

Meta data support with each aggregation request/response #8279

merged 1 commit into from
Nov 3, 2014

Conversation

Mpdreamz
Copy link
Member

This commit adds the ability to associate a bit of state with each individual aggregation.

The aggregation response can be hard to stitch back together without having a reference to the aggregation request. In many cases this is not available, many json serializer frameworks cache types globally or have a static deserialisation override mechanism. In these cases making the original request available, if at all possible, would be a hack.

The old facets returned _type which was just enough metadata to know what the originating facet type in the request was.

This PR takes _type one step further by introducing ANY arbitrary meta data. This could be further abused for instance by generic/automated aggregations that include UI state (color information, thresholds, user input states, etc) per aggregation.

Here's a gist of a sense session you can use to review this branch

This PR supersedes #6465 which got stale and horribly out of date with all the new aggregations in 1.2-1.4.

Tested on a cluster of multiple nodes as well.

Example request:

"aggs": {
    "name": {
      "terms": {
        "field": "title"
      },
      "meta": { "number_of_clicks" : 2 },
      "aggs": {
        "viewport" : {
          "meta" : { 
              "hasColor" : true 
          },
          "geo_bounds" : {
            "field" : "location"
          }
        },
        "empty_agg" : {
          "meta" : { 
             "string" : "returns"
          },
          "sum": {
            "field": "i_do_not_exist"
          }
        },

Example response:

"aggregations": {
      "name": {
         "meta": {
            "number_of_clicks": 2
         },
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "banner",
               "doc_count": 1,
               "viewport": {
                  "meta": {
                     "hasColor": true
                  },
                  "bounds": {
                     "top_left": {
                        "lat": 28,
                        "lon": 21
                     },
       <redacted />

}
}
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The second method looks unnecessary? I mean you could just cast the result from readOptionalGenericValue? (null pointers can be casted to any class)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thats good to know (not the case in C# :)), since this method is called in a bunch of places i did not want to introduce to many nullchecks and casts. Will change it to casts on readOptionalGenericValue()

@jpountz
Copy link
Contributor

jpountz commented Nov 2, 2014

Hey @Mpdreamz the change looks good. The comment on the PR seems to suggest that any json document can be used as metadata (eg. a number) but the Java API requires a hash, is it intended?

@Mpdreamz
Copy link
Member Author

Mpdreamz commented Nov 2, 2014

Thanks for the review @jpountz !

No this is an oversight from copying it over from the previous PR that did allow any Json construct as meta, but now it has to be a hash (based on feedback on that PR).

@Mpdreamz
Copy link
Member Author

Mpdreamz commented Nov 3, 2014

@jpountz incorporated your feedback in the PR.

@@ -222,7 +222,8 @@
# Set a custom port to listen for HTTP traffic:
#
#http.port: 9200

http.cors.enabled: true
http.cors.allow-origin: http://localhost:8000
Copy link
Contributor

Choose a reason for hiding this comment

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

Left-over? :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Argh! good catch, i'm an idiot :)

@jpountz
Copy link
Contributor

jpountz commented Nov 3, 2014

I tried to look at how likely it would be to forget adding support to custom metadata, and I like the fact that you made the metadata a compulsory constructor argument of the aggregators, and that the parsing logic is shared. I think my only concern is that it would be easy to forget to read the metadata in the InternalAggregation objects, so maybe we could try to refactor InternalAggregation so that you could never forget to serialize/deserialize the metadata? I'm thinking about something like:

class InternalAggregation {

  public final void writeTo(StreamOutput out) { // final so that sub-classes cannot extend
    out.writeGenericValue(metaData);
    doWriteTo(out);
  }

  protected abstract void doWriteTo(StreamOutput out); // this is the method sub-classes should extend instead

}

(and similarly for readFrom)

@Mpdreamz
Copy link
Member Author

Mpdreamz commented Nov 3, 2014

++ I briefly discussed this with @spinscale but opted out for now to keep the PR simple, happy to refactor it! Should I also bump writing and reading the name to this method?

@jpountz
Copy link
Contributor

jpountz commented Nov 3, 2014

Should I also bump writing and reading the name to this method?

That would be great!

import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.Test;
import sun.security.util.BigInt;
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm?

@jpountz
Copy link
Contributor

jpountz commented Nov 3, 2014

I just left two minor comments, but other than that it looks good to me, so feel free to push without further reviews.

Can you update the example request in the description of the PR to replace 2 with a json document since a map is now enforced?

This commit adds the ability to associate a bit of state with each
individual aggregation.

The aggregation response can be hard to stitch back together without
having a reference to the aggregation request. In many cases this is not
available, many json serializer frameworks cache types globally or have a
static deserialisation override mechanism. In these cases making the
original request available, if at all possible, would be a hack.

The old facets returned `_type` which was just enough metadata to know
what the originating facet type in the request was.

This PR takes `_type` one step further by introducing ANY arbitrary meta
data. This could be further <strike>ab</strike>used for instance by
generic/automated aggregations that include UI state (color information,
thresholds, user input states, etc) per aggregation.
@Mpdreamz Mpdreamz merged commit 82278bb into elastic:master Nov 3, 2014
@javanna
Copy link
Member

javanna commented Nov 4, 2014

I see that this is marked for 1.5 as well but wasn't backported to 1.x. Is there any concern around backwards compatibility? Thoughts @jpountz @Mpdreamz ?

},
"meta": {
"color": "blue"
},
Copy link
Member

Choose a reason for hiding this comment

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

this "," can be removed ;)

@jpountz jpountz removed the v1.5.0 label Nov 4, 2014
@jpountz
Copy link
Contributor

jpountz commented Nov 4, 2014

I'd be happy either way: the change has the appropriate version checks to be compatible with 1.x releases. I'm removing the tag until the change is backported (if we backport it).

@clintongormley clintongormley changed the title [Aggregations] Meta data support Meta data support with each aggregation request/response Jun 7, 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.

4 participants