Integrating Lucene.Net.Contrib.Spatial with Sitecore Search API (Geo-Spatial Search)



Note: Newer release is published on github that support sitecore 8.1 & 8.2 and fixes the issue with location results being cached, Go to this post to read about the details of the new release

I have created an Extension to Sitecore.ContentSearch API which uses Lucene.Net.Contrib.Spatial library to generate spatial queries.


Currently, i added a method "WithinRadius()" to retrieve locations within specific radius (In miles) of center point , the results will be sorted based the distance from the center point.

This extension assumes that you have Latitude and Longitude stored as fields in your location items.

Installation:
 You can either get the source from github or install nuget package.

    Marketplace:
  1. Go to Sitecore Marketplace and download "Lucene Spatial Search Support" package.
  2. Install the package using Sitecore Installer wizard
Configurations:
  1. Go to 'Sitecore.ContentSearch.Spatial.config' file in your include folder, you will need to add your location templates details, like template ID, Latitude field name, longitude field name.
    You can multiple templates for locations in case you have different locations templates
  2.  Rebuild your 'sitecore_master_index' and 'sitecore_web_index' indexes from Sitecore control panel

How to use:
  1. Include the following name spaces in your class
    using Sitecore.ContentSearch.Spatial;
    using Sitecore.ContentSearch.Spatial.SearchTypes;
  2. You will need to 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));
    }
I will include more spatial queries in the future! Let me know in the comments if you have any question.
Bonus 'Geo Location Field Type':
This module comes with Geo Location field type which make it easier to search and pick locations with sitecore content editor, it uses google maps API to search locations and render maps:
Note: if you installed the module using Nuget, you need to install "Geo Location Data Type-1.0.zip" package which is located in "Website\packages\Sitecore.ContentSearch.Spatial.1.1.0.0\content\Package-GeoLocation"
How to use :
  1.  Go to the template that you want to add Geo Location field to, and add this template "/sitecore/templates/User Defined/Geo Spatial/Spatial Point" as base template.
  2. Now you can search and pick locations directly from Content editor.
There is no need to include templates that uses this field in Templates section in 'Sitecore.ContentSearch.Spatial.config file.

Comments

Popular posts from this blog

Attaching a file on WFFM Send email action

Solr with Sitecore Checklist

Set Workflow using Item Buckets Search Operations