如何根据搜索查询中的特定术语返回匹配数?

时间:2014-07-25 13:56:48

标签: elasticsearch

在我的搜索查询中,我有这个:

...
term: { CategoryId: [1,2,3] }
...

我需要返回每个类别找到多少匹配项。目前只返回total个匹配项。可能吗?我认为这可能与aggregation有关,但我无法找到正确的解决方案......

1 个答案:

答案 0 :(得分:1)

示例查询可以是

POST /test/products/_search
{
   "size": 0,
   "aggs": {
      "category": {
         "terms": {
            "field": "category"
         }
      }
   }
}

所以回应是,

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 10,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "category": {
         "buckets": [
            {
               "key": "1",
               "doc_count": 10
            },
            {
               "key": "2",
               "doc_count": 12
            }
         ]
      }
   }
}

每个类别都没有提供任何文件。 希望这会有所帮助!!

相关问题