Filtering multiple geolocation

Hello,

Let’s say I have a transportation app where drivers can put the departure location (city) and the desired destination location(city) for a trip.

I want to add search capabilities based on the geolocation. In the doc, we should create a _geoloc attribute to be able to filter by geolocation. But in my case, I have two locations departure and destination.

{
  name: 'trip',
  departure: {
    name: 'paris',
    lat: 1234,
    long: 1234
  },
  desiredDestinations: {
    name: 'brussels',
    lat: 1234,
    long: 1234
  }
};

How can I search for a trip that leave around Paris (200 miles radius) and go to Brusells (400 miles radius)?

Now a more complicated scenario, the driver can have multiple desired destinations:

{
  name: 'trip',
  departure: {
    name: "paris",
    lat: 1234,
    long: 1234
  },
  desiredDestinations: [
    {
      name: "brussels",
      lat: 1234,
      long: 1234
    },
    {
      name: "Berlin",
      lat: 1234,
      long: 1234
    }
  ]
}

a seach with departure:paris and destination:brusells should return this document as well as a search with departure:paris and destination:berlin.

How Can I achieve that?

Thanks

Hi @patrick.finken,

Welcome to the Algolia Community!

I understand you have multiple geo locations on a record, with arrivals and destinations. The search should return matches when both a departure and one of the destinations both match. Is that correct?

The Algolia geosearch will return a match if any of the geolocations match for a record.

To solve this, you can separate each record that you currently have in your index, into multiple records with the same information on each record, except for the _geoloc information.

In other words, have one record for departure and one or more records for destinations.

In my example, I am using departure and arrival, with only one arrival destination, but you can adjust for multiple destinations.

In your current records, you have multiple geolocations:

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

Separate them into two records (more with multiple destinations so that you have one ‘_geoloc’ per record):

{ 
     "title": "record 1", // departure
     "type": "departure",
      ... // other attributes all the same on each of these two records 
     "_geoloc": { "lat": 43.212498, "lng": 2.350351 }, 
}, 
{ 
     "title": "record 1", // arrival
     "type": "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 (or sets), by one of the attributes that are the same on each of these new records, but is unique to this new pair (or set) of records (title in this example), so that you can group these records into pairs (or sets).

If you set the ‘distinct’ parameter to ‘2’ or higher, it will return all records that match in the search results. You can check the ‘type’ attribute (in my example) to see if there is both an arrival and a departure present in the set. You will only need to display one of the records for the ‘trip’.

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

This property will be ‘0’ on the first matching record of the set, ‘1’ on the second matching record of the set, ‘2’ on the third matching record in the set, etc.

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

This ‘_distinctSeqId’ property may be helpful to see if you have a full trip as you will need at least 2 records to match in the set, and then check to make sure that one record is for a departure.

To Summarize:

  • Separate each record into two records: one for departure, one for arrival

  • Set ‘distinct’ to 2 (or the max number of destinations you might have), and the ‘attributeForDistinct’ to an attribute that will be the same on each of those two new records (‘title’ in my example).
    This can be done in your dashboard:
    Screen-Shot-2019-09-12-at-2-51-06-PM.png

Or in your code:

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

  • Set an attribute to distinguish between ‘departure’ and ‘destination’ on each record.

  • In your frontend code, determine if you have both a departure and a destination in a set.

  • Display only one of the records in a matching set to your user.

Keep in mind: The larger the ‘distinct’ parameter, the slower your search will be. I would try to keep the number to 4 or less, the smaller the better.

Let us know how this works for you!

Hello Cindy,

Thanks for your answer. I will try that and I will let you know.