Geo search is a search based on geographical coordinates and distances.
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, mapping is not needed as long as geographical locations are exposed as instances of the `
GeoLocation` class. 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 `
After objects with locations are indexed, 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.
And you create and index three instances of it with locations in Stockholm.
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.
This produces a single result: the restaurant close to Sergels torg.
You can also search for documents within a specific range of a location.
The use of the two extension methods `
Kilometer` and `
Kilometers` above. The `
WithinDistanceFrom` method requires distances as either miles or kilometers, which is represented by the abstract `
GeoDistance` type and its sub classes `
Kilometer` and `
Miles`. The extension methods above offer a convenient way to create instances of the `
Kilometer` class, which is a sub-class of `
GeoDistance`. 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).
You can use the `
Within` method to find restaurants in Sodermalm.
## 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.
Matching documents with no location are ordered _last_ when using the `
OrderBy` method, and _first_ when using the `
## 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](🔗).