javascriptelasticsearchkibanaelasticsearch-5kibana-5

Elastic search 5.3 - failed to find geo_point field


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?


Solution

  • 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.