在弹性搜索中对多个字段运行聚合

分享于2022年07月17日 elasticsearch elasticsearch-aggregation 问答
【问题标题】:在弹性搜索中对多个字段运行聚合(Run aggregation on multiple fields in elastic search)
【发布时间】:2022-01-25 11:32:02
【问题描述】:

我正在开发一个类似于 https://www.kijijiautos.ca 的二手车市场网站。 我想对我的数据集进行聚合,以获取例如“福特”的汽车数量,并且在同一聚合中,我想获取二手车、新车和其他规格的总数

我尝试了以下方法:

GET auto/_search
{
  "size": 0,
  "aggs": {
    "stats": {
      "multi_terms": {
        "terms": [{"field": "Make"}, {"field": "Type"}]
      }
    }
  }
}

我得到了以下结果

{
  "took" : 149,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "stats" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 14378,
      "buckets" : [
        {
          "key" : [
            "BMW",
            "Used"
          ],
          "key_as_string" : "BMW|Used",
          "doc_count" : 2826
        },
        {
          "key" : [
            "Volkswagen",
            "Used"
          ],
          "key_as_string" : "Volkswagen|Used",
          "doc_count" : 2592
        },
        {
          "key" : [
            "Audi",
            "Used"
          ],
          "key_as_string" : "Audi|Used",
          "doc_count" : 2310
        },
        {
          "key" : [
            "Opel",
            "Used"
          ],
          "key_as_string" : "Opel|Used",
          "doc_count" : 1494
        },
        {
          "key" : [
            "Ford",
            "Used"
          ],
          "key_as_string" : "Ford|Used",
          "doc_count" : 1485
        },
        {
          "key" : [
            "Renault",
            "Used"
          ],
          "key_as_string" : "Renault|Used",
          "doc_count" : 1303
        },
        {
          "key" : [
            "Peugeot",
            "Used"
          ],
          "key_as_string" : "Peugeot|Used",
          "doc_count" : 1196
        },
        {
          "key" : [
            "Fiat",
            "Used"
          ],
          "key_as_string" : "Fiat|Used",
          "doc_count" : 1149
        },
        {
          "key" : [
            "Skoda",
            "Used"
          ],
          "key_as_string" : "Skoda|Used",
          "doc_count" : 668
        },
        {
          "key" : [
            "SEAT",
            "Used"
          ],
          "key_as_string" : "SEAT|Used",
          "doc_count" : 629
        }
      ]
    }
  }
}

但这不是我所期待的 我期望的是以下内容:

{
  // first bucket
  [
    {
      "doc_count" : 123,
      "key" : "BMW"
    }
    // other makes here
  ]
  // second bucket
  [
    {
      "doc_count" : 2500,
      "key" : "Used"
    },
    {
      "doc_count" : 500,
      "key" : "New"
    }
  ]
}

如果这在 elasticsearch 中是可能的,那么请帮我写这个查询

谢谢


【解决方案1】:

我找到了我的问题的解决方案,我想把它留在这里以供参考。 (找到解决方案 here

这个想法是在 aggs 对象中指定多个聚合

GET auto/_search
{
  "size": 0,
  "aggs": {

    // aggregate on types
    "types": {
      "terms": {
        "field": "Type",
        "size": 10
      }
    },

    // aggregate on makes
    "makes": {
      "terms": {
        "field": "Make",
        "size": 10
      }
    },

    // aggregate on body types
    "body type": {
      "terms": {
        "field": "Body Type",
        "size": 10
      }
    }
  }
}

结果符合预期

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "types" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Used",
          "doc_count" : 22414
        },
        {
          "key" : "New",
          "doc_count" : 3021
        },
        {
          "key" : "Pre-registered",
          "doc_count" : 2071
        },
        {
          "key" : "Demonstration",
          "doc_count" : 1427
        },
        {
          "key" : "Employee's car",
          "doc_count" : 900
        },
        {
          "key" : "Antique / Classic",
          "doc_count" : 195
        },
        {
          "key" : "Automatic",
          "doc_count" : 1
        },
        {
          "key" : "Metallic",
          "doc_count" : 1
        }
      ]
    },
    "makes" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 9814,
      "buckets" : [
        {
          "key" : "Volkswagen",
          "doc_count" : 3295
        },
        {
          "key" : "BMW",
          "doc_count" : 3212
        },
        {
          "key" : "Audi",
          "doc_count" : 2768
        },
        {
          "key" : "Ford",
          "doc_count" : 2109
        },
        {
          "key" : "Opel",
          "doc_count" : 1840
        },
        {
          "key" : "Fiat",
          "doc_count" : 1692
        },
        {
          "key" : "Renault",
          "doc_count" : 1691
        },
        {
          "key" : "Peugeot",
          "doc_count" : 1458
        },
        {
          "key" : "Skoda",
          "doc_count" : 1193
        },
        {
          "key" : "SEAT",
          "doc_count" : 958
        }
      ]
    },
    "body type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Sedans",
          "doc_count" : 8410
        },
        {
          "key" : "Off-Road/Pick-up",
          "doc_count" : 6498
        },
        {
          "key" : "Station wagon",
          "doc_count" : 4894
        },
        {
          "key" : "Compact",
          "doc_count" : 3516
        },
        {
          "key" : "Van",
          "doc_count" : 1844
        },
        {
          "key" : "Transporter",
          "doc_count" : 1539
        },
        {
          "key" : "Coupe",
          "doc_count" : 1144
        },
        {
          "key" : "Convertible",
          "doc_count" : 1104
        },
        {
          "key" : "Other",
          "doc_count" : 654
        }
      ]
    }
  }
}