【问题标题】:Filtering aggregation issue on string type array values过滤字符串类型数组值的聚合问题
【发布时间】:2016-05-12 16:55:00
【问题描述】:

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

以下是属性

  1. FieldName:Subject,Type:String,Analyzer:Keyword
  2. FieldName:Receivers,Type:String,Analyzedr:Keyword

索引日期

  1. Subject:hello,Receivers:["A@abc.com","B@abc","C@abc.com"]

问题在于过滤聚合应用于术语聚合时。如果 "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,但在某些情况下,我们可能需要在 include it self 中使用正则表达式,如下所示。我们需要 "A@abc.com","B@abc" 以及仅过滤 ".*abc.com.*"仅来自“A@abc.com”、“B@abc”。所以输出应该是 "A@abc.com" 但它同时返回 "A@abc.com","B@abc"

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

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

提前致谢

【问题讨论】:

  • 您确定您的receivers 字段的映射正确吗?另外,为什么不使用"index":"not_analyzed" 而不是keyword 分析器?
  • 它会返回所有值,因为您在那里有一个值数组并且文档匹配,无论匹配多少值。
  • @AndreiStefan:感谢您的快速回复。您的意思是对于数组值没有办法只匹配精确值?
  • 不是你这样做的方式。使用includeterms 查看我的答案。那应该可以。
  • 有没有其他方法可以处理字符串类型的数组值,只返回数组中的过滤值而不是所有值?

标签: elasticsearch nest


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2020-07-21
    • 1970-01-01
    • 2018-04-26
    • 2020-11-27
    • 1970-01-01
    相关资源
    最近更新 更多