elasticsearch 筛选后显示相同类别的聚合

ncgqoxb0  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(2)|浏览(143)

我想为Elasticsearch中的产品创建过滤器和过滤器后的聚合。我有所有产品的基本聚合:

"size": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 87,
        "buckets": [
            {
                "key": "6",
                "doc_count": 89
            },
            {
                "key": "5,5",
                "doc_count": 60
            }
        ]
    }
},
"brand": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 87,
        "buckets": [
            {
                "key": "Apple",
                "doc_count": 89
            },
            {
                "key": "Samsung",
                "doc_count": 60
            },
            {
                "key": "Xiaomi",
                "doc_count": 48
            },
            {
                "key": "Huawei",
                "doc_count": 33
            }
        ]
    }
}

在我查询了其中一个品牌和尺寸后,如:

query": {
      "bool": {
        "filter": [
          "term": {
            "brand": "Samsung"
          },
          "term": {
            "size": "6"
          }
        ]
      }
    }

我只取回选定brand的聚合。但我仍希望在聚合中看到具有相同大小的所有其他brand
ES是否可以做到这一点?
非常感谢您的回答。

mm5n2pyu

mm5n2pyu1#

我不太明白你在找什么。
您可以尝试以下查询(按品牌进行术语聚合,然后按大小进行子聚合):

{
    "size": 0,
    "query": {
        "match_all": {}
    },
    "aggs": {
        "byBrand": {
            "terms": {
                "field": "brand"
            },
            "aggs": {
                "bySize": {
                    "terms": {
                        "field": "size"
                    }
                }
            }
        }
    }
}

否则,您可以先按大小筛选,然后再按品牌聚合:

{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "size": "6"
                }
            }
        }
    },
    "size": 0,
    "aggs": {
        "byBrand": {
            "terms": {
                "field": "brand"
            }
        }
    }
}

如果这对您没有帮助,请将您预期的输出添加到问题中。

k3bvogb1

k3bvogb12#

有多种方法
1.使用globalfilter aggregation
全局聚合-查找所有文档,即使是按查询筛选的文档。

查询

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "brand.keyword": "Samsung"
          }
        },
        {
          "term": {
            "size": "6"
          }
        }
      ]
    }
  },
  "aggs": {
    "all_brands": {
      "global": {}, --> refer all documents
      "aggs": {
        "size_filter": { --> filter size 6
          "filter": {
            "term": {
              "size": 6
            }
          },
          "aggs": {
            "all_brands_terms": {
              "terms": {
                "field": "brand.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

结果

hits" : [
      {
        "_index" : "index67",
        "_type" : "_doc",
        "_id" : "FyERPYQBJutE-yZcbYF4",
        "_score" : 0.0,
        "_source" : {
          "brand" : "Samsung",
          "size" : 6
        }
      }
    ]
  },
  "aggregations" : {
    "all_documents" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Apple",
          "doc_count" : 1
        },
        {
          "key" : "Samsung",
          "doc_count" : 1
        }
      ]
    }
  }

1.后置过滤器(_F)
当您使用post_filter参数来筛选搜寻结果时,会在计算汇总之后筛选搜寻结果。后置筛选对汇总结果没有影响。

查询

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "size": "6"
          }
        }
      ]
    }
  },
  "aggs": {
    "all_documents": {
      "terms": {
        "field": "brand.keyword",
        "size": 10
      }
    }
  },
  "post_filter": {
    "term": {
      "brand.keyword": "Samsung"
    }
  }
}

结果

hits" : [
      {
        "_index" : "index67",
        "_type" : "_doc",
        "_id" : "FyERPYQBJutE-yZcbYF4",
        "_score" : 0.0,
        "_source" : {
          "brand" : "Samsung",
          "size" : 6
        }
      }
    ]
  },
  "aggregations" : {
    "all_documents" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Apple",
          "doc_count" : 1
        },
        {
          "key" : "Samsung",
          "doc_count" : 1
        }
      ]
    }
  }

相关问题