Elasticsearch group and aggregate nested values -


i want in 1 request data build this:

categories:  - laptops (5)  - accessories (50)  - monitors (10)  -- above part easy --  attributest actual category ex. laptops:  - card reder:   - mmc (1)   - sd (5)  - resolution:   - 1024x768 (2)   - 2048x1536 (3) 

first make mapping on elasticsearch this:

{     "mappings": {     "product": {         "properties": {             "name": {                 "type": "string"             },             "categoryname": {                 "type": "string",                "index": "not_analyzed"             },             "pricebrutto": {                 "type": "float"             },             "categorycode": {                 "type": "integer"             },             "productattributefields" : {                 "properties" : {                     "name" : {                         "index" : "not_analyzed",                         "type" : "string"                     },                     "value" : {                         "index" : "not_analyzed",                         "type" : "string"                     }                 }             }          }       }    } } 

then add objects looks below. in productattributefields many attributes. if laptop has many ports, every port array in productattributefields.

array (     [name] => macbook pro     [categorycode] => 123     [categoryname] => notebooks     [pricebrutto] => 1500     [productattributefields] => array         (             [0] => array                 (                     [name] => resolution                     [value] => 2048x1536                 )              [1] => array                 (                     [name] => memory readers                     [value] => mmc                 )             [2] => array                 (                     [name] => memory readers                     [value] => sd                 )         ) ) 

now want result this:

array (     [took] => 132     [timed_out] =>      [_shards] => array         (             [total] => 1             [successful] => 1             [failed] => 0         )      [hits] => array         (             [total] => 631             [max_score] => 0             [hits] => array                 (                 )          )      [aggregations] => array         (             [attrs] => array                 (                     [doc_count_error_upper_bound] => 0                     [sum_other_doc_count] => 4608                     [buckets] => array                         (                             [0] => array                                 (                                     [key] => resolution                                     [doc_count] => 619                                     [attrsvalues] => array                                         (                                             [doc_count_error_upper_bound] => 0                                             [sum_other_doc_count] => 14199                                             [buckets] => array                                                 (                                                     [0] => array                                                         (                                                             [key] => 2048x1536                                                             [doc_count] => 123                                                         )                                                      [1] => array                                                         (                                                             [key] => 1024x768                                                             [doc_count] => 3                                                         )                                                  )                                          )                                  )                              [1] => array                                 (                                     [key] => memory readers                                     [doc_count] => 618                                     [wartosci] => array                                         (                                             [doc_count_error_upper_bound] => 0                                             [sum_other_doc_count] => 14185                                             [buckets] => array                                                 (                                                     [0] => array                                                         (                                                             [key] => mmc                                                             [doc_count] => 431                                                         )                                                      [1] => array                                                         (                                                             [key] => sd                                                             [doc_count] => 430                                                         )                                                  )                                          )                                  )                          )                  )         ) ) 

i'm close solving problem (below query), in second level aggregation have of values (ex. in "resolution" have 2048x1536, mmc , sd). want have in "resolution" "2048x1536", "1024x768" , other values has key "resolution", on "card readers" "mmc", "sd" , other values has key "card readers".

'body' => [     'query' => [         'match' => [             categorycode = 123         ],     ],     'aggs' => [         'attrs' => [             'terms' => [                 'field' => 'productattributefields.name',             ],             'aggs' => [                 'attrsvalues' => [                     'terms' => [                         'field' => 'productattributefields.value',                         'size' => 100,                     ],                 ],             ],         ],     ], ] 

you need change mapping , make productattributefields nested field can retain association between productattributefields.name , productattributefields.value.

the mapping should this:

{   "mappings": {     "product": {       "properties": {         "name": {           "type": "string"         },         "categoryname": {           "type": "string",           "index": "not_analyzed"         },         "pricebrutto": {           "type": "float"         },         "categorycode": {           "type": "integer"         },         "productattributefields": {           "type": "nested",           "include_in_parent": true,            "properties": {             "name": {               "index": "not_analyzed",               "type": "string"             },             "value": {               "index": "not_analyzed",               "type": "string"             }           }         }       }     }   } } 

and query changes to

{   "query": {     "match": {       "categorycode": 123     }   },   "aggs": {     "attrs_root": {       "nested": {         "path": "productattributefields"       },       "aggs": {         "attrs": {           "terms": {             "field": "productattributefields.name"           },           "aggs": {             "attrsvalues": {               "terms": {                 "field": "productattributefields.value",                 "size": 100               }             }           }         }       }     }   } } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -