简体   繁体   English

Elasticsearch映射

[英]Elasticsearch mapping

How to change mapping in existing index of elasticsearch? 如何更改现有弹性搜索索引中的映射? I need set type for location on geo_point type. 我需要在geo_point类型上设置类型的位置。

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

This article explains several approaches to this problem. 本文介绍了解决此问题的几种方法。 Types can not be explicitly changed on an existing field. 无法在现有字段上显式更改类型。

To apply new mapping you need to reindex the data.If you add new field its no problem , no need to reindex data. 要应用新映射,您需要重新索引数据。如果添加新字段没有问题,则无需重新索引数据。

To delete existing index 删除现有索引

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

To add geo point type 添加地理点类型

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"
                    }
                }
            }
        }
    }
}'

use above curl command to create new mapping to index. 使用上面的curl命令创建索引的新映射。 for more info refer link 有关更多信息,请参阅链接

One option is to use a template index with the mapping that you want and update the mapping ( https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html ) 一种选择是使用模板索引和您想要的映射并更新映射( https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html

Supposing your index name is something like "myindex". 假设您的索引名称类似于“myindex”。 You can create a mapping by running: 您可以通过运行来创建映射:

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"
                    }
                 }
            }
        }
    }
  }
}'

This allows you to create new indices that start with the prefix "myindex" with the new mapping. 这允许您使用新映射创建以前缀“myindex”开头的新索引。 The next step is read from the existing index to the new index using a tool such as Logstash. 下一步是使用Logstash等工具从现有索引读取到新索引。

Create a config file (eg reindex.conf) like the example (update with your settings): 像示例一样创建配置文件(例如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]}"
       }
}

After that, you can delete the old index and create an alias to the new index so that your application can still refer to it using the old index name. 之后,您可以删除旧索引并为新索引创建别名,以便您的应用程序仍然可以使用旧索引名称引用它。 Example: 例:

curl -X DELETE localhost:9200/myindex

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

If you ever create a myindex3 for example, you can just do: 例如,如果你创建了myindex3,你可以这样做:

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

If you're changing the type of a field in the existing mapping. 如果要更改现有映射中字段的类型。 I would suggest you to send the ignore_conflicts parameter. 我建议你发送ignore_conflicts参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM