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.
Dataset
In this tutorial we’ll use a dataset of the 3000+ of the 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
}
]
|
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.
Initialize the client
1
2
3
4
5
6
7
8
9
10
11
12
| // composer autoload
require __DIR__ . '/vendor/autoload.php';
// if you are not using composer
// require_once 'path/to/algoliasearch.php';
$client = \Algolia\AlgoliaSearch\SearchClient::create(
'YourApplicationID',
'YourAdminAPIKey'
);
$index = $client->initIndex('your_index_name');
|
1
2
3
4
5
6
| require 'rubygems'
require 'algoliasearch'
Algolia.init(application_id: 'YourApplicationID',
api_key: 'YourAdminAPIKey')
index = Algolia::Index.new('your_index_name')
|
1
2
3
4
5
6
7
8
9
10
11
| // const algoliasearch = require('algoliasearch');
// const algoliasearch = require('algoliasearch/reactnative');
// const 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
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');
|
1
2
3
4
| from algoliasearch.search_client import SearchClient
client = SearchClient.create('YourApplicationID', 'YourAdminAPIKey')
index = client.init_index('your_index_name')
|
1
2
| let client = Client(appID: "YourApplicationID", apiKey: "YourAdminAPIKey")
let index = client.index(withName: "your_index_name")
|
We released a Kotlin API client, which is better suited for Android development. Check it out here.
1
2
| Client client = new Client("YourApplicationID", "YourAdminAPIKey");
Index index = client.getIndex("your_index_name");
|
1
2
| SearchClient client = new SearchClient("YourApplicationID", "YourAdminAPIKey");
SearchIndex index = client.InitIndex("your_index_name");
|
1
2
3
4
| SearchClient client =
DefaultSearchClient.create("YourApplicationID", "YourAdminAPIKey");
SearchIndex index = client.InitIndex("your_index_name");
|
1
2
3
4
5
6
7
8
| package main
import "github.com/algolia/algoliasearch-client-go/algolia/search"
func main() {
client := search.NewClient("YourApplicationID", "YourAdminAPIKey")
index := client.InitIndex("your_index_name")
}
|
1
2
| // No initIndex
val client = new AlgoliaClient("YourApplicationID", "YourAdminAPIKey")
|
1
2
3
4
5
6
7
| val client = ClientSearch(
applicationID = ApplicationID("latency"),
apiKey = APIKey("YourAdminAPIKey")
)
val indexName = IndexName("your_index_name")
client.initIndex(indexName)
|
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| $index->setSettings([
'searchableAttributes' => [
'name',
'city',
'country',
'iata_code'
],
'customRanking' => [
'desc(links_count)'
],
'ranking' => [
'typo',
'geo',
'words',
'attribute',
'proximity',
'exact',
'custom'
]
]);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| index.set_settings(
searchableAttributes: [
'name',
'city',
'country',
'iata_code'
],
customRanking: [
'desc(links_count)'
],
ranking: [
'typo',
'geo',
'words',
'attribute',
'proximity',
'exact',
'custom'
]
)
|
1
2
3
4
5
| index.setSettings(new IndexSettings()
.setSearchableAttributes(Arrays.asList("name", "city", "country", "iata_code"))
.setCustomRanking(Collections.singletonList("desc(links_count"))
.setRanking(Arrays.asList("typo", "geo", "words", "attribute", "proximity", "exact", "custom"))
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| res, err := index.SetSettings(search.Settings{
SearchableAttributes: opt.SearchableAttributes(
"name",
"city",
"country",
"iata_code",
),
CustomRanking: opt.CustomRanking("desc(links_count)"),
Ranking: opt.Ranking(
"typo",
"geo",
"words",
"attribute",
"proximity",
"exact",
"custom",
),
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| val settings = settings {
searchableAttributes {
+"name"
+"city"
+"country"
+"iata_code"
}
customRanking {
+Desc("links_count")
}
ranking {
+Typo
+Geo
+Words
+Attribute
+Proximity
+Exact
+Custom
}
}
index.setSettings(settings)
|
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.
Ranking
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.
1
2
3
| $results = $index->search('', [
'aroundLatLng' => '40.71, -74.01'
]);
|
1
| results = index.search('', aroundLatLng: '40.71, -74.01')
|
1
| index.search(new Query().setAroundLatLng("40.71, -74.01"));
|
1
| res, err := index.Search("", opt.AroundLatLng("40.71, -74.01"))
|
1
2
3
4
5
| val query = query {
aroundLatLng = Point(40.71f, -74.01f)
}
index.search(query)
|
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.
1
2
3
4
5
6
7
8
9
10
| /*
'94.228.178.246' 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 = '94.228.178.246';
$results = $index->search('', [
'aroundLatLngViaIP' => true,
'X-Forwarded-For' => $ip
]);
|
1
2
3
4
5
| # '94.228.178.246' 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', '94.228.178.246')
results = index.search('', aroundLatLngViaIP: true)
|
1
2
3
| index.search(new Query().setAroundLatLngViaIP(true),
new RequestOptions().addExtraHeader("X-Forwarded-For", "94.228.178.246")
);
|
1
2
3
4
5
6
7
| // "94.228.178.246" 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.
extraHeaders := opt.ExtraHeaders(map[string]string{
"X-Forwarded-For": "94.228.178.246",
})
res, err := index.Search("", opt.AroundLatLngViaIP(true), extraHeaders)
|
1
2
3
4
5
| val query = query {
aroundLatLngViaIP = true
}
index.search(query, requestOptions { headerForwardedFor("94.228.178.246") })
|
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.
1
2
3
4
| $results = $index->search('', [
'aroundLatLng' => '40.71, -74.01'
'aroundRadius' => 10000000 // 10000km
]);
|
1
2
3
4
| results = index.search('',
aroundLatLng: '40.71, -74.01',
aroundRadius: 10000000 # 10000km
)
|
1
2
3
4
| index.search(new Query()
.setAroundLatLng("40.71, -74.01")
.setAroundRadius(AroundRadius.of(10000000))
);
|
1
2
3
4
5
| res, err := index.Search(
"",
opt.AroundLatLng("40.71, -74.01"),
opt.AroundRadius(10000000), // 10'000 km
)
|
1
2
3
4
5
6
| val query = query {
aroundLatLng = Point(40.71f, -74.01f)
aroundRadius = AroundRadius.InMeters(10000000) // 10000km
}
index.search(query)
|