# GeoPoint vs GeoLocation and Search and Sorting

**URL:** <https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018>\
**Category:** Elasticsearch\
**Created:** [May 14, 2021, 12:54pm UTC](https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018 "2021-05-14T12:54:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirtash](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@Kirtash](https://discuss.elastic.co/u/Kirtash)\
**Post date:** [May 14, 2021, 12:54pm UTC](https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018/1 "2021-05-14T12:54:00Z")

</div>

Good morning,

Now I'm beginning to work with geoLocations and I'm using the library nest to interact to ElasticSearch, but my first question is:

- Whats is more correct? GeoLocation type or geoPoint:

Location

```auto
"location" : {
            "lat" : 2.0,
            "lon" : 34.2
          }

```

GeoPoint:

```auto
          "location" : {
            "type" : "point",
            "coordinates" : [
              2.8,
              41.9
            ]
          }

```

My intention is to do a query to find the candidates that are within a specific radius of a geopoint. I read the documentation but my query fails and I don't know the reason, can you help me please?

```auto
GET candidates/_search
{
  "from": 0,
  "size": 20, 
  "min_score": 0.1,
  "query": {
    "bool": {
      "must": [
        {
          "geo_distance": {
            "distance": "1km",
            "location": {
              "lat": 10,
              "lon": 10
            }
          }
        }
      ]
    }
  }
}

```

Thanks!!!

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 14, 2021, 8:38pm UTC](https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018/2 "2021-05-14T20:38:35Z")

</div>

There are only 4 geo field types in Elasticsearch see [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html#spatial_datatypes)

However there are a number of ways specify / load those `geo_point`s see [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html)

#### Spatial data types

[`geo_point`](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html)

Latitude and longitude points.

[`geo_shape`](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html)

Complex shapes, such as polygons.

[`point`](https://www.elastic.co/guide/en/elasticsearch/reference/current/point.html)

Arbitrary cartesian points.

[`shape`](https://www.elastic.co/guide/en/elasticsearch/reference/current/shape.html)

Arbitrary cartesian geometries.

In order to use, You will need to create a mapping with the correct field types in it in order to use `geo_point` or `geo_shape`

I also explained some geo searches [here](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/8)

---

<div class="post-metadata">

**Author:** ![Kirtash](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@Kirtash](https://discuss.elastic.co/u/Kirtash)\
**Post date:** [May 15, 2021, 5:17pm UTC](https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018/3 "2021-05-15T17:17:56Z")

</div>

First of all, thanks for the answer @stephenb!!

I think that the better option will be geo\_point 🙂

About the search, is not possible make a query with only one index that has a geo\_point, and specify another geopoint + distance and get these documents? Without create shape indexes. My intention is to create a form in which the user defines a location of google maps, and a distance in km.

With those values, I would like to get the documents that their geoposition is within the specified radious.

Thanks again!!

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 15, 2021, 6:07pm UTC](https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018/4 "2021-05-15T18:07:25Z")

</div>

You mean like this 😉

There are 2 types geo\_distance results returned by distance or geo\_shape these point are withing this shape circle

Take a look [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html) for the query types and [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/sort-search-results.html#geo-sorting) for sorting

Example

Put in a few points

```
DELETE my-index-000001
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "text": "location 1",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}

PUT my-index-000001/_doc/2
{
  "text": "location 2",
  "location": { 
    "lat": 42.52,
    "lon": -71.00
  }
}

PUT my-index-000001/_doc/3
{
  "text": "House in london",
  "location": { 
    "lat": 51.5,
    "lon": -0.127
  }
}

```

Here is a search using intersect (Points withing the circle  
NOTE : be carefull how you put in the coordinates it is lon, lat

```
# intersects not sorted
GET /my-index-000001/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": [
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "circle",
                "coordinates": [-71.3, 41.1],
                "radius": "500km"
              },
              "relation": "intersects"
            }
          }
        }
      ]
    }
  }
}

```

Results Not Sorted (you could by adding the sort clause below but I just wanted to show you both)

```
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "text" : "location 1",
          "location" : {
            "lat" : 41.12,
            "lon" : -71.34
          }
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "text" : "location 2",
          "location" : {
            "lat" : 42.52,
            "lon" : -71.0
          }
        }
      }
    ]
  }
}

```

Now a distance search and sorted.

```
# Distance plus sort. 
GET /my-index-000001/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "500km",
          "location": [-71.3, 42.5] <!-- NOTE this needs to be the same field name as your geo_point s
        }
      }
    }
  },
  "sort" : [
    {
      "_geo_distance" : {
          "location" : [-71.3, 42.5], <!-- NOTE this needs to be the same field name as your geo_point s
          "order" : "asc",
          "unit" : "km",
          "mode" : "min",
          "distance_type" : "arc",
          "ignore_unmapped": true
      }
    }
  ]
}

```

Results

```
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "text" : "location 2",
          "location" : {
            "lat" : 42.52,
            "lon" : -71.0
          }
        },
        "sort" : [
          24.69089016681285
        ]
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "text" : "location 1",
          "location" : {
            "lat" : 41.12,
            "lon" : -71.34
          }
        },
        "sort" : [
          153.48501594012578
        ]
      }
    ]
  }
}
```

---

<div class="post-metadata">

**Author:** ![Kirtash](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@Kirtash](https://discuss.elastic.co/u/Kirtash)\
**Post date:** [May 17, 2021, 7:41am UTC](https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018/5 "2021-05-17T07:41:28Z")

</div>

Thanks for the examples!! Now is working!! 😉 My error was the mapping.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 14, 2021, 7:41am UTC](https://discuss.elastic.co/t/geopoint-vs-geolocation-and-search-and-sorting/273018/6 "2021-06-14T07:41:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
