21 Nov 2018

Filter results around a location


In this tutorial we’ll see how we can filter results around a location. This location can either be set manually or taken from the current user position.


In this tutorial we’ll use a dataset of the 3000+ of the biggest airports in the world.

    objectID: "3797",
    name: "John F Kennedy Intl",
    city: "New York",
    country: "United States",
    iata_code: "JFK",
    _geoloc: {
      lat: 40.639751,
      lng: -73.778925
    links_count: 911
  // […]

To tell Algolia where each record is located, we need to have the latitude and longitude stored in the _geoloc attribute.

You can download the dataset here. Have look at how to import it in Algolia here.

Initialise the client

// composer autoload
require __DIR__ . '/vendor/autoload.php';

// if you are not using composer
// require_once 'path/to/algoliasearch.php';

$client = \Algolia\AlgoliaSearch\SearchClient::create(

$index = $client->initIndex('your_index_name');
require 'rubygems'
require 'algoliasearch'

Algolia.init(application_id: 'YourApplicationID',
             api_key:        'YourAPIKey')
index = Algolia::Index.new('your_index_name')
// var algoliasearch = require('algoliasearch');
// var algoliasearch = require('algoliasearch/reactnative');
// var algoliasearch = require('algoliasearch/lite');
// import algoliasearch from 'algoliasearch';
// or just use algoliasearch if you are using a <script> tag
// if you are using AMD module loader, algoliasearch will not be defined in window,
// but in the AMD modules of the page

var client = algoliasearch('YourApplicationID', 'YourAPIKey');
var index = client.initIndex('your_index_name');
from algoliasearch import algoliasearch

client = algoliasearch.Client("YourApplicationID", 'YourAPIKey')
index = client.init_index('your_index_name')
let client = Client(appID: "YourApplicationID", apiKey: "YourAPIKey")
let index = client.index(withName: "your_index_name")
Client client = new Client("YourApplicationID", "YourAPIKey");
Index index = client.getIndex("your_index_name");
AlgoliaClient client = new AlgoliaClient("YourApplicationID", "YourAPIKey");
Index index = client.InitIndex("your_index_name");
 // Synchronous version
 APIClient client = new ApacheAPIClientBuilder("YourApplicationID", "YourAPIKey").build();

 // Asynchronous version
 AsyncAPIClient client = new AsyncHttpAPIClientBuilder("YourApplicationID", "YourAPIKey").build();

 // Google AppEngine
 APIClient client = new AppEngineAPIClientBuilder("YourApplicationID", "YourAPIKey").build();

 Index index = client.initIndex("index");
import "github.com/algolia/algoliasearch-client-go/algoliasearch"

func main() {
  client := algoliasearch.NewClient("YourApplicationID", "YourAPIKey")
  index := client.InitIndex("your_index_name")
// No initIndex
val client = new AlgoliaClient("YourApplicationID", "YourAPIKey")

Configure Index Settings

Even if we want to sort by distance to a location we need the textual relevance to be good in case refine the search with a query.

For that lets configure the index.

  'searchableAttributes' => [
  'customRanking' => [
  'ranking' => [
  searchableAttributes: [
  customRanking: [
  ranking: [

Searchable attributes

We’re going to search in our 4 textual attributes: name, city, country and iata_code.

Custom Ranking

Lets use the number of other connected airports to any airport as a ranking metric. The more connection the better.


When filtering around a location, Algolia can also sort the results by distance from this location.

The sorting by distance happens in the criterion geo of the ranking formula. If you don’t have this criterion active, you cannot sort by distance.

Filtering around a given location

Let’s filter airports around New York City. New York City has a latitude of 40.71 and a longitude of -74.01.

We are going to use the aroundLatLng parameter.

$results = $index->search('', [
  'aroundLatLng' => '40.71, -74.01'
results = index.search('', aroundLatLng: '40.71, -74.01')

We are using the empty query ('') to tell that we want all airports

Filtering around the current user location

As we don’t know in advance the Lat/Lng coordinates of the current user, we can rely on the IP address, that we’ll automatically associate to a location.

We are going to use the aroundLatLngViaIP parameter.

'' should be replaced with the actual IP you would like to search around.
Depending on your stack there are multiple ways to get this information.
$ip = '';

$results = $index->search('', [
  'aroundLatLngViaIP' => true,
  'X-Forwarded-For' => $ip
# '' should be replaced with the actual IP you would like to search around.
# Depending on your stack there are multiple ways to get this information.
client.set_extra_header('X-Forwarded-For', '')

results = index.search('', aroundLatLngViaIP: true)

Filtering radius

By default the engine will define automatically a radius to filter on depending on the area density.

To define the radius ourselves we can use the aroundRadius parameter. The bigger it is the less filtering you have.

Let’s sort by distance to New York City taking a radius of 1000km.

$results = $index->search('', [
  'aroundLatLng' => '40.71, -74.01'
  'aroundRadius' => 10000000 // 10000km
results = index.search('',
  aroundLatLng: '40.71, -74.01',
  aroundRadius: 10000000 # 10000km

What’s next

Here are a few other tutorials that could interest you:

© Algolia - Privacy Policy