Aggregation based on two fields in Elasticsearch results in SearchParseExcepetion with "cannot find aggregator type"

1.2k views Asked by At

I am using Elasticsearch 1.4.0 and trying out the aggregation feature. I keep getting a SearchParseException with the message Cannot find aggregator type [fieldName] in [aggregationName].

In JSON format, my data looks like the following.

{ "userCode": "abcd123", "response": 1 }
{ "userCode": "abcd123", "response": 1 }
{ "userCode": "abcd123", "response": 0 }
{ "userCode": "wxyz123", "response": 0 }
{ "userCode": "wxyz123", "response": 0 }
{ "userCode": "wxyz123", "response": 1 }

Note, there are 2 users, abcd123 and wxyz123, and I simply want to count the number of times each responded 1 and 0. If I put this data into a SQL table, in SQL select syntax, I would do something like this (if this SQL example helps to illustrate what I am trying to do).

select userCode, response, count(*) as total
from response_table
group by userCode, response

I would expect a results set that looks like the following.

abcd123, 0, 1 //user abcd123 responded 0 once
abcd123, 1, 2 //user abcd123 responded 1 twice
wxyz123, 0, 2 //user wxyz123 responded 0 twice
wxyz123, 1, 1 //user wxyz123 responded 1 once

For Elasticsearch, my aggregation JSON looks like the following.

{
 "aggs": {
  "users": {
   "terms": { "field": "userCode" },
   "aggs": {
    "responses" : {
     "terms": { "field": "response" }
    }
   }
  }
 }
}

However, I get the SearchParseException: Cannot find aggregator type [responses] in [aggs]. What am I doing wrong?

If it helps, my mapping file is very simple, and looks like the following.

{
  "data": {
    "properties": {
      "userCode": {
        "type": "string",
        "store": "yes",
        "index": "analyzed",
        "term_vector": "no"
      },
      "response": {
        "type": "integer",
        "store": "yes",
        "index": "analyzed",
        "term_vector": "yes"
      }
    }
  }
}
1

There are 1 answers

1
Jane Wayne On

The following aggregation worked for me (it got me the results I wanted), but I'd still like for some clarifications on why my previous approach resulted in a SearchParseException.

{
 "aggs": {
  "users": {
   "terms": { "field" : "userCode" },
   "aggs": {
    "responses": {
     "histogram": { "field": "response", "interval": 1 }
    }
   }
  }
 }
}