Skip to content

Instantly share code, notes, and snippets.

@ismailmayat
Created November 13, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ismailmayat/3902c660527c8b3d20b38ae724ab9892 to your computer and use it in GitHub Desktop.
Save ismailmayat/3902c660527c8b3d20b38ae724ab9892 to your computer and use it in GitHub Desktop.
document writing event
using System.Collections.Generic;
using System.Globalization;
using Examine;
using Examine.LuceneEngine;
using Examine.LuceneEngine.Providers;
using ExamineContrib.Spatial.Model;
using Lucene.Net.Documents;
using Lucene.Net.Spatial.Tier.Projectors;
using Lucene.Net.Util;
using Umbraco.Core;
namespace ExamineContrib
{
public class Init : IApplicationEventHandler
{
readonly Dictionary<int, CartesianTierPlotter> _plotters = new Dictionary<int, CartesianTierPlotter>();
private readonly int _startTier;
private readonly int _endTier;
/// <summary>
/// cartesian stuff nicked from
/// https://www.leapinggorilla.com/Blog/Read/1010/spatial-search-in-lucenenet---worked-example
/// </summary>
public Init()
{
IProjector projector = new SinusoidalProjector();
var plotter = new CartesianTierPlotter(0, projector,
Constants.Fields.LocationTierPrefix);
_startTier = plotter.BestFit(Constants.MaxKms);
_endTier = plotter.BestFit(Constants.MinKms);
for (var tier = _startTier; tier <= _endTier; tier++)
{
_plotters.Add(tier, new CartesianTierPlotter(tier, projector, Constants.Fields.LocationTierPrefix));
}
}
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
WireupSpatialInjection();
}
private void WireupSpatialInjection()
{
//note difference here compared to ExamineEvents line 33
var locationIndexer = (SimpleDataIndexer)ExamineManager.Instance.IndexProviderCollection["TownIndexer"];
locationIndexer.DocumentWriting += locationIndexer_DocumentWriting;
}
private void locationIndexer_DocumentWriting(object sender, DocumentWritingEventArgs e)
{
if (IsSpatialAndHasGeoCodeValues(e))
{
double latitude = double.Parse(e.Fields["latitude"], CultureInfo.InvariantCulture);
double longitude = double.Parse(e.Fields["longitude"], CultureInfo.InvariantCulture);
var location = new Location(e.NodeId,e.Fields["name"],new Coordinate(latitude,longitude));
if (location.Coordinates != null)
{
e.Document.Add(new Field(Constants.Fields.HasGeoCode, Constants.FieldFlags.HasField, Field.Store.NO, Field.Index.NOT_ANALYZED));
e.Document.Add(new Field(Constants.Fields.Latitude, NumericUtils.DoubleToPrefixCoded(location.Coordinates.Latitude), Field.Store.YES, Field.Index.NOT_ANALYZED));
e.Document.Add(new Field(Constants.Fields.Longitude, NumericUtils.DoubleToPrefixCoded(location.Coordinates.Longitude), Field.Store.YES, Field.Index.NOT_ANALYZED));
AddCartesianTiers(location.Coordinates, e.Document);
}
}
}
private static bool IsSpatialAndHasGeoCodeValues(DocumentWritingEventArgs e)
{
return e.Fields.ContainsKey("latitude") && !string.IsNullOrEmpty(e.Fields["longitude"]);
}
private void AddCartesianTiers(Coordinate cord, Document document)
{
for (var tier = _startTier; tier <= _endTier; tier++)
{
var ctp = _plotters[tier];
var boxId = ctp.GetTierBoxId(cord.Latitude, cord.Longitude);
document.Add(new Field(ctp.GetTierFieldName(),
NumericUtils.DoubleToPrefixCoded(boxId),
Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment