简体   繁体   English

如何在ElasticSearch中汇总返回结果的数量?

[英]How to aggregate on number of returned results in ElasticSearch?

FYI, I'm using ES 1.7.2 in a Mac. 仅供参考,我在Mac中使用ES 1.7.2。

I've been trying to get the aggregations on a result set but what I'm getting is the aggregations on all the records. 我一直在尝试获取结果集上的汇总,但是我得到的是所有记录上的汇总。

Let's say I want to return 200 vehicles that are Ford Focus SE and from those 200, I want to know the Trims of all them and how many vehicles each Trim has. 假设我要归还200辆福特Focus SE的车辆,从那200辆车中,我想知道所有这些车辆的饰件以及每个饰件有多少辆车。 So basically a count for those Trims but also get the 200 results back. 因此,基本上可以算出这些Trim的数量,但也能获得200个结果。

Here's what I have so far(I'm using Sense/Marvel...Easier to test): 到目前为止,这是我所拥有的(我正在使用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" }
  }
}

So I do get 20 results back like so: 所以我确实得到了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
               }
            ]
         }
      }
   }
}

But as you can see, the Trims show up with a bigger count than the 200, which means it's doing the aggregations on all the vehicles. 但是正如您所看到的,“修剪”显示的数量大于200,这意味着它正在对所有车辆进行汇总。

I need some help and I can't find anything that actually makes this work. 我需要一些帮助,但找不到任何可以真正实现此目的的东西。

Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

You should add the same filter from the query to the aggregation. 您应该将查询中的相同过滤器添加到聚合中。 Note that I've only created the example for the facet "trims": 请注意,我只是为构面“修剪”创建了示例:

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

I'm not sure to have understood your usage of the post_filter, but if needed you can just add it in the "filter" section of the aggregation. 我不确定您是否了解post_filter的用法,但是如果需要,可以将其添加到聚合的“ filter”部分中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM