Elasticsearch mapping

本篇次要介绍一下 Elasticsearch mapping 的概念, 它是什么 以及如何自定义它, 并且再介绍一下 dynamic 的机制

如果把 Elasticsearch中的mapping 比照到 mysql 中 就是 mysql中的 表的 scheme , 就是表的定义构造,

失常在 关系型数据库mysql中 须要先把表的scheme 定义好 再插入数据, 并且 你无奈插入未在scheme 中定义的字段 , 而 es 中 能够在你未给索引定义 mapping 的时候 主动帮你创立 mapping , 并且你也能够通过 dynamic 来管制是否容许灵便动静的 增加 mapping中的属性

1.什么是Mapping

  • Mapping 相似 mysql 中的 schema 的定义,就是定义索引属性字段的

    • 定义索引中字段的名称
    • 定义索引中字段的数据类型 , 如 text , long , keyword....
    • 定义索引中字段的的倒排索引相干配置 ( Analyzer...)
  • 一个Mapping 属于一个索引的Type

    • 每个文档都属于一个Type
    • 一个Type有一个Mapping 定义
    • es7.0开始, 在Mapping中不须要指定 Type信息, 因为7.0之后只有_doc Type

2.es 主动创立mapping

当咱们去创立一个 索引的时候 未指定 mapping , es会默认帮这个索引创立一个 mapping

创立一个 索引并且索引一条数据

PUT blog/_doc/1{  "name": "es mapping",  "type": "es",  "desc": "es mapping desc",  "author": "johnny",  "word_count": 50,  "create_time": "2022-10-31"}
GET blog/_mapping  # 查看一个 es主动生成的 mapping

从下面能够看进去 属性都被es 主动创立了 对应的mapping , 包含每个属性的type类型等, 那它为什么会这样转化的,什么时候是 text, 什么时候是 long 上面就来介绍 es 中mapping 的 类型自动识别

3. mapping 类型自动识别

JSON类型Elasticsearch 类型
字符串1.匹配日期格局 会设置成Date
2.匹配数字 设置成 float 或者 long ,该选项默认敞开的
3.设置成Text , 并且增加 keyword 子字段
整数long
浮点数float
布尔值boolean
对象object
数组由第一个非空数值的类型所定义 .如 ["jack","johnny"] 则类型为 Text
空值疏忽 ???
我试验的版本里7.18 , 如果设置null 会被主动定义为Text , 具体不太分明

4. 自定义创立mapping

除了下面的介绍的 es 主动创立 mapping 外, 还能够自定义 索引的mapping , 更加灵便和合乎业务需要等等.

留神以前的版本须要在 mappings 上面还有一层 type , 如 mappings: { "_doc" : { "properties" : {xxx} }} 然而7.0之后 type就不须要了

PUT blog_info{  "mappings": {      "properties": {        "blog_name": {          "type": "keyword"        },        "blog_desc": {          "type": "text"        },        "blog_word_count": {          "type": "long"        },        "create_time": {          "type": "date"        }      }    }}

其中text和keyword类型,text类型的字段在新增或批改文档时会主动分词, 而keyword 不会,它会保留插入的原始文本

索引一条数据

PUT blog_info/_doc/1{  "blog_name": "es mapping",  "blog_desc": "es mapping desc",  "blog_word_count": 12,  "blog_auther": "johnny",  "create_time": "2022-10-31"}

5. mapping 属性设置analyzer 分词器

默认分词器 standard , 它会把中文一个个拆开,必定是不适宜的,如果是索引中文的信息, 须要设置字段的分词器,

