關閉ES動態創建mapping


使用ES的默認配置會使我們在索引不存在於mapping中的字段時,會自動創建。

這無疑會給我們帶來困擾。

在我們不想要某個字段被搜索的時候,我們可以在開始關閉動態創建mapping。

執行如下操作:

PUT /test_xzy/_mapping/data
{
  "dynamic":false
}

然后查看mapping:

GET /test_xzy/_mappings/data

得到:

{
  "test_xzy": {
    "mappings": {
      "data": {
        "dynamic": "false",
        "properties": {
          "age": {
            "type": "long"
          },
          "hate": {
            "type": "string"
          },
          "like": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

這樣一來,在不存在於mapping中的字段在被索引之后不會存在於mapping中,這樣的字段也不能被用來搜索。

我們更嚴格一點可以設置不在mapping中的字段的數據不能寫入ES:

PUT /test_xzy/_mapping/data
{
  "dynamic":"strict"
}

這樣我們之后寫入非”name,age,like,hate“的其他字段數據時會得到如下錯誤:

mapping set to strict, dynamic introduction of [hatae] within [data] is not allowed

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM