【问题标题】:Aggregation on .keyword to return only the keys that contain a specific string聚合 .keyword 以仅返回包含特定字符串的键
【发布时间】:2020-02-20 04:22:59
【问题描述】:

elasticsearch 中的聚合新手。使用 7.2。我正在尝试在 Tree.keyword 上编写一个聚合,以仅返回具有包含单词“Branch”的键的文档的计数。我尝试过子聚合、bucket_selector(不适用于键字符串)和脚本。有人对如何解决这个问题有任何想法或建议吗?

映射:

{
  "testindex" : {
    "mappings" : {
      "properties" : {
        "Tree" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        }
      }
    }
  }
}

返回所有键的示例查询,但我需要做的是限制只返回带有“分支”或更好的键,但只返回有多少“分支”键的计数:

GET testindex/_search
{
  "aggs": {
    "bucket": {
      "terms": {
        "field": "Tree.keyword"
      }
    }
  }
}

返回:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "Tree" : [
            "Car:76",
            "Branch:yellow",
            "Car:one",
            "Branch:blue"
          ]
        }
      }
    ]
  },
  "aggregations" : {
    "bucket" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Car:76",
          "doc_count" : 1
        },
        {
          "key" : "Branch:yellow",
          "doc_count" : 1
        },
        {
          "key" : "Car:one",
          "doc_count" : 1
        },
        {
          "key" : "Branch:blue",
          "doc_count" : 1
        }
      ]
    }
  }
}

【问题讨论】:

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

您必须为限制结果添加包含。这是代码示例,希望对您有所帮助。

 GET testindex/_search
    {
    "_source": {
    "includes": [
      "Branch"
    ]
    },
      "aggs": {
        "bucket": {
          "terms": {
            "field": "Tree.keyword"
          }
        }
      }
    }

【讨论】:

    【解决方案2】:

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

    对于你的情况,应该是这样的,

    GET testindex/_search
    {
      "aggs": {
        "bucket": {
          "terms": {
            "field": "Tree.keyword",
            "include": "Branch:*"
          }
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      感谢大家的帮助!不幸的是,这些解决方案都不适合我。我最终使用脚本返回所有分支,然后将其他所有内容设置为新键。然后使用存储桶脚本在 Total_Buckets 中减去 1。可能是一个更好的解决方案,但希望它可以帮助某人

      
      GET testindex/_search
      {
        "aggs": {
          "bucket": {
            "cardinality": {
              "field": "Tree.keyword",
              "script": {
                "lang": "painless",
                "source": "if(_value.contains('Branches:')) { return _value} return 1;"
              }
            }
          },
          "Total_Branches": {
            "bucket_script": {
              "buckets_path": {
                "my_var1": "bucket.value"
              },
              "script": "return params.my_var1-1"
            }
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-27
        • 1970-01-01
        • 1970-01-01
        • 2014-07-31
        • 1970-01-01
        • 2021-12-13
        • 2016-01-24
        • 1970-01-01
        相关资源
        最近更新 更多