过滤汇总结果

时间:2020-10-06 16:02:16

标签: elasticsearch

此问题是this question的子问题。作为单独的问题发布以引起注意。

示例文档:

{
  "id":1,
  "product":"p1",
  "cat_ids":[1,2,3]
}
{
  "id":2,
  "product":"p2",
  "cat_ids":[3,4,5]
}
{
  "id":3,
  "product":"p3",
  "cat_ids":[4,5,6]
}

询问:获取属于特定类别的产品。例如cat_id = 3

查询:

GET product/_search 
{
 "size": 0,
 "aggs": {
   "cats": {
     "terms": {
       "field": "cats",
       "size": 10
     },"aggs": {
       "products": {
         "terms": {
           "field": "name.keyword",
           "size": 10
         }
       }
     }
   }
 }
}

问题:

  1. 如何在此处过滤cat_id = 3的汇总结果。我也尝试了bucket_selector,但是它不起作用。

    注意:由于cat_ids过滤具有多个值,因此无法进行聚合

1 个答案:

答案 0 :(得分:1)

您可以filter values,根据要创建的存储桶。

可以过滤将为其创建存储桶的值。 这可以使用include和exclude参数来完成, 基于正则表达式字符串或精确值数组。 此外,include子句可以使用分区表达式进行过滤。

添加包含索引数据,搜索查询和搜索结果的工作示例

索引数据:

  @Override
public boolean onDoubleTapEvent(MotionEvent e) {
  
      //To-do lock screen
      //If lock then again Double tap wake up the screen
   }

   return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
   return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
   return true;
}

搜索查询:

{
  "id":1,
  "product":"p1",
  "cat_ids":[1,2,3]
}
{
  "id":2,
  "product":"p2",
  "cat_ids":[3,4,5]
}
{
  "id":3,
  "product":"p3",
  "cat_ids":[4,5,6]
}

搜索结果:

{
  "size": 0,
  "aggs": {
    "cats": {
      "terms": {
        "field": "cat_ids",
        "include": [                   <-- note this
          3
        ]
      },
      "aggs": {
        "products": {
          "terms": {
            "field": "product.keyword",
            "size": 10
          }
        }
      }
    }
  }
}