2

There is documentation on FilterGeo but how to do multiple geo filtering?

Also, GEO datatype documented here seems to be single latitude and longitude pair and not an array of latitude and longitude.

My requirement is radius search on multiple points.

2 Answers 2

2

We can store multiple coordinates on same object by defining multiple GEO attributes in Sitecore Search and pairing them with FilterOr in the code

In below example, we have three coordinates attributes like so locationcoordinates0, locationcoordinates1, and locationcoordinates2

Code snippet:

// Get request context
const context = PageController.getContext();

// Get coordinates to filter from that user is looking for.
const coordinate = { lat: props.location.lat, lon: props.location.lng };

// Convert distance from miles to km using helper function
const distance = `${convertMilesToKM(router?.query?.r as string)}km`;

// Define filter one
const geoFilterA = new FilterGeo('locationcoordinates0', distance, coordinate);

// Define filter two
const geoFilterB = new FilterGeo('locationcoordinates1', distance, coordinate);

// Define filter three
const geoFilterC = new FilterGeo('locationcoordinates2', distance, coordinate);

// Combine all Geo filter object into one FilterOr
const filterOr = new FilterOr([geoFilterA, geoFilterB, geoFilterC]);

// Set Geo data into request context as well (this may be optional for filtering but is important for distance sorting)
context.setGeoLocation(coordinate);

// Run the search query
query
  .getRequest()
  .setSearchFilter(filterOr)
  .setSearchSortCriteria({ name: 'locationdistancesort', order: 'asc' });
2

You can also try FilterGeoWithin , It is mainly for multiple location search

 const coordinates = [
      { lat: -28.281932, lon: -55.573635 },
      { lat: -31.254713, lon: -50.928666 },
      { lat: -16.181724, lon: -47.217881 }
    ];
 query.getRequest().setSearchFilter(new FilterGeoWithin('location', '7km',coordinates));

Use FilterGeo when: You need pinpoint accuracy or want to focus on a single point. This is ideal for searches that relate to a fixed point, like finding services or items closest to a user’s current location.

Use FilterGeoWithin when: Your search criteria require covering an area rather than a point. This is great for regional promotions, events, or when you want to include multiple neighborhoods or parts of a city in your search scope.

2
  • 1
    FilterGeoWithin won't fit in my scenario. Because it says - "Creates a filter object that excludes items outside the polygon created by an array of coordinates." in Sitecore documentation.
    – Harsh Baid
    Commented Sep 26 at 23:35
  • 1
    My requirement isn't polygon search but radius search on multiple points.
    – Harsh Baid
    Commented Sep 26 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.