2

I am referring this Geo Distance Query example given on elastic search documentation.

Version : 5.3

As per tutorial i performed this query to insert data in index

PUT /my_locations/location/1
{
    "pin" : {
        "location" : {
            "lat" : 40.12,
            "lon" : -71.34
        }
    }
}

Then I'm simply trying to apply Geo distance query which is also shown in documentation.

GET /my_locations/location/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}

But it is showing me this error. "failed to find geo_point field [pin.location]" Other time it showed me this error "field [pin.location] is not a geo_point field".

Do i need to insert this record in a different way or am i missing some arguments in query?

parth
  • 624
  • 1
  • 10
  • 29
  • can you post your index mapping? You have to specified the geo_point type on your filed in the mapping ( => https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html ) – Pierre Mallet Feb 28 '18 at 12:55
  • I looked into index pattern and it is showing me Data type for location is Number – parth Feb 28 '18 at 12:58
  • {"gym":{"mappings":{"gyms":{"properties":{"location":{"type":"float"},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"pin":{"properties":{"location":{"properties":{"lat":{"type":"float"},"lon":{"type":"float"}}}}}}}}}} – parth Feb 28 '18 at 13:01

1 Answers1

2

Thank you @Pierre Mallet

I looked around with documentation and come to conclusion that i first need to create index with geo_point data type. I cannot apply data type on existed index. So my steps are like this

PUT gym
{
  "mappings": {
    "gyms": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

Insert record which contains location

PUT gym/gyms/1
{
  "location" : [ -71.34, 41.12 ]
}

Then look for index and it's data type

GET /gym/gyms/_mapping/

You will see that location field has geo_point data type. Then you can perform your query.

GET gym/gyms/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
              "geo_distance" : {
                    "distance" : "150km",
                    "location" : [ -70.34, 40.12 ]
                }
            }
        }
    }
}

It is working perfectly now.

parth
  • 624
  • 1
  • 10
  • 29