在elasticsearch聚合中获取空桶数组

时间:2017-08-17 16:29:00

标签: elasticsearch

我正在使用ElasticSearch的5.4.1版。

当我尝试执行groupBy聚合/桶聚合时,我没有在桶阵列中获取任何值。

这是我的索引:

   curl -X PUT localhost:9200/urldata -d '{
    "mappings" : {
        "components" : {
            "properties" : {
                "name" : {
                    "type" : "keyword",
                    "index" : "not_analyzed"
                },
                "status" : {
                    "type" : "keyword",
                    "index" : "not_analyzed"
                },
                "timestamp":{
                    "type":"date",
                    "index":"not_analyzed"
                }
            }
        }
    }
}'

这是聚合查询:

curl -XGET 'localhost:9200/urldata/_search?pretty' -H 'Content-Type: application/json' -d'
    {
      "size": 0,
      "aggs": {
        "components": {
          "terms": {
            "field": "name.keyword"
          }
        }
      }
    }
    '

输出:

{  
"took":2,
   "timed_out":false,
   "_shards":{  
      "total":5,
      "successful":5,
      "failed":0
   },
   "hits":{  
      "total":3,
      "max_score":0.0,
      "hits":[  

      ]
   },
   "aggregations":{  
      "components":{  
         "doc_count_error_upper_bound":0,
         "sum_other_doc_count":0,
         "buckets":[  

         ]
      }
   }
}

我哪里错了?

1 个答案:

答案 0 :(得分:1)

试试这个,应该这样做:

{
  "size": 0,
  "aggs": {
    "components": {
      "terms": {
        "field": "name"
      }
    }
  }
}

修改

以下是复制用例的所有步骤:

PUT test
{
    "settings" : {
        "index" : {
            "number_of_shards" : 1, 
            "number_of_replicas" : 0
        }
    }
}

PUT test/_mapping/people_name
{  
  "properties":{  
      "name":{  
        "type":"keyword",
        "index":"not_analyzed"
      },
      "status":{  
        "type":"keyword",
        "index":"not_analyzed"
      },
      "timestamp":{  
         "type":"date",
         "index":"not_analyzed"
      }
  }
}

POST test/people_name
{
  "name": "A", 
  "status": "success", 
  "created_at": "2017-08-17"
}

POST test/people_name
{
  "name": "A", 
  "status": "success_2", 
  "created_at": "2017-06-15"
}

POST test/people_name
{
  "name": "B", 
  "status": "success", 
  "created_at": "2017-09-15"
}

GET test/people_name/_search
{
  "size": 0,
  "aggs": {
    "components": {
      "terms": {
        "field": "name"
      }
    }
  }
}

汇总的结果是:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "components": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "A",
          "doc_count": 2
        },
        {
          "key": "B",
          "doc_count": 1
        }
      ]
    }
  }
}