Spatial search with Sitecore 9.x and SOLR 7.2
- Sridharan Padmanabhan
- Nov 25, 2023
- 1 min read
Updated: Dec 13, 2024
First things first, this is an out-of-the-box feature than can be implemented with very little efforts.
Following are the things you would need to add to your application —
An item template inheriting /sitecore/templates/System/Geospatial/Coordinate.
A computed field (AbstractComputedIndexField) to store the coordinates of the solr type SpatialRecursivePrefixTreeFieldType
The Sitecore template /sitecore/templates/System/Geospatial/Coordinate comprises of two “double” fields latitude and longitude.
A custom computed field like the one shown below -
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.ContentSearch.Data;
using Sitecore.Data;
namespace yourNameSpace{
public class CoordinateComputedIndexField : AbstractComputedIndexField{
public override object ComputeFieldValue(IIndexable indexable){
var indexableItem = indexable as SitecoreIndexableItem;
var item = indexableItem?.Item;
if (item?.Database == null) return null;
double latitude = 0;
double longitude = 0;
var parsed = double.TryParse(item[ID.Parse(Constants.Templates.Facility.Fields.Latitude)], out latitude);
parsed = parsed && double.TryParse(item[ID.Parse(Constants.Templates.Facility.Fields.Longitude)], out longitude);
return parsed ? new Coordinate(latitude, longitude) : null;}}}
Corresponding computed field configuration -
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
<sitecore search:require="solr">
<contentSearch>
<indexConfigurations>
<defaultSolrFacilitiesIndexConfiguration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
<documentOptions ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/documentOptions">
<fields hint="raw:AddComputedIndexField">
<field fieldName="coordinate" returnType="string">ExampleTenant.Foundation.Facilities.Search.ComputedFields.CoordinateComputedIndexField, ExampleTenant.Foundation.Facilities</field>
</fields>
<include hint="list:AddIncludedTemplate">
<myTemplate>{086b3834-be01-4d3e-b1e2-3827a2010ddd}</myTemplate>
</include>
</documentOptions>
</defaultSolrFacilitiesIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
You should see a computed field coordinate_rpt created on your documents with corresponding latitude,longitude
You can use this field as your “Spatial Field” and run any kind of spatial operations on this field, refer blog posts below for more details on spatial search -
Additional references:

Comments