Skip to content

Cannot do min aggregation on nested fields #26531

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

Closed
Burgov opened this issue Sep 7, 2017 · 2 comments
Closed

Cannot do min aggregation on nested fields #26531

Burgov opened this issue Sep 7, 2017 · 2 comments
Labels
:Analytics/Aggregations Aggregations >non-issue :Search/Search Search-related issues that do not fall into other categories

Comments

@Burgov
Copy link

Burgov commented Sep 7, 2017

Elasticsearch version (bin/elasticsearch --version):
Tried on both 5.2.0 and 5.5.0

Plugins installed: [xpack]

JVM version (java -version):
1.8.0_91

OS version (uname -a if on a Unix-like system):
Linux x 4.4.0-81-generic #104-Ubuntu SMP Wed Jun 14 08:17:06 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Description of the problem including expected versus actual behavior:
When doing a min/max aggregation on a nested field, rather than getting the expected value, you get null

Steps to reproduce:

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "doc": {
         "properties": {
            "entries": {
               "type": "nested",
               "properties": {
                  "name": {
                     "type": "keyword"
                  },
                  "position": {
                     "type": "long"
                  }
               }
            },
            "id": {
               "type": "keyword"
            },
            "version": {
               "type": "long"
            }
         }
      }
   }
}

PUT /test_index/doc/_bulk
{"index":{"_id":1}}
{"id":"i1","version":1,"entries":[{"name":"n1","position":1},{"name":"n2","position":2}]}
{"index":{"_id":2}}
{"id":"i1","version":2,"entries":[{"name":"n2","position":3},{"name":"n3","position":4}]}
{"index":{"_id":3}}
{"id":"i2","version":1,"entries":[{"name":"n1","position":8},{"name":"n2","position":7}]}
{"index":{"_id":4}}
{"id":"i2","version":2,"entries":[{"name":"n2","position":6},{"name":"n3","position":5}]}

POST /test_index/_search
{
   "aggs": {
      "id_terms": {
         "terms": {
            "field": "id"
         },
         "aggs": {
            "nested_entries": {
               "nested": {
                  "path": "entries"
               },
               "aggs": {
                  "filter_name": {
                     "filter": {
                        "term": {
                           "entries.name": "n2"
                        }
                     },
                     "aggs": {
                        "min_position": {
                           "min": {
                              "field": "position"
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

expected output:

{
    // snip
   "aggregations": {
      "id_terms": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "i1",
               "doc_count": 2,
               "nested_entries": {
                  "doc_count": 4,
                  "filter_name": {
                     "doc_count": 2,
                     "min_position": {
                        "value": 2,
                        "value_as_string": "2.0"
                     }
                  }
               }
            },
            {
               "key": "i2",
               "doc_count": 2,
               "nested_entries": {
                  "doc_count": 4,
                  "filter_name": {
                     "doc_count": 2,
                     "min_position": {
                        "value": 6,
                        "value_as_string": "6.0"
                     }
                  }
               }
            }
         ]
      }
   }
}

actual output:

{
    // snip
   "aggregations": {
      "id_terms": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "i1",
               "doc_count": 2,
               "nested_entries": {
                  "doc_count": 4,
                  "filter_name": {
                     "doc_count": 2,
                     "min_position": {
                        "value": null
                     }
                  }
               }
            },
            {
               "key": "i2",
               "doc_count": 2,
               "nested_entries": {
                  "doc_count": 4,
                  "filter_name": {
                     "doc_count": 2,
                     "min_position": {
                        "value": null
                     }
                  }
               }
            }
         ]
      }
   }
}
@costin
Copy link
Member

costin commented Sep 7, 2017

You need to specify the full path of the nested field just its name in a similar fashion to filter in filter_name: "field": "position" -> "field": "entries.position"

Hth

@Burgov
Copy link
Author

Burgov commented Sep 7, 2017

@costin ha, you're right, i missed it in my example, and when I add it, it works. However, I have a case where it doesn't work even though I do add that path part. Thought I had produced a minimum case for reproducing it, but apparently I didn't... back to the drawing table.

@Burgov Burgov closed this as completed Sep 7, 2017
@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Nested Docs labels Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/Aggregations Aggregations >non-issue :Search/Search Search-related issues that do not fall into other categories
Projects
None yet
Development

No branches or pull requests

3 participants