Bucket Sorting by Metrics in Elastic 2.x

Hi All,

I have an index containing 2 index types. I am trying to create buckets in the disease_locus index_type (3 docs) based on a link to the hits index_type (12 docs).

I am aggregating the hits documents based on a term that links these docs to their "parent" doc (not an official parent-child doc relationship) in the disease_locus index and I then want to sort the buckets that are returned but a min value in the bucket, which is filtered from a nested document in the hits. This worked in 1.7 but since upgrading to 2.x I cannot get the query to work, I get the following error...

Invalid terms aggregation order path [build_filter>filtered_result>locus_start.value]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end. Sub-path [build_filter] points to non single-bucket aggregation

I can provide mapping and data to replicate the error but the character limit on this post means I wasn't able to upload.

Query that I am trying to run which throws the error...

GET /regions_test/_search?size=0&pretty
{"query":{"bool":{"must":[{"nested":{"query":{"term":{"build_info.build":38}},"path":"build_info"}}]}},"aggregations":{"build_info":{"aggs":{"seq_hits":{"terms":{"field":"build_info.seqid","size":0},"aggs":{"disease_hits":{"reverse_nested":{},"aggs":{"diseases_by_seqid":{"terms":{"field":"disease_locus","size":0,"order":{"build_filter>filtered_result>locus_start":"asc"}},"aggs":{"build_filter":{"aggs":{"filtered_result":{"filter":{"query":{"match":{"build_info.build":38}}},"aggs":{"locus_start":{"min":{"field":"build_info.start"}}}}},"nested":{"path":"build_info"}}}}}}}}},"nested":{"path":"build_info"}}}}

If anybody could point out why my order":{"build_filter>filtered_result>locus_start":"asc"} isn't working I would be most grateful.

Yours,
Ellen.

Mapping for index..

["{"regions_latest":{"mappings":{"disease_locus":{"properties":{"disease":{"type":"string"},"hits":{"type":"string"},"locus_id":{"type":"string","index":"not_analyzed"},"region":{"type":"string","index":"not_analyzed"},"seqid":{"type":"string"}}},"hits":{"properties":{"build_info":{"type":"nested","properties":{"build":{"type":"integer"},"end":{"type":"integer"},"seqid":{"type":"string"},"start":{"type":"integer"}}},"disease":{"type":"string"},"disease_locus":{"type":"string","index":"not_analyzed"}}}}}}"]

Test data for hits index_type...

[{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jrt9ou-fWOUOsy","_score":0,"_source":{"disease":"T1D","disease_locus":"T1D_10001","build_info":[{"build":38,"start":5988280,"end":6136651,"seqid":"10"},{"build":37,"start":6030243,"end":6178614,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jwt9ou-fWOUO7b","_score":0,"_source":{"disease":"T1D","disease_locus":"T1D_10001","build_info":[{"build":38,"start":5988280,"end":6128337,"seqid":"10"},{"build":37,"start":6030243,"end":6170300,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jyt9ou-fWOUPDq","_score":0,"_source":{"disease":"MS","disease_locus":"MS_10001","build_info":[{"build":38,"start":5988280,"end":6134149,"seqid":"10"},{"build":37,"start":6030243,"end":6176112,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jwt9ou-fWOUO-K","_score":0,"_source":{"disease":"MS","disease_locus":"MS_10001","build_info":[{"build":38,"start":5988280,"end":6129961,"seqid":"10"},{"build":37,"start":6030243,"end":6171924,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jst9ou-fWOUOt-","_score":0,"_source":{"disease":"MS","disease_locus":"MS_10001","build_info":[{"build":38,"start":5988280,"end":6134149,"seqid":"10"},{"build":37,"start":6030243,"end":6176112,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jrt9ou-fWOUOr5","_score":0,"_source":{"disease":"T1D","disease_locus":"T1D_10001","build_info":[{"build":38,"start":5988280,"end":6134149,"seqid":"10"},{"build":37,"start":6030243,"end":6176112,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jwt9ou-fWOUO7d","_score":0,"_source":{"disease":"T1D","disease_locus":"T1D_10001","build_info":[{"build":38,"start":5988280,"end":6132638,"seqid":"10"},{"build":37,"start":6030243,"end":6174601,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jrt9ou-fWOUOsz","_score":0,"_source":{"disease":"T1D","disease_locus":"T1D_10002","build_info":[{"build":38,"start":6388017,"end":6503142,"seqid":"10"},{"build":37,"start":6429979,"end":6545104,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jyt9ou-fWOUPDp","_score":0,"_source":{"disease":"MS","disease_locus":"MS_10001","build_info":[{"build":38,"start":5988280,"end":6131453,"seqid":"10"},{"build":37,"start":6030243,"end":6173416,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","id":"AVGGe7jst9ou-fWOUOt","_score":0,"_source":{"disease":"MS","disease_locus":"MS_10001","build_info":[{"build":38,"start":5988280,"end":6131453,"seqid":"10"},{"build":37,"start":6030243,"end":6173416,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jwt9ou-fWOUO7e","_score":0,"_source":{"disease":"T1D","disease_locus":"T1D_10001","build_info":[{"build":38,"start":6035365,"end":6146375,"seqid":"10"},{"build":37,"start":6077328,"end":6188338,"seqid":"10"}]}}
,{"_index":"regions_latest","_type":"hits","_id":"AVGGe7jyt9ou-fWOUPDC","_score":0,"_source":{"disease":"T1D","disease_locus":"T1D_10001","build_info":[{"build":38,"start":5988280,"end":6136651,"seqid":"10"},{"build":37,"start":6030243,"end":6178614,"seqid":"10"}]}}]

Test data for disease_locus index_type...

[{"_index":"regions_latest","_type":"disease_locus","_id":"T1D_10002","_score":0,"_source":{"hits":["AVGGe7jrt9ou-fWOUOsz"],"disease":"T1D","locus_id":"T1D_10002"}}
,{"_index":"regions_latest","_type":"disease_locus","_id":"MS_10001","_score":0,"source":{"hits":["AVGGe7jst9ou-fWOUOt-","AVGGe7jwt9ou-fWOUO-K","AVGGe7jyt9ou-fWOUPDq","AVGGe7jst9ou-fWOUOt","AVGGe7jyt9ou-fWOUPDp"],"disease":"MS","locus_id":"MS_10001"}}
,{"_index":"regions_latest","_type":"disease_locus","_id":"T1D_10001","_score":0,"_source":{"hits":["AVGGe7jrt9ou-fWOUOr5","AVGGe7jrt9ou-fWOUOsy","AVGGe7jwt9ou-fWOUO7b","AVGGe7jwt9ou-fWOUO7d","AVGGe7jyt9ou-fWOUPDC","AVGGe7jwt9ou-fWOUO7e"],"disease":"T1D","locus_id":"T1D_10001"}}]