今天有小夥伴問,Elasticsearch 索引裡面的mapping 設定 dynamic = true是什麼意思?
一:ES mapping dynamic是做什麼用的
- Mapping中的字段類型一旦設定後,禁止直接修改。原因是:Lucene實作的反向索引生成後不允許修改
- 隻能建立一個索引,然後reindex資料
- 預設允許新增字段
通過dynamic參數來控制字段的新增:
- true:預設,允許自動新增字段,但是mapping不顯示,查詢傳回JSON有
- false:不允許自動新增字段,但是文檔可以正常寫入,但無法對新增字段進行查詢等操作
- strict:文檔不能寫入,報錯
二:執行個體示範
1:限制政策 true
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": true, //開啟 —— 遇到陌生字段時, 進行動态映射
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
}
put my_index/_doc/1
{
"name": "John Smith",
"first_name": "John",
"last_name": "Smith"
}
2:限制政策 false
delete my_index
{}
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": false, // 關閉 —— 忽略遇到的陌生字段
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
}
3:限制政策 strict
delete my_index
{}
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": "strict", // 嚴格控制政策
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
}
put my_index/_doc/1
{
"name": "John Smith",
"first_name": "John",
"last_name": "Smith"
}
小夥伴你清楚了嗎?
以上案例均使用:Elasticsearch 6.5.3 版本示範。
轉自:https://blog.csdn.net/yexiaomodemo/article/details/108884663