Elasticsearch-js: Cant get aggregates to return any results

Created on 4 Aug 2015  路  2Comments  路  Source: elastic/elasticsearch-js

I am trying to get aggregation working and use the following query:

new elastic.Client({host: 'localhost:9200'}).search({
        "index" : "logstash-2015.08.04",
        "type" : "stdout",
        "size" : 0,
        "aggs" : {
            "group-sessions" : {
                "terms" : {
                    "field": "sessionId"
                }
            }
        }
    }).then((body) => {
        console.log(body);
    }).catch((error) => {
        console.log(error);
    });

All I get is the following result:

{ took: 1,
  timed_out: false,
  _shards: { total: 5, successful: 5, failed: 0 },
  hits: { total: 57, max_score: 0, hits: [] } }

I would expect an "aggregations" property in the result. If I change size I do see results inside "hits". Also if I misspell "aggs" in the query there is no error. If I run the same query using CURL everything is OK.

curl -X POST "http://127.0.0.1:9200/logstash-2015.08.04/stdout/_search?pretty" -d'
{
     "size" : 0,
     "aggs" : {
        "group-sessions" : {
            "terms" : {
              "field": "sessionId"
            }
        }
     }
}'

In this case I get aggregation results as expected. If I mispell "aggs" here I get nice error message.

So in summary "aggs" seems to be ignored. I assume I'm doing something basic wrong but cant find it.

Most helpful comment

The issue here is that values you want in the body of the request (like "aggs" and "size") need to be specified under the body key in the request parameters. Params that can be specified at the top level of a search request are listed in the api docs for each method and you will notice that "aggs" is not listed there.

So your first example would read like this instead:

new elastic.Client({host: 'localhost:9200'})
.search({
    "index" : "logstash-2015.08.04",
    "type" : "stdout",
    "body": {
        "size" : 0,
        "aggs" : {
            "group-sessions" : {
                "terms" : {
                    "field": "sessionId"
                }
            }
        }
    }
}).then((body) => {
    console.log(body);
}).catch((error) => {
    console.log(error);
});

I agree with you, the fact that you specified "aggs" here and didn't get an error is an annoying side effect of the "loose" nature of this low level client. By low-level we mean that this is basically a wrapper around the core http library, but with affordances for clustering, changes in cluster topology, and near complete awareness of the api. It also means that there are few restrictions in place, and issues like this are common.

Our hope is that a higher-level, more user friendly/opinionated client can utilize the low-level client for communicating with elasticsearch, while providing a better experience for some users. I know options exist, but the only that I have used so far are elastic.js which are you can see from their first example is a helper for building that "body" parameter.

Hope that helps. Closing for now.

All 2 comments

The issue here is that values you want in the body of the request (like "aggs" and "size") need to be specified under the body key in the request parameters. Params that can be specified at the top level of a search request are listed in the api docs for each method and you will notice that "aggs" is not listed there.

So your first example would read like this instead:

new elastic.Client({host: 'localhost:9200'})
.search({
    "index" : "logstash-2015.08.04",
    "type" : "stdout",
    "body": {
        "size" : 0,
        "aggs" : {
            "group-sessions" : {
                "terms" : {
                    "field": "sessionId"
                }
            }
        }
    }
}).then((body) => {
    console.log(body);
}).catch((error) => {
    console.log(error);
});

I agree with you, the fact that you specified "aggs" here and didn't get an error is an annoying side effect of the "loose" nature of this low level client. By low-level we mean that this is basically a wrapper around the core http library, but with affordances for clustering, changes in cluster topology, and near complete awareness of the api. It also means that there are few restrictions in place, and issues like this are common.

Our hope is that a higher-level, more user friendly/opinionated client can utilize the low-level client for communicating with elasticsearch, while providing a better experience for some users. I know options exist, but the only that I have used so far are elastic.js which are you can see from their first example is a helper for building that "body" parameter.

Hope that helps. Closing for now.

@spalger
Makes perfect sense. Thanks for the prompt reply - very much appreciated. Was starting to tear what little hair I have left out. It would be really useful if that explanation is put in a prominent position somewhere in "large friendly letters".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bttf picture bttf  路  4Comments

drakejin picture drakejin  路  4Comments

souuu picture souuu  路  3Comments

lizpark picture lizpark  路  3Comments

kaustavghosh06 picture kaustavghosh06  路  5Comments