In this tutorial, we’ll see how we can filter results inside a rectangle 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.

json
[
  {
    "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

Configure Index Settings

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.

First, let’s configure the index.

Searchable attributes

We’re going to search in our 4 textual attributes: name, city, country and iata_code.

Custom Ranking

We’ll use the number of other connected airports to any airport as a ranking metric. The more connection the better.

Filtering inside a rectangle area

Let’s filter inside the United State of America. The USA can be considered as a rectangle:

To filter inside this rectangle we need the upper-left and bottom-right latitude and longitude:

  • 49.067996905313834, 65.73828125
  • 25.905859247243498, 128.8046875

We are going to use the insideBoundingBox parameter:

We are using the empty query ('') to tell that we want all airports.