elasticsearch - Elastic Search Aggregation on Array Items -


given below elastic document on want fire aggregation query.

{     "id": 1,     "attributes":[     {         "fieldid": 1,         "value": "male"     },     {         "fieldid": 2,         "value": "12/11/2015"     } }  {     "id": 2,     "attributes":[     {         "fieldid": 1,         "value": "male"     },     {         "fieldid": 2,         "value": "11/11/2015"     } } 

the result has follows.

[     {         "key" : "male",         "doc_count" : 1     } ]  [     {         "key" : "12/11/2015",         "doc_count" : 1     },     {         "key" : "11/11/2015",         "doc_count" : 1     } ] 

is there way can achieved in elastic search.

that's possible. see example:

we have map attributes nested type able aggregate properly.

put /test {   "mappings": {     "sample": {       "properties": {         "id": {           "type": "integer"         },         "attributes": {           "type": "nested",           "properties": {             "fieldid": {               "type": "integer"             },             "value": {               "type": "string",               "index": "not_analyzed"             }           }         }       }     }   } } 

let's add given test data:

put /test/sample/1 {"id":1,"attributes":[{"fieldid":1,"value":"male"},{"fieldid":2,"value":"12/11/2015"}]} put /test/sample/2 {"id":2,"attributes":[{"fieldid":1,"value":"male"},{"fieldid":2,"value":"11/11/2015"}]} 

and let's run query:

get /test/_search {   "size": 0,   "query": {     "match_all": {}   },   "aggs": {     "nest": {       "nested": {         "path": "attributes"       },       "aggs": {         "fieldids": {           "terms": {             "field": "attributes.fieldid",             "size": 0           },           "aggs": {             "values": {               "terms": {                 "field": "attributes.value",                 "size": 0               }             }           }         }       }     }   } } 

what do?

  1. run nested aggregation first in order nested objects , aggregate them properly.
  2. create buckets using terms aggregation each fieldid, in case we'll 2 of them: 1 , 2.
  3. run terms aggregation again each of buckets above in order coresponding values.

so that's output.

{   "took": 2,   "timed_out": false,   "_shards": {     "total": 5,     "successful": 5,     "failed": 0   },   "hits": {     "total": 2,     "max_score": 0,     "hits": []   },   "aggregations": {     "nest": {       "doc_count": 4,       "fieldids": {         "doc_count_error_upper_bound": 0,         "sum_other_doc_count": 0,         "buckets": [           {             "key": 1,             "doc_count": 2,             "values": {               "doc_count_error_upper_bound": 0,               "sum_other_doc_count": 0,               "buckets": [                 {                   "key": "male",                   "doc_count": 2                 }               ]             }           },           {             "key": 2,             "doc_count": 2,             "values": {               "doc_count_error_upper_bound": 0,               "sum_other_doc_count": 0,               "buckets": [                 {                   "key": "11/11/2015",                   "doc_count": 1                 },                 {                   "key": "12/11/2015",                   "doc_count": 1                 }               ]             }           }         ]       }     }   } } 

it's not precisely you've requested. that's closest can in elasticsearch.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -