我有简单的存储库来保存我的状态日志
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
问题是,当我添加一些数据时,映射未应用。
{"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
}
}
我可以手动创建索引和映射,但是它具有动态名称,我不会每个月/每天这样做。
有什么办法可以自动化吗?谢谢。
你可以使用允许你定义将在索引创建时自动应用的模板的索引模板。模板可以包括设置和映射。
对于你的数据,你可以创建一个像这样的索引模板,该模板将匹配所有匹配foo-*
和bar-*
PUT/ _template/foobar
{
"index_patterns": [
"foo-*",
"bar-*"
],
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"src_location": {
"type": "geo_point"
},
"dst_location": {
"type": "geo_point"
}
}
}
}
现在创建一个与模板定义匹配的索引,并向其中添加数据
POST/ foo-1/_doc/1
{
"id": 158,
"src_location": "1.486912, 2.493157",
"dst_location": "11.489026, -22.501309"
}
当你检索索引的映射时。你会得到这个
GET /foo-1/_mapping
{
"foo-1": {
"mappings": {
"properties": {
"dst_location": {
"type": "geo_point"
},
"id": {
"type": "keyword"
},
"src_location": {
"type": "geo_point"
}
}
}
}
}