1

I have simple repository to save my status log

class OrderStatusRepository
  include Elasticsearch::Persistence::Repository
  include Elasticsearch::Persistence::Repository::DSL

  def index_name
    "statuses-#{ Time.now.strftime('%Y-%m')}"
  end

  mapping do
    indexes :src_location, type: 'geo_point'
    indexes :dst_location, type: 'geo_point'
  end
end

The issue is mapping is not applied, when i add some data.

 {"id":158,"src_location":"1.486912, 2.493157","dst_location":"11.489026, -22.501309"}

"dst_location": {
  "type": "text", #NOT GEOPOINT !!!!
  "fields": {
    "keyword": {
    "type": "keyword",
    "ignore_above": 256
  }
}

I can create the index and mappings manually, but it has dynamic name and i'm not going to do it every month/day.

Is there any way to automate this? Thanks.

1 Answer 1

2

You can use index templates that allow you to define templates that will automatically be applied on index creation time. The templates can include both settings and mappings.

For your data, you can create an index template like this which will match any indexes matching foo-* and bar-*

PUT/ _template/foobar

{
  "index_patterns": [
    "foo-*",
    "bar-*"
  ],
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "src_location": {
        "type": "geo_point"
      },
      "dst_location": {
        "type": "geo_point"
      }
    }
  }
}

Now create an index that will match the templates definition and add data to it

POST/ foo-1/_doc/1

{
  "id": 158,
  "src_location": "1.486912, 2.493157",
  "dst_location": "11.489026, -22.501309"
}

When you retrieve the mapping for the index. you will get this

GET /foo-1/_mapping

{
  "foo-1": {
    "mappings": {
      "properties": {
        "dst_location": {
          "type": "geo_point"
        },
        "id": {
          "type": "keyword"
        },
        "src_location": {
          "type": "geo_point"
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.