ncb000gt / node-es

NodeJS module for ElasticSearch.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Count using a query

omundy opened this issue · comments

Looking at the source code it appears there is no way to count with a query? Or am I doing something wrong? Trying to use this:

var options = {
    _index : 'cats',
    _type : 'cat',
    body: {
      "query": {
        "geo_bounding_box": { 
          "location": {
            "top_left": {
              "lat": 43,
              "lon": -72
            },
            "bottom_right": {
              "lat": 40,
              "lon": -74
            }
          }
        }
      }
    }
}
this.es.count(options,function (err, data) {
    console.log(data);
});

To run this:

GET cats/_count
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 43,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}

Scratch that, I now see this is possible. Thanks.

this.es.search({
    search_type : 'count'
}, {
    "query": {
        "geo_bounding_box": { 
          "location": {
            "top_left": {
              "lat": 43,
              "lon": -72
            },
            "bottom_right": {
              "lat": 40,
              "lon": -74
            }
          }
        }
      }
}, function (err, data) {
    console.log(data);
});

@omundy - thank you for the report... noticed there is also missing documentation for the count method (which accepts an optional query parameter, but this is not clearly noted in the docs): https://github.com/ncb000gt/node-es#count

Sweet, thanks.

@omundy - I've updated the docs now, but this should work as well (modified your original code example slightly):

var options = {
    _index : 'cats',
    _type : 'cat'
};

var query = {
      "query": {
        "geo_bounding_box": { 
          "location": {
            "top_left": {
              "lat": 43,
              "lon": -72
            },
            "bottom_right": {
              "lat": 40,
              "lon": -74
            }
          }
        }
      }
    }
};

this.es.count(options, query, function (err, data) {
    console.log(data);
});

Closing issue as resolved - thank you @omundy !