In this tutorial, you’ll learn how to filter results around a polygonal location. 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
val response = client . setSettings (
indexName = "ALGOLIA_INDEX_NAME" ,
indexSettings = IndexSettings (
searchableAttributes = Some ( Seq ( "name" , "country" , "city" , "iata_code" )),
customRanking = Some ( Seq ( "desc(links_count)" ))
)
)
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 polygonal area
The USA can be considered as a polygon:
To filter inside this rectangle, you need to pass the latitude and longitude of all the points to the insidePolygon
parameter:
42.01,-124.31,
48.835509470063045,-124.40453125000005
45.01082951668149,-65.95726562500005
31.247243545293433,-81.06578125000004
25.924152577235226,-97.68234374999997
32.300311895879545,-117.54828125
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var response = await client . SearchSingleIndexAsync < Hit >(
"ALGOLIA_INDEX_NAME" ,
new SearchParams (
new SearchParamsObject
{
InsidePolygon = new List < List < Double >>
{
new List < Double >
{
42.01 ,
- 124.31 ,
48.835509470063045 ,
- 124.40453125000005 ,
45.01082951668149 ,
- 65.95726562500005 ,
31.247243545293433 ,
- 81.06578125000004 ,
25.924152577235226 ,
- 97.68234374999997 ,
32.300311895879545 ,
- 117.54828125 ,
},
},
}
)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
final response = await client . searchSingleIndex (
indexName: "ALGOLIA_INDEX_NAME" ,
searchParams: SearchParamsObject (
insidePolygon: [
[
42.01 ,
- 124.31 ,
48.835509470063045 ,
- 124.40453125000005 ,
45.01082951668149 ,
- 65.95726562500005 ,
31.247243545293433 ,
- 81.06578125000004 ,
25.924152577235226 ,
- 97.68234374999997 ,
32.300311895879545 ,
- 117.54828125 ,
],
],
),
);
1
2
3
4
5
6
7
8
9
response , err := client . SearchSingleIndex ( client . NewApiSearchSingleIndexRequest (
"ALGOLIA_INDEX_NAME" ) . WithSearchParams ( search . SearchParamsObjectAsSearchParams (
search . NewEmptySearchParamsObject () . SetInsidePolygon (
[][] float64 {
[] float64 { 42.01 , - 124.31 , 48.835509470063045 , - 124.40453125000005 , 45.01082951668149 , - 65.95726562500005 , 31.247243545293433 , - 81.06578125000004 , 25.924152577235226 , - 97.68234374999997 , 32.300311895879545 , - 117.54828125 }}))))
if err != nil {
// handle the eventual error
panic ( err )
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
client . searchSingleIndex (
"ALGOLIA_INDEX_NAME" ,
new SearchParamsObject ()
. setInsidePolygon (
Arrays . asList (
Arrays . asList (
42.01 ,
- 124.31 ,
48.835509470063045 ,
- 124.40453125000005 ,
45.01082951668149 ,
- 65.95726562500005 ,
31.247243545293433 ,
- 81.06578125000004 ,
25.924152577235226 ,
- 97.68234374999997 ,
32.300311895879545 ,
- 117.54828125
)
)
),
Hit . class
);
1
2
3
4
5
6
7
8
9
10
11
12
const response = await client . searchSingleIndex ({
indexName : ' indexName ' ,
searchParams : {
insidePolygon : [
[
42.01 , - 124.31 , 48.835509470063045 , - 124.40453125000005 , 45.01082951668149 , - 65.95726562500005 ,
31.247243545293433 , - 81.06578125000004 , 25.924152577235226 , - 97.68234374999997 , 32.300311895879545 ,
- 117.54828125 ,
],
],
},
});
1
2
3
4
5
6
var response = client . searchSingleIndex (
indexName = "ALGOLIA_INDEX_NAME" ,
searchParams = SearchParamsObject (
insidePolygon = listOf ( listOf ( 42.01 , - 124.31 , 48.835509470063045 , - 124.40453125000005 , 45.01082951668149 , - 65.95726562500005 , 31.247243545293433 , - 81.06578125000004 , 25.924152577235226 , - 97.68234374999997 , 32.300311895879545 , - 117.54828125 )),
),
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$response = $client -> searchSingleIndex (
'ALGOLIA_INDEX_NAME' ,
[ 'insidePolygon' => [
[
42.01 ,
- 124.31 ,
48.835509470063045 ,
- 124.40453125000005 ,
45.01082951668149 ,
- 65.95726562500005 ,
31.247243545293433 ,
- 81.06578125000004 ,
25.924152577235226 ,
- 97.68234374999997 ,
32.300311895879545 ,
- 117.54828125 ,
],
],
],
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
response = client . search_single_index (
index_name = "ALGOLIA_INDEX_NAME" ,
search_params = {
"insidePolygon" : [
[
42.01 ,
- 124.31 ,
48.835509470063045 ,
- 124.40453125000005 ,
45.01082951668149 ,
- 65.95726562500005 ,
31.247243545293433 ,
- 81.06578125000004 ,
25.924152577235226 ,
- 97.68234374999997 ,
32.300311895879545 ,
- 117.54828125 ,
],
],
},
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
response = client . search_single_index (
"ALGOLIA_INDEX_NAME" ,
Algolia :: Search :: SearchParamsObject . new (
inside_polygon: [
[
42.01 ,
- 124.31 ,
48.835509470063045 ,
- 124.40453125000005 ,
45.01082951668149 ,
- 65.95726562500005 ,
31.247243545293433 ,
- 81.06578125000004 ,
25.924152577235226 ,
- 97.68234374999997 ,
32.300311895879545 ,
- 117.54828125
]
]
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
val response = client . searchSingleIndex (
indexName = "ALGOLIA_INDEX_NAME" ,
searchParams = Some (
SearchParamsObject (
insidePolygon = Some (
Seq (
Seq ( 42.01 , - 124.31 , 48.835509470063045 , - 124.40453125000005 , 45.01082951668149 , - 65.95726562500005 ,
31.247243545293433 , - 81.06578125000004 , 25.924152577235226 , - 97.68234374999997 , 32.300311895879545 ,
- 117.54828125 )
)
)
)
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let response : SearchResponse < Hit > = try await client . searchSingleIndex (
indexName : "ALGOLIA_INDEX_NAME" ,
searchParams : SearchSearchParams . searchSearchParamsObject ( SearchSearchParamsObject ( insidePolygon : [[
42.01 ,
- 124.31 ,
48.835509470063045 ,
- 124.40453125000005 ,
45.01082951668149 ,
- 65.95726562500005 ,
31.247243545293433 ,
- 81.06578125000004 ,
25.924152577235226 ,
- 97.68234374999997 ,
32.300311895879545 ,
- 117.54828125 ,
]]))
)
The empty query (''
) returns all matching airports.
© Algolia · Privacy Policy · Cookie settings