Area: Optimizely Search & Navigation
Applies to versions: 12 and higher
Other versions:

Geographic distance facets

Recommended reading 

Facets are used to group documents based on some crtiteria, for example specific terms or date range. This topic explains how to use facets based on geographical distance.

How it works

Geographic distance facets group documents with a GeoLocation type property by distance from a location. Geographical distance facets are requested of, and extracted from, search results via the GeoDistanceFacetFor method.

Geographic distance facets need several ranges into which to group documents. Use the the NumericRange class to do this.


Assume you indexed a number of restaurants as instance of this class:

using EPiServer.Find;

public class Restaurant
    public string Name { get; set; }
    public GeoLocation Location { get; set; }

Request the number of restaurants within 1, 5, and 10 kilometers of a location via the GeoDistanceFacetFor method, as shown below.

var sergelsTors = new GeoLocation(59.33265, 18.06468)
var ranges = new List<NumericRange>
    new NumericRange {From = 0, To = 1},
    new NumericRange {From = 0, To = 5},
    new NumericRange {From = 0, To = 10}

result = client.Search<WithCoordinates>()
  .GeoDistanceFacetFor(x => x.Location, sergelsTorg, ranges.ToArray())

To extract the facet from the results variable, use the GeoDistanceFor method (same name as the method used to retrieve facets). This returns an instance of the GeoDistanceFacet class, which contains an instance of the GeoDistanceRangeResult per requested range.

facet = result.GeoDistanceFacetFor(x => x.Location);

foreach (var range in facet)
    Console.WriteLine("There are " + range.TotalCount 
                    + " restaurants within "
                    + range.To 
                    + " of Sergels Torg");
Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 31, 2016

Recommended reading