_geoloc Polygon AND instead of OR

Hello, we use Algolia services and on one of our indexes, we have an attribute _geoloc with two objects inside that corresponds to a departure address and an arrival address.

I use your helper to search in a polygon. it works very well however I receive in the results all the records that we either the starting address or the address to arrive in the polygon. it is therefore a condition in OR that is by default, I would simply like this one to be in AND in order to receive only the records that we both locate in the polygon

Thank you in advance

Hi @eth3rnit3,

Welcome to the Algolia Community!

Algolia checks for points within the given polygon and doesn’t allow for AND or OR conditions within the _geoloc search.

Since you have two _geoloc points, if either of those match, then the record will be considered a match, and return the record as a result.

One way to achieve the results you desire, would be to create two different indices, one for departure locations and one for arrival locations.

To get results where both match, search both indices which will give you two different result sets.

Create a union of those result sets by comparing the results of each to see which results are in both sets, and then return those matching records to the user.

Let us know how this works for you!

Hello Cyndy and thank you for your help.

So if I understood correctly, I should create two new indexes. One containing the starting address and the other containing an arrival address. I request both and make a UNION of the results.

Is it possible to build the request on Algolia so that my frontend application has only one request to make? Also, is it possible to manage the pagination with Algolia?

Because we think we have an average of 2000 results per request, it would be too complicated and not really efficient to have to manage all this on the frontend side.

Développeur Web et Mobile
M: +336-62-60-90-48
eth3rnit3@gmail.com - www.dmkreation.com
Chemin de l’oratoire
LinkedIn icon

Hi @eth3rnit3,

I answered your question through our support email address, but will answer here too in case someone else needs the response.

To keep the search fast, Algolia does all ‘building’ at indexing time and for _geoloc, only finds points in the selected polygon. If there are multiple ‘_geoloc’ on the record, if any are matches, the record will be part of the result set.

To make sure that both points are in the selected polygon, you would need to separate the two _geoloc locations into separate records or even separate indices.

I understand the two indices may be a lot of processing on your fronted.

Another option that may work better for you, depending on your use case:

You can separate each record that you currently have in your index, into two records with the same information on each record, except for the _geoloc information.
In other words, have one record for departure and one record for arrival:

Current record:

{ "title": "record 1", // with two _geoloc 
     ...  // other attributes
      "_geoloc": [
           { "lat": 43.212498, "lng": 2.350351 }, 
           { "lat": 43.837241, "lng": 4.35804 }


     "title": "record 1", // departure 
      ... // other attributes all the same on each of these two records 
     "_geoloc": { "lat": 43.212498, "lng": 2.350351 }, 
     "title": "record 1", // arrival 
      ... // other attributes all the same on each of these two records 
     "_geoloc": { "lat": 43.837241, "lng": 4.35804 } 

Using the ‘distinct’ parameter, group the records into pairs, by one of the attributes that are the same on each of these new records, but is unique to this new pair of records (title?), so that you can group these records into pairs.

If you set the ‘distinct’ parameter to ‘2’, it will return both records in the search results, when both _geoloc are found.

Setting ‘distinct’ equal to 2, will also return the ‘_distinctSeqId’ property in your results.

This property will be ‘0’ on the first matching record of the pair and ‘1’ on the second matching record of the pair, when both records are found (both departure and arrival).

If only one of the records is found in the pair, that record’s ‘_distinctSeqId’ property will be set to ‘0’ in your results.

Since you only want the results that have two records (both arrival and departure), and all other information is the same, you can eliminate all the records in your results that have ‘_distinctSeqId’ = 0, which would leave only those records with ‘_distinctSeqId = 1’.

To Summarize:

  • Separate each record into two records: one for departure, one for arrival
  • Set ‘distinct’ to 2 and the ‘attributeForDistinct’ to an attribute that will be the same on each of those two new records.

This can be done in your dashboard:

Or in your code:

results = index.search('query', { distinct: 2 })

  • In your frontend code, remove or hide hits that have ‘_distinctSeqId’ = 0, leaving only hits that have ‘_distinctSeqId = 1’.
    If only one match was found in the pair, either departure or arrival, but not both, then you will only have one match for that pair, with ‘_distinctSeqId’ = 0.
    So, any hit with ‘_distinctSeqId = 1’ means that both arrival and departure records were found as a match.
  • Display only these ‘_distinctSeqId = 1’ records to the user, which would represent that both the arrival and departure locations were found in the polygon.

Let us know how this works for you!


Ok, you can make this thing working BUT we can not paginate with that. I mean, we can not get the next 20 results. We do not know how many results it will remain after the filter.

An idea how to handle this? Thx