HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In


Geo search is search based on geographical coordinates and distances.

For the server to recognize that a pair of numbers are coordinates, the fields need to be mapped as a geo point. 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 either a `GeoLocation` type property or a custom mapping, such as an extension method that returns a `GeoLocation`.

After objects with locations are indexed, you can filter on them using an expression that invokes the `Within` and `WithinDistanceFrom` extension methods.

## Distance from location

Here we describe how to 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 type 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.



Note

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 `OrderByDescending` method.

## Aggregations/facets

Apart from filtering and sorting by geographical coordinates/distance, you can also count how many documents/locations are within certain distances from a specific location using the `GeoDistanceFacetFor` method. See [geographical distance facets](🔗).