3

We have added attribute called locationcoordinate of GEO datatype to the result item and crawled and are able to see latitude and longitude value in it.

We have added filter and sort options in Sitecore Search but it is sorting by number value in latitude and longitude in the GEO attribute.

How can we sort by distance to show closest result item from a point first?

2 Answers 2

2

Solution:

To sort by distance, we need to pass the GEO data in the request context.

Code snippet to set GEO data in request context:

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

// set co-ordinate from props (which we are getting from Google API from search input box)
const coordinate = { lat: props.location.lat, lon: props.location.lng };

// Call helper function to convert miles to km as our search was based on miles metric for radius filtering.
const distance = `${convertMilesToKM(router?.query?.r as string)}km`;

// set geo filtering
const geoFilter = new FilterGeo('locationcoordinate', distance, coordinate);

// set geo data in request context
context.setGeoLocation(coordinate);

// Run the Search query with Sorting option that uses the GEO attribute
query
    .getRequest()
    .setSearchFilter(geoFilter)
    .setSearchSortCriteria({ name: 'locationdistancesort', order: 'asc' });

context.setGeoLocation(coordinate);

Note: Setting the sorting criteria is important along with GEO data that is in the Request context and sorting name locationdistancesort in above code is defined as Sorting options in Sitecore Search portal under Administration > Domain settings

Code snippet to reset GEO data in request context:

Once GEO data is set in request context Sitecore Search will display results sorted by distance from point A to co-ordinates stored on the result item.

context.resetGeoLocation();

Resetting will be useful when same search interface has radio button (or some selection) to switch between location-based search and text-based search

Some reference links:

GEO data is discussed here

GEO datatype is discussed here

Creating sorting option is discussed here

Note: This is not officially documented in Sitecore doc site yet (as of 9/25/2024).

2

There are to two types of Geo-Location Filters

  1. FilterGeo : This filter uses a single point, defined by latitude and longitude, to find items near that specific location. It’s perfect when you want to show results centered around a single known point, like a store or landmark.

This will set your circle radius in between it will geometrically search (Single lat and log in object)

const geoFilter = new FilterGeo('location', '7km', {lat: 22.8,lon: 99,});

and to set this filter and call sitecore search API

query.getRequest().setSearchFilter(geoFilter);
  1. FilterGeoWithin : This filter uses multiple points to create a polygonal area. It returns items located within this specified area. It’s useful for more complex scenarios where you need to include a larger, specific area rather than a single point.

This will set your circle radius in between it will geometrically search (multiple lat and long in array of object)

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

and to set this filter and call sitecore search API

query.getRequest().setSearchFilter(geoFilter);

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.