Using Solr spatial search
This topic is valid for Sitecore 9.0 and later.
You can use some of the Solr spatial search features from Sitecore. You use spatial search to:
-
Search for an index point (using the format: Latitude, Longitude)
-
Filter search results based on a circle or bounding box
-
Sort or boost scoring by distance between points
Sitecore supports the SpatialRecursivePrefixTreeFieldType type (RPT for short) from Solr for indexing and searching. There is a Coordinate Sitecore template, in /sitecore/templates/System/Geospatial/ in the master database, for storing index points. The Coordinate template has two fields – Latitude and Longitude. When you create an item from this template, the coordinates are indexed in Solr.
Use
Use
This example code shows how you use a Sitecore LINQ query to search for a list of points that are within a 10-kilometer radius of the Kuala Lumpur city center, which has the coordinates 3.156230 and 101.713614:
The WithinRadius API
The WithinRadius API
This API has three parameters:
-
A property that contains the coordinates of the point. You must mark this property
[IndexField("coordinate")]and return theCoordinateclass.For example:
/// <summary>Gets or sets the coordinate of POI.</summary>[IndexField("coordinate")][DataMember]public Coordinate Location { get; set; } -
The coordinates of the center point to use when searching for other points within a certain distance.
-
The radius used to search for any point that is within a certain distance from the specified center point coordinates.
There is a fourth, optional parameter. You use it to specify whether Solr searches for points within a bounding box instead of within a circle.
You can use the Sitecore LINQ query order to return points that are ordered in either ascending (the default) or descending distance:
You can combine the WithinRadius API with the OrderByDistance API or the OrderByDistanceDescending API to retrieve a list of points that lie within the specified radius. The results are ordered by ascending or descending proximity to the center point, as in this example:
WithinRadius.aspx.cs: