1. 程式人生 > >Elasticsearch索引的建立、檢視及修改

Elasticsearch索引的建立、檢視及修改

mapping的寫入與檢視

   使用elasticsearch儲存資料之前建立索引非常關鍵,一個好的索引使後續業務的查詢更加方便快捷,我們建立索引時如果不指定相關資訊,會按照預設設定建立,如果我們想要更加強大的功能,比如中文檢索、拼音檢索、首拼檢索,就需要我們自己規劃索引的建立,一般索引建立後不能更改,所以建立索引時要特別注意。下面是建立索引的最基礎的步驟,供新手們參考。

以下POST命令如果執行不成功可以換成PUT嘗試,本人在kibana軟體中親測有效。

1、首先建立一個索引:

curl -XPOST "http://127.0.0.1:9200/productindex"

命令執行正常則返回:{"acknowledged":true}    

現在只建立了一個索引,並沒有設定mapping,檢視一下索引mapping的內容: curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty"  {   "productindex" : {     "mappings" : { }   } }

2、給索引新增屬性

可以看到mapping為空,我們只建立了一個索引,並沒有進行mapping配置,mapping自然為空。  下面給productindex這個索引加一個type,type name為product,並設定mapping: curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '  {     "product": {             "properties": {                 "title": {                     "type": "string",                     "store": "yes"                 },                 "description": {                     "type": "string",                     "analyzer": "standard"                 },                 "price": {                     "type": "double"                 },                 "onSale": {                     "type": "boolean"                 },                 "type": {                     "type": "integer"                 },                 "createDate": {                     "type": "date"                 }             }         }   }

命令執行正常則返回:{"acknowledged" : true}

3、檢視mapping結果

上面的操作中,我們給productindex加了一個type,並寫入了product的mapping資訊,再次檢視: curl -XGET "http://127.0.0.1:9200/productindex/_mapping" {   "productindex" : {     "mappings" : {       "product" : {         "properties" : {           "createDate" : {             "type" : "date",             "format" : "strict_date_optional_time||epoch_millis"           },           "description" : {             "type" : "string",             "analyzer": "standard"           },           "onSale" : {             "type" : "boolean"           },           "price" : {             "type" : "double"           },           "title" : {             "type" : "string",             "store" : true           },           "type" : {             "type" : "integer"           }         }       }     }   } }

4、修改mapping(新增一個新欄位)

如果想給product新增一個欄位,那麼需要修改mapping,嘗試一下: curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping

{      "product": {                 "properties": {                      "amount":{                         "type":"integer"                    }                 }             }     }' {

5、修改mapping(修改原有欄位)  

如果要修改一個欄位的型別呢,比如onSale欄位的型別為boolean,現在想要修改為string型別,嘗試一下:  curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping

{      "product": {                 "properties": {                  "onSale":{                     "type":"string"                 }             }         } } 返回錯誤: {   "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機制的。