PUT blog_info{  "mappings": {      "properties": {        "blog_desc": {          "type": "text",          "analyzer": "ik_smart" //设置这个字段的分词器         }      }    }}

大部分分词器是须要以es 中插件的形式 装置的 ,后续会出一篇专门的 analyzer 分词器

6. mapping 属性设置 boost 权重

在es搜寻的时候 会有一个相关性算分的过程 , 如果不设置 每个字段的默认boost 权重为1.0 , 如果心愿加大 依照广告投放金额的分 那么能够设置boost 以进步搜寻 天然就排在后面了

PUT blog_info{  "mappings": {      "properties": {        "put_amount": {          "type": "text",          "boost": "5"         }      }    }}

7. mapping 属性设置 copy_to

该属性容许多个字段 copy 到指定的字段, 能够进行搜寻这个字段,然而_source 中是不显示的

PUT peope{  "mappings": {    "properties": {      "first_name": {        "type": "text",        "copy_to": "full_name"      },      "last_name":{        "type": "text",        "copy_to": "full_name" // copy_to 指定字段      },      "full_name":{        "type": "text"      }    }  }}
GET peope/_search?q=full_name:johnny  //应用 full_name 去搜寻//能够看到 并没有 full_name 的返回 然而能够通过它去搜寻"_source" : {   "first_name" : "johnny",   "last_name" : "qiang"}

8. mapping 属性设置 index

通过给 属性设置 index 来管制该 字段是否 参加 索引, 默认 true , 如果index 设置为false 那么 不能记录索引 并且不能够搜寻

PUT peope{  "mappings": {    "properties": {      "first_name": {        "type": "text",        "index": false //设置 index false      },      "last_name":{        "type": "text"      }    }  }}POST peope/_doc{  "first_name": "johnny is good name",  "last_name": "qiang"}

留神 url-search 搜不到然而不报错, 而 requestbody 查问 index false 的字段 会报错

GET peope/_search?q=first_name:johnny //搜寻不到数据 因为 //"hits" : [ ] GET peope/_search?q=last_name:johnny // 能够看到因为 last_name 默认index 了 所以能够搜寻到////    "hits" : [//      {//        "_index" : "peope",//        "_type" : "_doc",//        "_id" : "vobiMYQB4x9Wk60f2F21",//        "_score" : 0.2876821,//        "_source" : {//          "first_name" : "johnny is good name",//          "last_name" : "johnny is good name"//        }//      }//    ]GET peope/_search{  "query": {    "match": {      "first_name": "johnny"    }  }}// 抛错400 Cannot search on field [first_name] since it is not indexed.

9. mapping 设置 属性 null_value 默认值

null_value:当字段遇到null值时候的解决策略(字段为null时候是不能被搜寻的,也就是说,text类型的字段不能应用该属性,能够应用在keyword 字段上),设置该值后能够用你设置的值替换null值,这点可类比mysql中的"default"设置默认值, 然而也有点不一样, 后续就能够应用你设置的这个 null_value 去搜寻, 然而检索进去的数据_source 中 还是展现 null

PUT peope{  "mappings": {    "properties": {      "first_name": {        "type": "keyword",        "null_value": "default" // 设置当 文档的first_name 字段为null时候 转成default 去创立倒排索引      },      "last_name":{        "type": "text"      }    }  }}POST peope/_doc{  "first_name": null, //设置null值  "last_name": "johnny is good name",  "full_name": "johnny is good name"}GET peope/_search?q=first_name:default //依据 null_value 设置的值去搜寻,查问进去还是原来的null// {//        "_index" : "peope",//        "_type" : "_doc",//        "_id" : "xob-MYQB4x9Wk60fVF1_",//        "_score" : 0.2876821,//        "_source" : {//          "first_name" : null,  //          "last_name" : "johnny is good name",//          "full_name" : "johnny is good name"//        }// }

10. mapping 设置 dynamic

dynamic 是否容许动静新增字段

  • true : 容许动静新增字段 同时mapping 被更新 文档可被索引
  • false: 不容许动静新增字段 , mapping 不会被更新, 字段不能被索引, 然而数据能够入库并且信息会呈现在 _source 中
  • strict : 不容许写入, 间接报错

对于曾经存在的字段 一旦又数据写入,就不能进行批改字段定义了,因为 底层Lucene不容许批改, 如果心愿批改字段类型,必须 reindex 重建索引

10.1 dynamic false
PUT peope{  "mappings": {    "dynamic": false, // 设置在索引上的 而不是对应的字段上的     "properties": {      "first_name": {        "type": "text"      },      "last_name":{        "type": "text"      }    }  }}POST peope/_doc //dynamic false 能够入库文档数据{  "first_name": "johnny is good name",  "last_name": "johnny is good name",  "full_name": "johnny is good name"}GET peope/_search?q=full_name:johnny //尝试通过 新增的字段去搜寻// "hits" : [ ]GET peope/_search?q=first_name:johnny // 能够搜到数据, 并且_source 中能够看到新增的字段////    "hits" : [//      {//        "_index" : "peope",//        "_type" : "_doc",//        "_id" : "vobiMYQB4x9Wk60f2F21",//        "_score" : 0.2876821,//        "_source" : {//          "first_name" : "johnny is good name",//          "last_name" : "johnny is good name",//          "full_name" : "johnny is good name" //        }//      }//    ]
10.2 dynamic strict

strict : 严格模式 , 不容许 动静新增字段的

PUT peope{  "mappings": {    "dynamic": "strict",    "properties": {      "first_name": {        "type": "text"      },      "last_name":{        "type": "text"      }    }  }}POST peope/_doc //间接抛错{  "first_name": "johnny is good name",  "last_name": "johnny is good name",  "full_name": "johnny is good name"}// 400 mapping set to strict, dynamic introduction of [full_name] within [_doc] is not allowed

总结

本篇十分具体介绍了 Elasticsearch中 mapping , 介绍了mapping它是什么, 主动创立mapping的机制 , 自定义mapping 中各种参数设置. 一起来学习坚固吧.

欢送大家拜访 集体博客 Johnny小屋
欢送关注集体公众号