穀粒商城學習——P119-121映射

映射定義文檔如何被存儲和檢索的

@映射字段類型

text類型⽤於全⽂索引,搜索時會自動使用分詞器進⾏分詞再匹配
keyword 不分詞,搜索時需要匹配完整的值

創建索引並指定映射

PUT /my_index
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer" #整數存入如不指定類型會默認爲long
      },
      "email": {
        "type": "keyword" #keyword檢索時會精確匹配匹配
      },
      "name": {
        "type": "text" #檢索時候進行分詞匹配
      }
    }
  }
}

創建帶檢索

 

 

 

添加新的字段映射

PUT /my_index/_mapping
{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false # 字段不能被檢索。默認所有字段的index都是true的,只相當於一個冗餘存儲信息
    }
  }
}

 

更新映射

對於已經存在的字段映射,我們不能更新。更新必須創建新的索引,進行數據遷移。

數據遷移

先創建bank的正確映射newbank

put newbank
{
  "mappings": {
    "properties" : {
        "account_number" : {
          "type" : "long"
        },
        "address" : {
          "type" : "text"
        },
        "age" : {
          "type" : "integer"
        },
        "balance" : {
          "type" : "long"
        },
        "city" : {
          "type" : "keyword"
        },
        "email" : {
          "type" : "keyword"
        },
        "employer" : {
          "type" : "keyword"
        },
        "firstname" : {
          "type" : "keyword"
        },
        "gender" : {
          "type" : "keyword"
        },
        "lastname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "state" : {
          "type" : "keyword",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
  }
}
View Code

再將bank中的數據遷移到newbank中

POST _reindex
{
  "source": {
    "index": "bank"
    ,"type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}

source指定老數據索引,dest指定新索引

,"type": "account"之所以加了刪除線,是因爲新版本不需要指定這個類型,6.0之前的老版本需要指定(我試了一下不加也是可以的)

數據遷移及查詢

 

 遷移後所有的_type都是_doc了。感覺遷移叫複製比較好,因爲老bank數據還存在

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章