Dataset
The guide uses a dataset of the 3,000+ biggest airports in the world.JSON
_geoloc
attribute.
Initialize the client
Configure index settings
Even if you just want to sort by distance to a location, your textual relevance should also be good so that users can refine the search with a query. To do that, you must configure the index. The searchable attributes are:name
, city
, country
, and iata_code
.
Custom ranking
Algolia will use an airport’s number of connected airports as a ranking metric. The more connections, the better.Ranking
When filtering around a location, Algolia can also sort the results by distance from this location. This sorting by distance happens in the ranking formula’sgeo
criterion. If geo
isn’t active, you can’t sort by distance.
Filtering around a given location
To filter airports around New York City (latitude 40.71 and longitude -74.01), use thearoundLatLng
parameter.
''
) tells Algolia to retrieve all airports
Filtering around the user’s current location
As you don’t know your user’s coordinates in advance, you can retrieve their IP address’s associated location with thearoundLatLngViaIP
parameter.
Filtering by radius
By default, the engine automatically defines a radius to filter on depending on the population density of the user’s location. To define the radius yourself, use thearoundRadius
parameter. The bigger the radius, the less filtering you have.
This example sorts by distance to New York City, with a radius of 1,000 km.