Elasticsearch mapping

Elasticsearch mapping

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

image-20221101151025669

如果把 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

image-20221101132257644

从上面可以看出来 属性都被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小屋
欢迎关注个人公众号

欢迎关注个人公众号

posted @ 2022-11-03 23:42  AskaJohnny  阅读(320)  评论(0编辑  收藏  举报