相关阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.4/mapping-params.html 
1. 显式定义 Mapping ElasticSearch 本身会对加入的索引自定创建对应的 mapping,即 Dynamic Mapping,当然我们也可以显式的定义 mapping,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // 对 users 索引显式定义 mapping PUT users {     "mappings"  : {       "properties"  : {         "firstName"  : {           "type"  : "text"          },         "lastName"  : {           "type"  : "text"          },         "mobile" : { // 定义 mobile 字段类型为 text,且不可被检索           "type" : "text",           "index": false         }       }     } } 
2. 常见参数介绍 2.1 index index 控制当前字段是否被索引。默认为 true。如果设置成 false,该字段不可被检索。示例:
1 2 3 4 5 6 7 8 9 10 // 检索 users 的 mobile POST users/_search {   "query" : {     "match" : {       "mobile" : "12345678"      }   } } // 会报错误 Cannot search on field [mobile] since it is not indexed. 
2.2 null_value 假设现在需要支持检索出 mobile 字段为 null 的记录,我们则需要使用到 null_value 使该字段支持按 null 去检索。需要说明的是只有 keyword 类型的字段才支持 null_value。示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 // 显示设置索引 PUT users {     "mappings"  : {       "properties"  : {         "firstName"  : {           "type"  : "text"          },         "lastName"  : {           "type"  : "text"          },         "mobile"  : {           "type"  : "keyword" ,           "null_value" : "NULL"          }       }     } } // 存入 mobile 为空的文档 PUT users/_doc/1 {   "firstName" :"Ruan" ,   "lastName" : "Yiming" ,   "mobile" : null  } // 检索 mobile 为空的文档 GET users/_search {   "query" : {     "match" : {       "mobile" : "NULL"      }   } } 
需要说明的是如下这种文档不属于 mobile 值为空的范畴,而是不存在 mobile 字段,所以无法通过上述方式检索出来:
1 2 3 4 5 PUT users/_doc/2 {   "firstName" :"Ruan1" ,   "lastName" : "Yiming1"  } 
2.3 copy_to copy_to 支持将字段的数值拷贝到目标字段,从而实现类似 _all 的作用。另外,copy_to 的目标字段不出现在 _source 中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 PUT users {   "mappings" : {     "properties" : {       "firstName" :{         "type" : "text" ,         "copy_to" : "fullName"        },       "lastName" :{         "type" : "text" ,         "copy_to" : "fullName"        }     }   } } PUT users/_doc/1 {   "firstName" :"Ruan" ,   "lastName" : "Yiming"  } // 支持按 fullName 字段检索 POST users/_search {   "query" : {     "match" : {        "fullName" :{          "query" : "Ruan Yiming" ,         "operator" : "and"        }     }   } }