This guide shows you how to filter results around a location.
This location can be set manually or taken from the a user’s current position.
Dataset
The guide uses a dataset of the 3,000+ biggest airports in the world.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [
{
"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
}
]
|
Store each record’s location (latitude and longitude) in the _geoloc
attribute.
Initialize the client
- Download the data set
- Set up an API client and send your data to Algolia
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
.
1
2
3
4
5
6
7
8
| var response = await client.SetSettingsAsync(
"ALGOLIA_INDEX_NAME",
new IndexSettings
{
SearchableAttributes = new List<string> { "name", "country", "city", "iata_code" },
CustomRanking = new List<string> { "desc(links_count)" },
}
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| final response = await client.setSettings(
indexName: "ALGOLIA_INDEX_NAME",
indexSettings: IndexSettings(
searchableAttributes: [
"name",
"country",
"city",
"iata_code",
],
customRanking: [
"desc(links_count)",
],
),
);
|
1
2
3
4
5
6
7
8
9
| response, err := client.SetSettings(client.NewApiSetSettingsRequest(
"ALGOLIA_INDEX_NAME",
search.NewEmptyIndexSettings().SetSearchableAttributes(
[]string{"name", "country", "city", "iata_code"}).SetCustomRanking(
[]string{"desc(links_count)"})))
if err != nil {
// handle the eventual error
panic(err)
}
|
1
2
3
4
5
6
| client.setSettings(
"ALGOLIA_INDEX_NAME",
new IndexSettings()
.setSearchableAttributes(Arrays.asList("name", "country", "city", "iata_code"))
.setCustomRanking(Arrays.asList("desc(links_count)"))
);
|
1
2
3
4
5
6
7
| const response = await client.setSettings({
indexName: 'theIndexName',
indexSettings: {
searchableAttributes: ['name', 'country', 'city', 'iata_code'],
customRanking: ['desc(links_count)'],
},
});
|
1
2
3
4
5
6
7
| var response = client.setSettings(
indexName = "ALGOLIA_INDEX_NAME",
indexSettings = IndexSettings(
searchableAttributes = listOf("name", "country", "city", "iata_code"),
customRanking = listOf("desc(links_count)"),
),
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $response = $client->setSettings(
'ALGOLIA_INDEX_NAME',
['searchableAttributes' => [
'name',
'country',
'city',
'iata_code',
],
'customRanking' => [
'desc(links_count)',
],
],
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| response = client.set_settings(
index_name="ALGOLIA_INDEX_NAME",
index_settings={
"searchableAttributes": [
"name",
"country",
"city",
"iata_code",
],
"customRanking": [
"desc(links_count)",
],
},
)
|
1
2
3
4
5
6
7
| response = client.set_settings(
"ALGOLIA_INDEX_NAME",
Algolia::Search::IndexSettings.new(
searchable_attributes: ["name", "country", "city", "iata_code"],
custom_ranking: ["desc(links_count)"]
)
)
|
1
2
3
4
5
6
7
8
9
10
| val response = Await.result(
client.setSettings(
indexName = "ALGOLIA_INDEX_NAME",
indexSettings = IndexSettings(
searchableAttributes = Some(Seq("name", "country", "city", "iata_code")),
customRanking = Some(Seq("desc(links_count)"))
)
),
Duration(100, "sec")
)
|
1
2
3
4
5
6
7
| let response = try await client.setSettings(
indexName: "ALGOLIA_INDEX_NAME",
indexSettings: IndexSettings(
searchableAttributes: ["name", "country", "city", "iata_code"],
customRanking: ["desc(links_count)"]
)
)
|
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’s geo
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 the aroundLatLng
parameter.
1
2
3
4
| var response = await client.SearchSingleIndexAsync<Hit>(
"ALGOLIA_INDEX_NAME",
new SearchParams(new SearchParamsObject { AroundLatLng = "40.71, -74.01" })
);
|
1
2
3
4
5
6
| final response = await client.searchSingleIndex(
indexName: "ALGOLIA_INDEX_NAME",
searchParams: SearchParamsObject(
aroundLatLng: "40.71, -74.01",
),
);
|
1
2
3
4
5
6
7
| response, err := client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
"ALGOLIA_INDEX_NAME").WithSearchParams(search.SearchParamsObjectAsSearchParams(
search.NewEmptySearchParamsObject().SetAroundLatLng("40.71, -74.01"))))
if err != nil {
// handle the eventual error
panic(err)
}
|
1
| client.searchSingleIndex("ALGOLIA_INDEX_NAME", new SearchParamsObject().setAroundLatLng("40.71, -74.01"), Hit.class);
|
1
2
3
4
| const response = await client.searchSingleIndex({
indexName: 'indexName',
searchParams: { aroundLatLng: '40.71, -74.01' },
});
|
1
2
3
4
5
6
| var response = client.searchSingleIndex(
indexName = "ALGOLIA_INDEX_NAME",
searchParams = SearchParamsObject(
aroundLatLng = "40.71, -74.01",
),
)
|
1
2
3
4
5
| $response = $client->searchSingleIndex(
'ALGOLIA_INDEX_NAME',
['aroundLatLng' => '40.71, -74.01',
],
);
|
1
2
3
4
5
6
| response = client.search_single_index(
index_name="ALGOLIA_INDEX_NAME",
search_params={
"aroundLatLng": "40.71, -74.01",
},
)
|
1
2
3
4
| response = client.search_single_index(
"ALGOLIA_INDEX_NAME",
Algolia::Search::SearchParamsObject.new(around_lat_lng: "40.71, -74.01")
)
|
1
2
3
4
5
6
7
8
9
10
11
| val response = Await.result(
client.searchSingleIndex(
indexName = "ALGOLIA_INDEX_NAME",
searchParams = Some(
SearchParamsObject(
aroundLatLng = Some("40.71, -74.01")
)
)
),
Duration(100, "sec")
)
|
1
2
3
4
5
| let response: SearchResponse<Hit> = try await client.searchSingleIndex(
indexName: "ALGOLIA_INDEX_NAME",
searchParams: SearchSearchParams
.searchSearchParamsObject(SearchSearchParamsObject(aroundLatLng: "40.71, -74.01"))
)
|
An empty query (''
) 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 the aroundLatLngViaIP
parameter.
1
2
3
4
| var response = await client.SearchSingleIndexAsync<Hit>(
"ALGOLIA_INDEX_NAME",
new SearchParams(new SearchParamsObject { AroundLatLngViaIP = true })
);
|
1
2
3
4
5
6
| final response = await client.searchSingleIndex(
indexName: "ALGOLIA_INDEX_NAME",
searchParams: SearchParamsObject(
aroundLatLngViaIP: true,
),
);
|
1
2
3
4
5
6
7
| response, err := client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
"ALGOLIA_INDEX_NAME").WithSearchParams(search.SearchParamsObjectAsSearchParams(
search.NewEmptySearchParamsObject().SetAroundLatLngViaIP(true))))
if err != nil {
// handle the eventual error
panic(err)
}
|
1
| client.searchSingleIndex("ALGOLIA_INDEX_NAME", new SearchParamsObject().setAroundLatLngViaIP(true), Hit.class);
|
1
2
3
4
| const response = await client.searchSingleIndex({
indexName: 'indexName',
searchParams: { aroundLatLngViaIP: true },
});
|
1
2
3
4
5
6
| var response = client.searchSingleIndex(
indexName = "ALGOLIA_INDEX_NAME",
searchParams = SearchParamsObject(
aroundLatLngViaIP = true,
),
)
|
1
2
3
4
5
| $response = $client->searchSingleIndex(
'ALGOLIA_INDEX_NAME',
['aroundLatLngViaIP' => true,
],
);
|
1
2
3
4
5
6
| response = client.search_single_index(
index_name="ALGOLIA_INDEX_NAME",
search_params={
"aroundLatLngViaIP": True,
},
)
|
1
2
3
4
| response = client.search_single_index(
"ALGOLIA_INDEX_NAME",
Algolia::Search::SearchParamsObject.new(around_lat_lng_via_ip: true)
)
|
1
2
3
4
5
6
7
8
9
10
11
| val response = Await.result(
client.searchSingleIndex(
indexName = "ALGOLIA_INDEX_NAME",
searchParams = Some(
SearchParamsObject(
aroundLatLngViaIP = Some(true)
)
)
),
Duration(100, "sec")
)
|
1
2
3
4
| let response: SearchResponse<Hit> = try await client.searchSingleIndex(
indexName: "ALGOLIA_INDEX_NAME",
searchParams: SearchSearchParams.searchSearchParamsObject(SearchSearchParamsObject(aroundLatLngViaIP: true))
)
|
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 the aroundRadius
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.
1
2
3
4
5
6
7
8
9
10
| var response = await client.SearchSingleIndexAsync<Hit>(
"ALGOLIA_INDEX_NAME",
new SearchParams(
new SearchParamsObject
{
AroundLatLng = "40.71, -74.01",
AroundRadius = new AroundRadius(1000000),
}
)
);
|
1
2
3
4
5
6
7
| final response = await client.searchSingleIndex(
indexName: "ALGOLIA_INDEX_NAME",
searchParams: SearchParamsObject(
aroundLatLng: "40.71, -74.01",
aroundRadius: 1000000,
),
);
|
1
2
3
4
5
6
7
| response, err := client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
"ALGOLIA_INDEX_NAME").WithSearchParams(search.SearchParamsObjectAsSearchParams(
search.NewEmptySearchParamsObject().SetAroundLatLng("40.71, -74.01").SetAroundRadius(search.Int32AsAroundRadius(1000000)))))
if err != nil {
// handle the eventual error
panic(err)
}
|
1
2
3
4
5
| client.searchSingleIndex(
"ALGOLIA_INDEX_NAME",
new SearchParamsObject().setAroundLatLng("40.71, -74.01").setAroundRadius(AroundRadius.of(1000000)),
Hit.class
);
|
1
2
3
4
| const response = await client.searchSingleIndex({
indexName: 'indexName',
searchParams: { aroundLatLng: '40.71, -74.01', aroundRadius: 1000000 },
});
|
1
2
3
4
5
6
7
| var response = client.searchSingleIndex(
indexName = "ALGOLIA_INDEX_NAME",
searchParams = SearchParamsObject(
aroundLatLng = "40.71, -74.01",
aroundRadius = AroundRadius.of(1000000),
),
)
|
1
2
3
4
5
6
| $response = $client->searchSingleIndex(
'ALGOLIA_INDEX_NAME',
['aroundLatLng' => '40.71, -74.01',
'aroundRadius' => 1000000,
],
);
|
1
2
3
4
5
6
7
| response = client.search_single_index(
index_name="ALGOLIA_INDEX_NAME",
search_params={
"aroundLatLng": "40.71, -74.01",
"aroundRadius": 1000000,
},
)
|
1
2
3
4
| response = client.search_single_index(
"ALGOLIA_INDEX_NAME",
Algolia::Search::SearchParamsObject.new(around_lat_lng: "40.71, -74.01", around_radius: 1000000)
)
|
1
2
3
4
5
6
7
8
9
10
11
12
| val response = Await.result(
client.searchSingleIndex(
indexName = "ALGOLIA_INDEX_NAME",
searchParams = Some(
SearchParamsObject(
aroundLatLng = Some("40.71, -74.01"),
aroundRadius = Some(AroundRadius(1000000))
)
)
),
Duration(100, "sec")
)
|
1
2
3
4
5
6
7
| let response: SearchResponse<Hit> = try await client.searchSingleIndex(
indexName: "ALGOLIA_INDEX_NAME",
searchParams: SearchSearchParams.searchSearchParamsObject(SearchSearchParamsObject(
aroundLatLng: "40.71, -74.01",
aroundRadius: SearchAroundRadius.int(1_000_000)
))
)
|