GeoPoint vs GeoLocation and Search and Sorting

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

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

GeoPoint:

          "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?

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

Thanks!!!

There are only 4 geo field types in Elasticsearch see here

However there are a number of ways specify / load those geo_points see here

Spatial data types

geo_point

Latitude and longitude points.

geo_shape

Complex shapes, such as polygons.

point

Arbitrary cartesian points.

shape

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

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

I think that the better option will be geo_point :slight_smile:

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!!

You mean like this :wink:

There are 2 types geo_distance results returned by distance or geo_shape these point are withing this shape circle

Take a look here for the query types and here 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
        ]
      }
    ]
  }
}

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

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