elasticsearch - Aggregations on an array in a nested query -


i trying query users have @ least 1 color in common particular user , have been able unable figure out how aggregate results can user along colors have in common.

part of document sample user follows:

{     // ... other fields     "colors" : [         {             "id" : 1,             "name" : "green"         },         {             "id" : 7,             "name" : "blue"         }     ] } 

this query getting colors in common user has colors red, orange , green:

{   "query": {     "nested": {       "path": "colors",       "scoremode": "sum",       "query": {         "function_score": {           "filter": {             "terms": {               "colors.name": [                 "red","orange","green"               ]             }           },           "functions": [             // functions here custom scoring           ]         }       }     }   } } 

how can aggregate users colors in common?

you have use nested aggregations achieve this. see query below:

post <index>/<type>/_search {    "query": {       "nested": {          "path": "colors",          "query": {             "terms": {                "colors.name": [                   "red",                   "orange",                   "green"                ]             }          }       }    },    "aggs": {       "users_with_common_colors": {          "terms": {             "field": "user_id",             "size": 0,             "order": {                 "color_distribution>common": "desc"  <-- sort users in descending order of number of common colors             }          },          "aggs": {             "color_distribution": {                "nested": {                   "path": "colors"                },                "aggs": {                   "common": {                      "filter": {                         "terms": {                            "colors.name": [                               "red",                               "orange",                               "green"                            ]                         }                      },                      "aggs": {                         "colors": {                            "terms": {                               "field": "colors.name",                               "size": 0                            }                         }                      }                   }                }             }          }       }    } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -