elastic / elasticsearch-js

Official Elasticsearch client library for Node.js
https://ela.st/js-client
Apache License 2.0
5.24k stars 725 forks source link

Documentation on geospatial queries #228

Closed lazywithclass closed 9 years ago

lazywithclass commented 9 years ago

Hello, first of all thanks for this!

I'm pretty new with elasticsearch, I'm following this tutorial along with the docs to get started: http://www.elasticsearchtutorial.com/spatial-search-tutorial.html I am having some problems trying to figure out the correct search syntax for the following use case.

This is how I create my index:

curl -XPUT http://user:password@host/us_large_cities -d '
{
  "mappings": {
    "city": {
      "properties": {
        "city": {"type": "string"},
        "state": {"type": "string"},
          "location": {"type": "geo_point", lat_lon: true }
      }
    }
  }
}'

This is how I create a document:

curl -XPOST http://user:password@host/us_large_cities/city/ -d '
{
  "city": "Anchorage", 
  "state": "AK",
  "location": {
    "lat": "61.2180556", 
    "lon": "-149.9002778"
  }
}'

This is how I am using the library, I am sure I'm doing something wrong in this passage.

var elasticsearch = require('elasticsearch'),
    client = new elasticsearch.Client({ host: 'user:password@host' });

client.search({
  index: 'us_large_cities',
  type: 'city',
  "query": {
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "200m",
                "location" : {
                    // NOTICE HERE I CHANGED LATITUDE
                    // BUT I STILL GET A RECORD
                    "lat" : 100,
                    "lon" : -149.9002778
                }
            }
        }
    }
}, function (err, res) {
  if (err) console.log(err);
  else console.log(JSON.stringify(res, null, '  '));
  process.exit(0);
});

I know it's wrong because if I execute the following I get no results (which is correct, notice again the 100 for lat):

curl -XGET 'http://user:password@host/us_large_cities/city/_search?pretty=true' -d '
{
  "query": {
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "200m",
                "location" : {
                    "lat" : 100,
                    "lon" : -149.9002778
                }
            }
        }
    }
  }
}'
spalger commented 9 years ago

Hey @lazywithclass , looks like you're missing the body: key in your params. Checkout the search example in the esjs documentation for more info.

lazywithclass commented 9 years ago

Thanks @spalger, yeah, that was the problem. I'm now diving into the docs :D