public object ComputeFieldValue(Sitecore.ContentSearch.IIndexable indexable) { var indexableItem = indexable as SitecoreIndexableItem; if (indexableItem == null) return null; var item = (Sitecore.Data.Items.Item)indexableItem; if (item == null) return null; var latLon = item[SourceFieldName]; if (latLon == "" || latLon == "NULL") return null; // Convert from georss point to normal X, Y format latLon = latLon.Replace(" ", ", "); var spatialContext = SpatialContext.GEO; var geohashTree = new GeohashPrefixTree(spatialContext, 10); var strategy = new RecursivePrefixTreeStrategy(geohashTree, FieldName); var shape = spatialContext.ReadShape(latLon); var grid = strategy.GetGrid(); int levelForDistance = grid.GetLevelForDistance(strategy.DistErrPct); IList<Node> list = grid.GetNodes(shape, levelForDistance, true); return list.Select(node => node.GetTokenString()); }
public void CreateSpatialFilterAndWeight(PointRadiusCriterion geoFilter, Filter currentFilter, Weight currentWeight) { var spatialContext = SpatialContext.GEO; var geohashTree = new GeohashPrefixTree(spatialContext, 10); var strategy = new RecursivePrefixTreeStrategy(geohashTree, geoFilter.FieldName); var point = spatialContext.MakePoint(geoFilter.Longitude, geoFilter.Latitude); var spatialArgs = new SpatialArgs(SpatialOperation.Intersects, spatialContext.MakeCircle(point, DistanceUtils.Dist2Degrees(geoFilter.RadiusKm, DistanceUtils.EARTH_MEAN_RADIUS_KM))); var circle = spatialContext.MakeCircle(point, DistanceUtils.Dist2Degrees(geoFilter.RadiusKm, DistanceUtils.EARTH_MEAN_RADIUS_KM)); var circleCells = strategy.GetGrid().GetWorldNode().GetSubCells(circle); var luceneFilters = new List<Filter>(); if (currentFilter != null) luceneFilters.Add(currentFilter); var tempSpatial = strategy.MakeFilter(spatialArgs); luceneFilters.Add(tempSpatial); if (geoFilter.Sort != PointRadiusCriterion.SortOption.None) { var valueSource = strategy.MakeDistanceValueSource(point); var funcQ = new FunctionQuery(valueSource); // this is a bit odd... but boosting the score negatively orders results if (geoFilter.Sort == PointRadiusCriterion.SortOption.Ascending) { funcQ.Boost = -1; } spatialWeight = funcQ.CreateWeight(this); spatialWeight.GetSumOfSquaredWeights(); luceneFilters.Add(new QueryWrapperFilter(currentWeight.Query)); } spatialFilter = new ChainedFilter(luceneFilters.ToArray(), 1); }