In today’s digital world, businesses are increasingly leveraging location-based data to enhance user experiences. Whether it’s helping customers find nearby stores or delivering personalized content based on a user’s geographical location, geospatial search plays a pivotal role in providing relevant results. Sitecore's robust search features allow for seamless integration of geospatial capabilities, making it easier than ever to implement geospatial search in your digital solutions.
What is Geospatial Search?
Geospatial search refers to the ability to filter and retrieve content based on geographical data, such as latitude and longitude. This functionality is essential for industries where proximity is important, such as retail, healthcare, real estate, and logistics. Sitecore’s search engine makes it easy to filter content based on location, offering users results that are directly relevant to their current or specified geographic area.
How to Implement Geospatial Search in Sitecore
In this section, we’ll walk through how to implement geospatial search using Sitecore's search API.
Step 1: Indexing Geospatial Data
To get started, we need to create a custom field in Sitecore to store geographical coordinates. In this case, we created a field named Coordinates
with a GEO
data type, which can store latitude and longitude values.
Here’s an example of indexing a record for a doctor with their location. In this case, I used Sitecore Search's PUSH source and Postman to push dummy data. The following JSON is the sample input used to feed the PUSH API via Postman:
1{
2 "document": {
3 "id": "1DrJohnDoe",
4 "fields": {
5 "name": "Dr. John Doe",
6 "description": "Lorem ipsum sit a dor, lorem ipsum sit a dor, lorem ipsum",
7 "type": "doctor",
8 "coordinates": "40.7127837,-74.0059413"
9 }
10 }
11}
This JSON document represents a doctor, with the coordinates
field storing the latitude and longitude of their practice. This geospatial data will later be used to filter search results based on proximity.
Step 2: Modifying the Search API for Geo Filtering
Once the data is indexed, we need to filter search results based on the user’s location. The following code snippet demonstrates how to modify the Search API SDK to include geospatial filters:
1useSearchResults<ArticleModel, InitialState>({
2 query: (query): any => {
3 const latitude = 40.2404; // User's latitude
4 const longitude = -75.1652; // User's longitude
5 const radius = '100km'; // Search radius (100 km)
6 const sourceId = '000000'; // Source ID of PUSH API
7
8 const request = query.getRequest();
9 const geofilter = new FilterGeo('coordinates', radius, latitude, longitude).toJson();
10 geofilter['type'] = 'geoDistance';
11
12 // Conditionally add geo filter if coordinates are available
13 if (latitude && longitude) {
14 request.setSearchFilter(
15 new FilterAnd([new FilterEqual('name', 'Dr. John Doe'), geofilter])
16 );
17 }
18
19 // Conditionally add source ID of PUSH Source
20 if (sourceId) {
21 request.setSources([sourceId]);
22 }
23 }
24});
In this code, the latitude
and longitude
values represent the location of the user making the search. The FilterGeo
function is used to filter the search results by geospatial data, with the geoDistance
filter type ensuring that results are ordered based on proximity.
Why Geospatial Search is Important
Geospatial search is particularly valuable for businesses that depend on location-specific data. Here are some key reasons why implementing geospatial search can enhance your business:
-
Proximity-Based Results: Geospatial search allows users to find nearby services, stores, or content based on their current location, improving the overall user experience.
-
Personalized User Experience: By integrating location-based filtering, you can provide more relevant search results, ensuring that users find exactly what they need, when they need it.
-
Better Engagement: Providing users with content that’s relevant to their location can increase engagement, especially in industries such as retail and healthcare, where local presence is key.
-
Operational Efficiency: Geospatial search can streamline business operations by allowing for easy display and management of localized content, improving service delivery.
Conclusion
Geospatial search is a powerful feature in Sitecore that enhances the user experience by delivering more relevant and localized content. Whether you're working in retail, healthcare, or any other industry that relies on geographic data, implementing geospatial search can make a significant impact on your site's search functionality.
By leveraging Sitecore’s search API and incorporating geo filters into your queries, you can provide users with a more personalized and location-aware experience. As businesses continue to rely on geospatial data, integrating such features into your digital solutions will only become more critical for success.