【问题标题】:How to aggregate on number of returned results in ElasticSearch?如何在 ElasticSearch 中汇总返回结果的数量?
【发布时间】:2016-02-06 18:21:58
【问题描述】:

仅供参考,我在 Mac 中使用 ES 1.7.2。

我一直在尝试获取结果集的聚合,但我得到的是所有记录的聚合。

假设我想退回 200 辆福特福克斯 SE 车辆,从这 200 辆汽车中,我想知道所有这些车辆的内饰以及每个内饰有多少辆车。所以基本上计算了那些修剪,但也得到了 200 个结果。

这是我目前所拥有的(我正在使用 Sense/Marvel...更容易测试):

GET jdbc/_search
{
  "size": 200, 
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            { "term": { "listing.model": "Focus"   }},
            { "term": { "listing.make": "Ford" }}
          ]
        }
      }
    }
  },
  "aggs": {
    "trims": {
      "terms": { "field": "listing.trim" }
    },
    "trim_SE": {
      "filter": {
        "term": { "listing.trim": "SE" }
      },
    "aggs": {
      "trims": {
        "terms": { "field": "listing.trim"}
        }
      }
    }
  },
  "post_filter": { 
    "term": { "listing.trim": "SE" }
  }
}

所以我确实得到了 20 个结果,如下所示:

{
   "took": 18,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 10338,
      "max_score": 1,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mytype",
            "_id": "472",
            "_score": 1,
            "_source": {
               "listing": {
                  "vin": "111111111111",
                  "year": 2013,
                  "make": "Ford",
                  "model": "Focus",
                  "trim": "SE",
               }
            }
         },
         {...}
      ]
   }
   "aggregations": {
      "trims": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 30,
         "buckets": [
            {
               "key": "SE",
               "doc_count": 10338
            },
            {
               "key": "SEL",
               "doc_count": 1000
                },
            {
               "key": "Titanium",
               "doc_count": 874
            },
            {
               "key": "SES",
               "doc_count": 585
            },
            {
               "key": "S",
               "doc_count": 554
            },
            {
               "key": "",
               "doc_count": 447
            },
            {
               "key": "ST",
               "doc_count": 339
            },
            {
               "key": "ZTS",
               "doc_count": 60
            },
            {
               "key": "LX",
               "doc_count": 56
            },
            {
               "key": "Electric",
               "doc_count": 18
            }
         ]
      },
      "trim_SE": {
         "doc_count": 10338,
         "trims": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "SE",
                  "doc_count": 10338
               }
            ]
         }
      }
   }
}

但正如您所见,Trims 显示的计数比 200 多,这意味着它正在对所有车辆进行聚合。

我需要一些帮助,但我找不到任何能真正使这项工作正常进行的东西。

提前感谢您的帮助!

【问题讨论】:

  • 以后不要犹豫,使用found.no/play创建一个可复制的案例。这是分享弹性搜索问题的最佳工具。
  • 超级有趣的工具!感谢分享!我一定会仔细看看。

标签: search lucene search-engine elasticsearch


【解决方案1】:

您应该将查询中的相同过滤器添加到聚合中。 请注意,我只为方面“修剪”创建了示例:

{
    "aggs": {
        "trims": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "listing.model": "Focus"
                            }
                        },
                        {
                            "term": {
                                "listing.make": "Ford"
                            }
                        }
                    ]
                }
            },
            "aggs": {
                "trims": {
                    "terms": {
                        "field": "listing.trim"
                    }
                }
            }
        }
    }
}

我不确定你是否理解了你对 post_filter 的使用,但如果需要,你可以将它添加到聚合的“过滤器”部分。

来源:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html

【讨论】:

  • 感谢您的快速回复!不幸的是,它不起作用。也许我遇到了一些数据问题。这是我返回的错误:SearchParseException[[jdbc][4]: query[ConstantScore(BooleanFilter(+cache(listing.model:Focus) +cache(listing.make:Ford)))],from[0],size[200]: Parse Failure [Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [trims]]];
  • 对,我已经更新了代码以匹配术语聚合的文档 (elastic.co/guide/en/elasticsearch/reference/current/…)。
  • 再次感谢,但我想我没有说清楚。我希望根据指定的结果大小显示聚合和结果。例如,如果有 200,000 个点击,但我只想显示 200 个,那么聚合应该在这 200 个上。
  • 我也一直在四处挖掘,现在似乎无法做到,除非有其他方法,例如使用分页...不过感谢您的帮助。
  • 我明白了。一种解决方案是在 Elasticsearch 之外计算聚合,因为您的数据集似乎是有限的(您说的是 200 个结果)。
猜你喜欢
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2023-04-03
  • 2021-09-11
相关资源
最近更新 更多