In this tutorial, ‘ll see how to can filter results inside a rectangular area.
This location can either be set manually or taken from the current user position.
Dataset
The dataset contains 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, the latitude and longitude must be stored 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.
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)" ]
)
)
Searchable attributes
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.
Filtering inside a rectangle area
The USA can be considered as a polygon:
To filter inside this rectangle, you need to pass the upper-left and bottom-right latitude and longitude to the insideBoundingBox
parameter:
49.067996905313834, 65.73828125
25.905859247243498, 128.8046875
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var response = await client . SearchSingleIndexAsync < Hit >(
"ALGOLIA_INDEX_NAME" ,
new SearchParams (
new SearchParamsObject
{
InsideBoundingBox = new InsideBoundingBox (
new List < List < Double >>
{
new List < Double > { 49.067996905313834 , 65.73828125 , 25.905859247243498 , 128.8046875 },
}
),
}
)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
final response = await client . searchSingleIndex (
indexName: "ALGOLIA_INDEX_NAME" ,
searchParams: SearchParamsObject (
insideBoundingBox: [
[
49.067996905313834 ,
65.73828125 ,
25.905859247243498 ,
128.8046875 ,
],
],
),
);
1
2
3
4
5
6
7
8
9
response , err := client . SearchSingleIndex ( client . NewApiSearchSingleIndexRequest (
"ALGOLIA_INDEX_NAME" ) . WithSearchParams ( search . SearchParamsObjectAsSearchParams (
search . NewEmptySearchParamsObject () . SetInsideBoundingBox ( search . ArrayOfArrayOfFloat64AsInsideBoundingBox (
[][] float64 {
[] float64 { 49.067996905313834 , 65.73828125 , 25.905859247243498 , 128.8046875 }})))))
if err != nil {
// handle the eventual error
panic ( err )
}
1
2
3
4
5
6
7
8
client . searchSingleIndex (
"ALGOLIA_INDEX_NAME" ,
new SearchParamsObject ()
. setInsideBoundingBox (
InsideBoundingBox . of ( Arrays . asList ( Arrays . asList ( 49.067996905313834 , 65.73828125 , 25.905859247243498 , 128.8046875 )))
),
Hit . class
);
1
2
3
4
const response = await client . searchSingleIndex ({
indexName : ' indexName ' ,
searchParams : { insideBoundingBox : [[ 49.067996905313834 , 65.73828125 , 25.905859247243498 , 128.8046875 ]] },
});
1
2
3
4
5
6
var response = client . searchSingleIndex (
indexName = "ALGOLIA_INDEX_NAME" ,
searchParams = SearchParamsObject (
insideBoundingBox = InsideBoundingBox . of ( listOf ( listOf ( 49.067996905313834 , 65.73828125 , 25.905859247243498 , 128.8046875 ))),
),
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$response = $client -> searchSingleIndex (
'ALGOLIA_INDEX_NAME' ,
[ 'insideBoundingBox' => [
[
49.067996905313834 ,
65.73828125 ,
25.905859247243498 ,
128.8046875 ,
],
],
],
);
1
2
3
4
5
6
7
8
9
10
11
12
13
response = client . search_single_index (
index_name = "ALGOLIA_INDEX_NAME" ,
search_params = {
"insideBoundingBox" : [
[
49.067996905313834 ,
65.73828125 ,
25.905859247243498 ,
128.8046875 ,
],
],
},
)
1
2
3
4
5
6
response = client . search_single_index (
"ALGOLIA_INDEX_NAME" ,
Algolia :: Search :: SearchParamsObject . new (
inside_bounding_box: [[ 49.067996905313834 , 65.73828125 , 25.905859247243498 , 128.8046875 ]]
)
)
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 (
insideBoundingBox =
Some ( InsideBoundingBox ( Seq ( Seq ( 49.067996905313834 , 65.73828125 , 25.905859247243498 , 128.8046875 ))))
)
)
),
Duration ( 100 , "sec" )
)
1
2
3
4
5
6
7
8
9
10
11
12
13
let response : SearchResponse < Hit > = try await client . searchSingleIndex (
indexName : "ALGOLIA_INDEX_NAME" ,
searchParams : SearchSearchParams
. searchSearchParamsObject ( SearchSearchParamsObject (
insideBoundingBox : SearchInsideBoundingBox
. arrayOfArrayOfDouble ([[
49.067996905313834 ,
65.73828125 ,
25.905859247243498 ,
128.8046875 ,
]])
))
)
The empty query (''
) returns all matching airports.
© Algolia · Privacy Policy · Cookie settings