0

I have a mapping like below:

{
"my_locations": {
  "aliases": {

  },
  "mappings": {
    "_doc": {
      "properties": {
          "location": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
        },

I know that if the field type of location is "geo_point" then I can use following geo distance query.

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

I read that I cannot change the field type for location from text to geo_point(from elastic search documentation and stackoverflow) and I already have many data. So how can I find the location that are within the certain range from my input location?

3
  • You need to reindex your data in order to change the type of your location field to geo_point Commented Aug 7, 2018 at 7:01
  • @Val but reindexing require to remove all my data? Commented Aug 7, 2018 at 7:02
  • 1
    No, you simply need to create another index with the right data type and then reindex will copy your current index into the new one. Then you'll be able to run your query on the new index. elastic.co/guide/en/elasticsearch/reference/current/… Commented Aug 7, 2018 at 7:04

1 Answer 1

1

First, you need to create a new index with the correct data type

PUT my_locations_2
{
  "mappings": {
    "_doc": {
      "properties": {
          "location": {
            "type": "geo_point"
          }
      }
   }
}

Then you can use the reindex API in order to copy the data from the old index to the new one:

POST _reindex
{
  "source": {
    "index": "my_locations"
  },
  "dest": {
    "index": "my_locations_2"
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I will try it many thankx. I could not understand the documentation. So your suggested helped me understand some basic
I have some issue. When I store the location using text I have location": "40.124,-70.254" like this but with geo_point I have to store "location" : { "lat" : 40, "lon" : -70 }
you don't need to change your data, you can also store it as text as you can see here: elastic.co/guide/en/elasticsearch/reference/current/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.