Elasticsearch映射

如何改变弹性search现有索引中的映射? 我需要设置geo_pointtypes的位置types。

{ "stations": { "mappings": { "station": { "properties": { "address": { "type": "string" }, "district": { "type": "string" }, "location": { "properties": { "lat": { "type": "double" }, "lon": { "type": "double" } } }, "name": { "type": "string" } } } } } } 

这篇文章解释了这个问题的几种方法。 types不能在现有字段上明确更改。

要应用新的映射,您需要重新索引数据。如果添加新字段没有问题,则不需要重新索引数据。

删除现有的索引

 curl -XDELETE "http://localhost:9200/index/type/_mapping" 

添加地理点types

 curl -XPUT "http://localhost:9200/index/type/_mapping" -d' { "stations": { "mappings": { "station": { "properties": { "address": { "type": "string" }, "district": { "type": "string" }, "location": { "type": "geo_point" }, "name": { "type": "string" } } } } } }' 

使用上面的curl命令创build新的映射到索引。 更多信息请参考链接

一种select是使用模板索引和所需的映射,并更新映射( https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html

假设你的索引名称就像“myindex”。 您可以运行以下命令来创build映射:

 curl -XPUT "http://localhost:9200/_template/myindex_template_1" -d' { "template": "myindex*", "mappings" : { "stations": { "mappings": { "station": { "properties": { "address": { "type": "string" }, "district": { "type": "string" }, "location": { "type": "geo_point" }, "name": { "type": "string" } } } } } } }' 

这使您可以使用新映射创build以“myindex”前缀开头的新索引。 下一步是使用诸如Logstash之类的工具从现有索引读取到新索引。

像例子一样创build一个configuration文件(例如reindex.conf)(用你的设置进行更新):

 input { elasticsearch { hosts => ["localhost"] # Update with your server hostname here scan => true scroll => "5m" index => "myindex" #update with your existing index name docinfo => true } } output { stdout { codec => rubydebug } elasticsearch { host => "localhost" protocol => "http" index => "myindex2" # notice I created a new index name document_type => "%{[@metadata][_type]}" document_id => "%{[@metadata][_id]}" } } 

之后,您可以删除旧索引并为新索引创build一个别名,以便应用程序仍然可以使用旧索引名称引用它。 例:

 curl -X DELETE localhost:9200/myindex curl -X POST localhost:9200/_aliases -d ' { "actions" : [ { "add" : { "index" : "myindex2", "alias" : "myindex" } } ] }' 

例如,如果你创build了一个myindex3,你可以这样做:

 curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions" : [ { "remove" : { "index" : "myindex2", "alias" : "myindex" } }, { "add" : { "index" : "myindex3", "alias" : "myindex" } } ] }' 

如果您要更改现有映射中字段的types。 我build议你发送ignore_conflicts参数。

curl -X PUT“ http:// localhost:9200 / mapped-index / article / _mapping?ignore_conflicts = true ”-d“{”article“:{”properties“:{”body“:{”type“:”geo_point “}}}}”