Elasticsearch索引mapping的写入、查看与修改

mapping的写入与查看

首先建立一个索引:html

curl -XPOST "http://127.0.0.1:9200/productindex"
{"acknowledged":true}

如今只建立了一个索引,并无设置mapping,查看一下索引mapping的内容:json

curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty" 
{
  "productindex" : {
    "mappings" : { }
  }
}

能够看到mapping为空,咱们只建立了一个索引,并无进行mapping配置,mapping天然为空。 
下面给productindex这个索引加一个type,type name为product,并设置mapping:bash

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d ' 
{
    "product": {
            "properties": {
                "title": {
                    "type": "string",
                    "store": "yes"
                },
                "description": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "price": {
                    "type": "double"
                },
                "onSale": {
                    "type": "boolean"
                },
                "type": {
                    "type": "integer"
                },
                "createDate": {
                    "type": "date"
                }
            }
        }
  }
'

{
  "acknowledged" : true
}

上面的操做中,咱们给productindex加了一个type,并写入了product的mapping信息,再次查看:app

curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty"
{
  "productindex" : {
    "mappings" : {
      "product" : {
        "properties" : {
          "createDate" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "description" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "onSale" : {
            "type" : "boolean"
          },
          "price" : {
            "type" : "double"
          },
          "title" : {
            "type" : "string",
            "store" : true
          },
          "type" : {
            "type" : "integer"
          }
        }
      }
    }
  }
}

修改mapping

若是想给product新增一个字段,那么须要修改mapping,尝试一下:curl

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
     "product": {
                "properties": {
                     "amount":{
                        "type":"integer"
                   }
                }
            }
    }'
{
  "acknowledged" : true
}

新增成功。 
若是要修改一个字段的类型呢,好比onSale字段的类型为boolean,如今想要修改成string类型,尝试一下:url

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
     "product": {
                "properties": {
                 "onSale":{
                    "type":"string" 
               }
            }
        }
}'

返回错误:spa

{
  "error" : {
    "root_cause" : [ {
      "type" : "illegal_argument_exception",
      "reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"
    } ],
    "type" : "illegal_argument_exception",
    "reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"
  },
  "status" : 400
}

为何不能修改一个字段的type?缘由是一个字段的类型修改之后,那么该字段的全部数据都须要从新索引。Elasticsearch底层使用的是lucene库,字段类型修改之后索引和搜索要涉及分词方式等操做,不容许修改类型在我看来是符合lucene机制的。 
这里有一篇关于修改mapping字段的博客,叙述的比较清楚:Elasticsearch 的坑爹事——记录一次mapping field修改过程,能够参考.code

相关文章
相关标签/搜索