Elasticsearch geospatial search, problems with index setup -


i'm trying search documents added index, has been configured allow geospatial queries (or think).
elasticsearch instance hosted on qbox.io.

this code wrote create index command line

curl -xpost username:password@instance-id.qbox.io/events -d '{     "settings" : {         "number_of_shards" : 1     },     "mappings" : {       "mygeopoints": {         "properties": {           "geopoint": {             "type": "geo_point",             "lat_lon" : true           },           "radius": {             "type": "long"           }         }       }     }   }' 

as i've understood should create mapping between events index , type of search want perform on it.

this code wrote create test documents:

var elasticsearch = require('elasticsearch'); var client = new elasticsearch.client({   host: 'username:password@instance-id.qbox.io' });  client.create({   index: 'events',   type: 'geo_point',   body: {     location: {       lat: 51.507351,       lon: -0.127758     }   } }, console.log); 

this code wrote search document given radius

var elasticsearch = require('elasticsearch'); var client = new elasticsearch.client({   host: 'username:password@instance-id.qbox.io' });  client.search({   filtered: {     query: {       match_all: {}     },     filter: {       geo_distance: {         distance: '1km',         location: {           lat: 48.507351,           lon: -0.127758         }       }     }   } }, console.log); 

my problem documents event index show up, not filtering geospatial query; spot errors or have guide can follow this? i've searched , found bits of information.

there couple issues in code:

issue 1: when create document in second snippet, you're not using correct mapping type , body doesn't include correct field name declared in 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 mapping type, type you're declaring called mygeopoints , geo_point field called geopoint, create call must use them this:

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

issue 2: parameter hash in search call not correct query dsl needs assigned body parameter (similar create call) , practice add index parameter focus search on (see below)

issue 3: finally, in query you're not using proper field in geo_distance filter, have location instead of geopoint. query should 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); 

Comments