Geo search
Describes how to find documents in relation to a website visitor's geographical location when building search features with Optimizely Search & Navigation.
Geo search is a search based on geographical coordinates and distances.
You map fields as a geo point for the server to recognize that a pair of numbers are coordinates. However, through conventions on the server side and in the .NET API, you do not need mapping if instances of the GeoLocation
class exposes geographical locations. So, to use geo search functionality, indexed objects should have a GeoLocation
type property or a custom mapping, such as an extension method that returns a GeoLocation
.
After Optimizely Search & Navigation indexes objects with locations, you can filter them using an expression that invokes the Within
and WithinDistanceFrom
extension methods.
Distance from location
Find documents within a certain distance from a location.
Assume the following class.
using EPiServer.Find;
public class Restaurant {
public string Name {
get;
set;
}
public GeoLocation Location {
get;
set;
}
}
And you create and index three instances of it with locations in Stockholm.
var closeToSergelsTorg = new Restaurant() {
Name = "City steakhouse",
Location = new GeoLocation(59.33265, 18.06468)
};
var atSlussen = new Restaurant() {
Name = "Vegetarian place",
Location = new GeoLocation(59.32067, 18.07294)
};
var atSkanstull = new Restaurant() {
Name = "Food and beer inn",
Location = new GeoLocation(59.30792, 18.07672)
};
client.Index(closeToSergelsTorg, atSlussen, atSkanstull);
The previous example shows three restaurants, one close to Sergels Torg in the center of Stockholm and two in another part of Stockholm, Sodermalm. You can find restaurants close to Sergels Torg by searching for restaurant types and filtering out documents whose Location
property is not within one kilometer of Sergels Torg.
var sergelsTorg = new GeoLocation(59.33234, 18.06291);
var result = client.Search<Restaurant>()
.Filter(x => x.Location.WithinDistanceFrom(sergelsTorg, 1.Kilometer()))
.GetResult();
This code produces a single result: the restaurant close to Sergels Torg.
You can also search for documents within a specific range of locations.
result = client.Search<Restaurant>()
.Filter(x => x.Location.WithinDistanceFrom(sergelsTorg,
1.Kilometer(),
2.Kilometers()))
.GetResult();
Note
The use of the two extension methods
Kilometer
andKilometers
above. TheWithinDistanceFrom
method requires distances as either miles or kilometers, which is represented by the abstractGeoDistance
type and its sub classesKilometer
andMiles
. The extension methods above offer a convenient way to create instances of theKilometer
class, which is a sub-class ofGeoDistance
. Similar extension methods exist for miles.
Locations in an area
Using the Within
method, you can search for documents within an area represented by at least three coordinates. For example, you have the following helper class with coordinates that describe Sodermalm in Stockholm (found using Google Maps).
using System.Collections.Generic;
using EPiServer.Find;
public class CoordinatesOf {
public static IEnumerable < GeoLocation > Sodermalm {
get {
yield
return new GeoLocation(59.32194, 18.07148);
yield
return new GeoLocation(59.32154, 18.07483);
yield
return new GeoLocation(59.32045, 18.07457);
yield
return new GeoLocation(59.31607, 18.10727);
yield
return new GeoLocation(59.30718, 18.09792);
yield
return new GeoLocation(59.30350, 18.07989);
yield
return new GeoLocation(59.30284, 18.07105);
yield
return new GeoLocation(59.30827, 18.04925);
yield
return new GeoLocation(59.31015, 18.03955);
yield
return new GeoLocation(59.31598, 18.02582);
yield
return new GeoLocation(59.31830, 18.02599);
yield
return new GeoLocation(59.31927, 18.02745);
yield
return new GeoLocation(59.31979, 18.03140);
yield
return new GeoLocation(59.32154, 18.05097);
}
}
}
You can use the Within
method to find restaurants in Sodermalm.
result = client.Search<Restaurant>()
.Filter(x => x.Location.Within(CoordinatesOf.Sodermalm))
.GetResult();
Sort by geographical distance
To order search hits for GeoLocation
fields by distance from a location, use the OrderBy
and OrderByDescending
methods. As an example, the following code orders search results by proximity to Sergels Torg in Stockholm.
var sergelsTorg = new GeoLocation(59.33234, 18.06291);
var result = client.Search<Restaurant>()
.OrderBy(x => x.Location).DistanceFrom(sergelsTorg)
.GetResult();
Matching documents with no location are ordered last when using the OrderBy
method, and first when using the OrderByDescending
method.
Aggregations and facets
Apart from filtering and sorting by geographical coordinates and distance, you can also count how many documents and locations are within certain distances from a specific location using the GeoDistanceFacetFor
method. See geographical distance facets.
Updated 8 months ago