elasticsearch - .keyword上的聚合仅返回包含特定字符串的键

标签 elasticsearch elasticsearch-aggregation

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

对应:

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

返回所有键但我需要做的示例查询仅限于仅返回具有“Branch”或更好的键,而仅返回有多少“Branch”键的计数:
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
        }
      ]
    }
  }
}

最佳答案

您必须添加包含项才能获得极限结果。这是代码示例,希望对您有所帮助。

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

关于elasticsearch - .keyword上的聚合仅返回包含特定字符串的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58531014/

相关文章:

scala - 在Play框架中使用Elastic4s创建Elasticsearch索引时出现无效的Json错误

elasticsearch - 如何索引html内容,保持位置(如xpath,css选择器等)

elasticsearch - 在Elasticsearch中获取其文本字段仅包含数字值的文档

sorting - 如何在Elasticsearch中基于存储的字段值获取前5个文档?

elasticsearch - 我们如何在Elastic Search中的文字类型列上汇总SUM?

Django使用密码身份验证设置elasticsearch客户端

带分组的 Elasticsearch 查询

Elasticsearch 数组值计数聚合

elasticsearch - ElasticSearch聚合+在非数值字段5.3上排序

elasticsearch - 在 Elasticsearch 中突出显示