Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely Search & Navigation
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Introduction

Geographical distance facets group documents with a property of type GeoLocation by their distance from a given location. Geographical distance facets are requested, and extracted from search results by a method named GeoDistanceFacetFor. Just like range facets geographical distance facets need a number of ranges, represented by the NumericRange class, to group documents into.

Examples

Assuming we have indexed a number of restaurants as instance of this class:

C#
using EPiServer.Find;

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

    public GeoLocation Location { get; set; }
}

We can then request the number of restaurants located with 1, 5 and 10 kilometers from a given location with the GeoDistanceFacetFor method like this:

C#
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())
  .GetResult();

To extract the facet from the results variable we can again use a method named GeoDistanceFor. This will return an instance of the GeoDistanceFacet class which contains an instance of the GeoDistanceRangeResult per requested range.

C#
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: Jun 10, 2014

Recommended reading