Reputation: 2449
This is just a quick question. I am planning to change my search system to elasticsearch. If I send elasticsearch three pieces of information in my request:
And my elasticsearch results are tagged with the following information:
Is it possible for me to only return results within that distance without needed any additional elasticsearch plugins? Is this functionality built into elasticsearch?
Upvotes: 0
Views: 234
Reputation: 22342
Yes, in three pretty simple steps. You must map the data (like setting up a database schema), insert the data, and then search for it (filter).
You must appropriately map the field so that it is recognized as a geo_point
. Note: If you wanted more flexibility to store other types of geolocations (e.g., polygons or linestrings), then you would want to map it as a geo_shape
.
{
"mappings" : {
"TYPE_NAME" : {
"properties" : {
"fieldName" : { "type" : "geo_point" }
}
}
}
}
Once mapped, you can insert the location details using the point
type.
{
"fieldName" : {
"type" : "point",
"coordinates" : [ longitude, latitude ]
}
}
You can type it in differently to get the same effect, but perhaps more intuitively (rather than [ X, Y ], which is not the most common geospatial order).
{
"fieldName" : {
"type" : "point",
"coordinates" : {
"lat" : latitude,
"lon" : longitude
}
}
}
Either way, it will represent the same thing (same goes for below in the filter).
Once you have mapped the field and started to insert points, then you can run a geo_distance
filter to find points within the desired distance
:
{
"filtered" : {
"query" : { "match_all" : {} },
"filter" : {
"geo_distance" : {
"distance" : "10mi",
"fieldName" : [ longitude, latitude ]
}
}
}
}
Upvotes: 1