嵌套查询中数组的聚合

Aggregations on an array in a nested query

我正在尝试查询与特定用户至少有一种颜色相同的所有用户,我已经能够做到这一点,但是我无法弄清楚如何 aggregate 我的结果我可以获得用户以及他们共有的颜色。

我的示例用户文档的一部分如下:

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

这是我的查询,用于获取与具有红色、橙色和绿色的其他用户相同的颜色:

{
  "query": {
    "nested": {
      "path": "colors",
      "scoreMode": "sum",
      "query": {
        "function_score": {
          "filter": {
            "terms": {
              "colors.name": [
                "Red","Orange","Green"
              ]
            }
          },
          "functions": [
            // Functions here for custom scoring
          ]
        }
      }
    }
  }
}

如何聚合具有共同颜色的用户

您需要使用nested aggregation, then apply filter aggregation for colors and finally use top hits to get the matching colors. I am using source filtering来获取颜色值

这是查询

{
  "size": 0,
  "query": {
    "nested": {
      "path": "colors",
      "query": {
        "terms": {
          "colors.color": [
            "green",
            "red"
          ]
        }
      }
    }
  },
  "aggs": {
    "user": {
      "terms": {            <----get users with unique name or user_id
        "field": "name",
        "size": 10
      },
      "aggs": {
        "nested_color_path": {  <---go inside nested documents
          "nested": {
            "path": "colors"
          },
          "aggs": {
            "match_color": {
              "filter": {         <--- use the filter to match for colors
                "terms": {
                  "colors.color": [
                    "green",
                    "red"
                  ]
                }
              },
              "aggs": {
                "get_match_color": {  <--- use this to get matched color
                  "top_hits": {
                    "size": 10,
                     "_source": {
                       "include": "name"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

您必须使用 nested aggregations 来实现此目的。请参阅以下查询:

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"  <-- This will sort the 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
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}