过滤字符串类型数组值的聚合问题

时间:2016-05-12 08:04:07

标签: elasticsearch nest

我们正在索引特定电子邮件的接收者,接收者可能是单个或可能是多个。

以下属性

  1. FieldName:Subject,Type:String,Analyzer:Keyword
  2. FieldName:Receivers,Type:String,Analyzedr:Keyword
  3. 索引日期

    1. Subject:hello,Receivers:["A@abc.com","B@abc","C@abc.com"]
    2. 问题在于过滤器聚合应用于术语聚合。如果“A @ abc.com”,“B @ abc”被过滤,那么逻辑上它只应在术语汇总中返回“A @ abc.com”,“B @ abc”,但它返回所有“A @ abc.com”, “B @ ABC”,C @ abc.com。

      以下是我的查询和输出 输入查询

      {
      "size":0,
      "aggs":{
          "filter":{
              "filter":{
                  "terms":{
                      "receivers":[
                          "A@abc.com",
                          "B@abc"
                          ]
                      }
              },
              "aggs":{
                  "result":{
                      "terms":{
                          "field":"receivers"
                      }
                  }
              }
          }
      }}
      

      输出

      {
      "took": 1,
      "timed_out": false,
      "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
      },
      "hits": {
          "total": 26464,
          "max_score": 0,
          "hits": []
      },
      "aggregations": {
          "filter": {
              "doc_count": 1,
              "result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                      {
                          "key": "A@abc.com",
                          "doc_count": 1
                      },
                      {
                          "key": "B@abc",
                          "doc_count": 1
                      },
                      {
                          "key": "C@abc.net",
                          "doc_count": 1
                      }
                  ]
              }
          }
      }}
      

      我们甚至试图使用include但是在某些情况下我们可能需要使用正则表达式自包含它如下所示。我们需要“A @ abc.com”,“B @ abc”以及仅过滤{{1仅来自“A @ abc.com”,“B @ abc”。因此输出应为“A@abc.com”,但它返回“A @ abc.com”,“B @ abc”

      ".*abc.com.*"

      请建议如何实现上述目标。

      提前致谢

1 个答案:

答案 0 :(得分:1)

您的查询应该有点不同:使用正则表达式时,这个不应该在数组中,而应该是独立的。点(.)应该被转义,因为它是一个保留字符:

{
  "size": 0,
  "aggs": {
    "filter": {
      "filter": {
        "terms": {
          "receiver": [
            "A@abc.com",
            "B@abc.com"
          ]
        }
      },
      "aggs": {
        "result": {
          "terms": {
            "field": "receiver",
            "include": ".*abc\\.com.*"
          }
        }
      }
    }
  }
}
相关问题