2
votes

I have a template with latitude and longitude field. I want to able to use lucene to search for records within given radius. I haven't found any code sample that works with sitecore 8.2. I've been told to use solr which is more feature rich but solr seems to complex for my needs as I'm not dealing with huge records.

Can someone guide me on how I can go about creating my own custom contentsearch library?

Thanks.

1
  • Are you getting an error message? I've just checked the bugs on github and there appears to be one reported from 8.1 from you. Is the error the same in 8.2 or a different one? Commented Oct 5, 2016 at 4:15

2 Answers 2

5
votes

You may have a look at the module LUCENE SPATIAL SEARCH SUPPORT on the Sitecore Marketplace. There is a link to the github repo which should give you an example of how that was done for Sitecore 7; hopefully not much has changed.

The creator built a library referencing these Lucene libraries:

  • Lucene.Net.Contrib.Spatial.dll
  • Lucene.Net.dll
  • Spatial4n.Core.dll

Then built an API on top with the appropriate ContentSearch configuration files. Perhaps not too straight forward but can be done.

As an aside, the Sitecore Experience Accelerator (SXA) comes with this feature built in for Lucene using a similar approach.

1
  • I've tried the github project cannot get it to work, I've raised an issue. I'll look into SXA. Thanks
    – Huzzi
    Commented Oct 4, 2016 at 21:40
4
votes

I haven't used the module in 8.2, but in 8.1 I had to change LuceneSearchWithSpatialContext.cs and remove the following constructor

protected LuceneSearchWithSpatialContext(ILuceneProviderIndex index,  CreateSearcherOption options = CreateSearcherOption.Writeable, SearchSecurityOptions securityOptions = SearchSecurityOptions.EnableSecurityCheck) :base(index,options,securityOptions)
{
    Assert.ArgumentNotNull(index, "index");
    this.index = index;
    this.settings = this.index.Locator.GetInstance<IContentSearchConfigurationSettings>();
}

Update the Sitecore.ContentSearch.Spatial.config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearchSpatial>
      <IncludeTemplates>
        <!-- * Include your location template details, like template id, latitude field name and longitude field name
             * You can include multiple templates 
        -->
        <!--<Template id="{TEMPLATE ID}" LatitudeField="[Latitude Field Name]" LongitudeField="[Longitude Field Name]"></Template>-->
      </IncludeTemplates>
    </contentSearchSpatial>
    <contentSearch>
      <indexConfigurations>
        <spatialLuceneIndexConfiguration ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration"  type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
          <documentBuilderType>Sitecore.ContentSearch.Spatial.Indexing.LuceneSpatialDocumentBuilder, Sitecore.ContentSearch.Spatial</documentBuilderType>
        </spatialLuceneIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

Your index should use the following configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="index_id" type="Sitecore.ContentSearch.Spatial.Provider.Lucene.LuceneIndexWithSpatial, Sitecore.ContentSearch.Spatial">
            ...
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>
5
  • I'll give this a try and let you know. Thanks Kelly.
    – Huzzi
    Commented Oct 5, 2016 at 8:38
  • I'm getting Current Index is not configured to use Spatial Search. For some reason the second constructor LuceneSearchWithSpatialContext never gets hit.
    – Huzzi
    Commented Oct 5, 2016 at 8:57
  • Are you able to post your index config file? Commented Oct 5, 2016 at 20:53
  • SE doesn't allow me to copy all the code. For the DocumentBuilderType i'm using <documentBuilderType>SitecoreBasic.Components.Indexing.Spatial.LuceneSpatialDocumentBuilder, SitecoreBasic</documentBuilderType>. I believe the indexing is working when I view the index I can see __location__x and __location__y values
    – Huzzi
    Commented Oct 6, 2016 at 10:17
  • I've updated my answer with some further config changes that might help. Commented Oct 6, 2016 at 22:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.