Elasticsearch geospatial search, problems with index setup

224 views Asked by At

I'm trying to search for documents previously added to an index, which has been configured to allow geospatial queries (or so I think).
My elasticsearch instance is hosted on qbox.io.

This is the code I wrote to create an index from the command line

curl -XPOST username:[email protected]/events -d '{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
      "mygeopoints": {
        "properties": {
          "geopoint": {
            "type": "geo_point",
            "lat_lon" : true
          },
          "radius": {
            "type": "long"
          }
        }
      }
    }
  }'

As I've understood it I should create a mapping between my events index and the type of search I want to perform on it.

This is the code I wrote to create test documents:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'username:[email protected]'
});

client.create({
  index: 'events',
  type: 'geo_point',
  body: {
    location: {
      lat: 51.507351,
      lon: -0.127758
    }
  }
}, console.log);

This is the code I wrote to search for a document given a radius

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'username:[email protected]'
});

client.search({
  filtered: {
    query: {
      match_all: {}
    },
    filter: {
      geo_distance: {
        distance: '1km',
        location: {
          lat: 48.507351,
          lon: -0.127758
        }
      }
    }
  }
}, console.log);

My problem is that all documents for the event index always show up, so I am not successfully filtering with the geospatial query; do you spot any errors or do you have any guide I can follow to do this? I've searched and just found only bits of information.

1

There are 1 answers

3
Val On BEST ANSWER

There are a couple issues in your code:

Issue 1: When you create your document in the second snippet, you're not using the correct mapping type and your body doesn't include the correct field name as declared in your mapping:

client.create({
  index: 'events',
  type: 'geo_point',     <-------- wrong type
  body: {
    location: {          <-------- wrong field name
      lat: 51.507351,
      lon: -0.127758
    }
  }
}, console.log);

Since in your mapping type, the type you're declaring is called mygeopoints and the geo_point field is called geopoint, your create call must use them properly like this:

client.create({
  index: 'events',
  type: 'mygeopoints',
  body: {
    geopoint: {
      lat: 51.507351,
      lon: -0.127758
    }
  }
}, console.log);

Issue 2: The parameter hash in your search call is not correct as your query DSL needs to be assigned to the body parameter (similar to your create call) and it is also good practice to add an index parameter to focus your search on (see below)

Issue 3: Finally, in your query you're not using the proper field in your geo_distance filter, you have location instead of geopoint. Your query should look like this:

client.search({
  index: 'events',                <---- add index name
  body: {                         <---- add query in body parameter
    query:{
     filtered: {
       filter: {
         geo_distance: {
           distance: '1km',
           geopoint: {            <---- proper geo point field name
             lat: 48.507351,
             lon: -0.127758
           }
         }
       }
     }
    }
  }
}, console.log);