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

相關文章
相關標籤/搜索