Sitecore Spatial/Geo Location Search Module

Previously i have released Sitecore.ContentSearch.Spatial module that support applying Geo-location based search using Sitecore ContentSearch API, That release had some issues with results getting cached and not updating when you publish the location items or make changes to them, Also it required that you use different [documentBuilderType] than Sitecore default.

In the new release you only to update your index to use (Sitecore.ContentSearch.Spatial.Provider.Lucene.LuceneIndexWithSpatial) class, and to add two computed fields configurations to your Index configurations, and that's it!

To install this module,

  1. Download or clone this repository on github to your machine, As of now, i'm using  references for Sitecore 8.2 -Update3 from Sitecore Nuget feed, You can update the references to match your Sitecore versions using Nuget Manager.
  2. Build the solution and copy the following dlls from the bin folder to your project:
    1. Sitecore.ContentSearch.Spatial.dll
    2. Spatial4n.Core.dll
    3. Lucene.Net.Contrib.Spatial.dll
  3. Copy "Sitecore.ContentSearch.Spatial.config" file into your include folder and change the following
    1. In the "IncludeTemplates" section, add the details of your location template in sitecore, In order for this module to work, You need to have longitude and latitude in that template.
    2. In the "Indexes" section, update the index name with your index name, This is to change the default LuceneIndex with LuceneIndexWithSpatial class.
    3. You will notice in the patch config file that i have added two computed fields to the defaultLuceneIndexConfiguration, spatialgeolocation__x and spatialgeolocation__y, Don't change the name of these fields, IF you are not using the defaultLuceneIndexConfiguration for your index, make sure these two fields are added to your custom Lucene Index configurations

How to use this module:

  1. After you have deployed the dlls and the config patches from previous steps, you need to rebuild your index.
  2. Include the following namespaces in your code
    using Sitecore.ContentSearch.Spatial;
    using Sitecore.ContentSearch.Spatial.SearchTypes;
  3. You will need use extension method 'GetExtendedQueryable' rather than 'GetQueryable', and also to use 'SpatialSearchResultItem' class as your POCO:
    using (var context = ContentSearchManager.GetIndex(indexname).CreateSearchContext())
         double radious = 150;
         double lat = 40.547608;
         double lng = -74.481771;
         var query = context.GetExtendedQueryable<SpatialSearchResultItem>()
                            .Where(item => item.GeoLocation.WithinRadius(lat, lng, radious));
    You can add SpatialSearchResultItem as your base class if you already have a custom POCO class
Note: I could not get sorting by distance from the passed point coordinates (Lat&Lng) using Lucene DistanceValueSource(), It always came back random, could be bug, However i was able to come up with this workaround, In the list returned from query.ToList(), You can sort results like this:

    query = query.ToList().OrderBy(x=>x.Distance(lat,lng))

This will get you a list of locations sorted from nearest to farthest from your point (lat & lng coordinates)

Note: This was tested on Sitecore 8.1 and 8.2, I will testing this with Sitecore 8.0 and may publish separate release for it

Let me know in the comments if you are having any issue!


  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging. Primavera Course in Chennai | primavera online training


Post a Comment

Popular posts from this blog

Attaching a file on WFFM Send email action

Solr with Sitecore Checklist

Set Workflow using Item Buckets Search Operations