Skip to content

Instantly share code, notes, and snippets.

@sl-digital
Created June 14, 2017 21:50
Show Gist options
  • Save sl-digital/c08cdc4dd8a65b70499e28376c1d5cd1 to your computer and use it in GitHub Desktop.
Save sl-digital/c08cdc4dd8a65b70499e28376c1d5cd1 to your computer and use it in GitHub Desktop.
ElasticSearch Radius Search with Distance Sort
$lat = $this->listing->latitude;
$lon = $this->listing->longitude;
$params = [
"index" => "listings",
"body" => [
"from" => 0,
"size" => 26,
"query" => [
"bool" => [
"must" => [
"match_all" => (object) []
],
"filter" => [
"geo_distance" => [
"distance" => "5mi",
"location" => [
"lat" => $lat,
"lon" => $lon
],
"unit" => "mi"
]
]
]
],
"_source" => (object) [],
"sort" => [
"_script" => [
"type" => "number",
"script" => [
"lang" => "painless",
"inline" => "doc['location'].planeDistance($lat,$lon) * (0.000621371)"
],
"order" => "asc"
]
]
]
];
$resp = $this->client->search($params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment