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.
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:
Since in your mapping type, the type you're declaring is called
mygeopointsand thegeo_pointfield is calledgeopoint, yourcreatecall must use them properly like this:Issue 2: The parameter hash in your search call is not correct as your query DSL needs to be assigned to the
bodyparameter (similar to yourcreatecall) and it is also good practice to add anindexparameter to focus your search on (see below)Issue 3: Finally, in your query you're not using the proper field in your
geo_distancefilter, you havelocationinstead ofgeopoint. Your query should look like this: