1

I'm using version 5.4.1 of ElasticSearch.

When I try to perform a groupBy aggregate/bucket aggregate, I'm not getting any values in the bucket array.

This is my index:

   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"
                }
            }
        }
    }
}'

And this the aggregate query:

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

Output:

{  
"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":[  

         ]
      }
   }
}

Where am I going wrong??

2
  • Can you provide some document example? Commented Aug 17, 2017 at 17:51
  • {name: "A", status: "success", created_at: "2017-08-17" } {name: "A",status: "failure",created_at: "2017-08-18"} Commented Aug 18, 2017 at 3:51

1 Answer 1

2

Try this, it should do it:

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

EDIT:

Here is all the steps to replicate your use case:

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"
      }
    }
  }
}

The result of the aggregation is:

{
  "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
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Tried this but it threw the exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. . Then I set fielddata=true and now it's working.Can't understand how can ES documentation can be so buggy.Atleast they should have mentioned this issue.
Which exception? Which version are you on?
I'm using ES version 5.4.1
So it's good? I just use your mapping. I had no problem going my query.
No. You misunderstood the documentation. They state that you should use name.keyword only if in your mapping nameis a text and you have another field name.keyword of type keyword. The query you write in your question should be name and not name.keyword according to your mapping. On my part I used your mapping and didn't have to set the field data to true. I will update my answer with all the steps
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.