【Elasticsearch学习笔记二】es的Mapping字段映射、Mapping字段常用类型、Mapping映射的创建、查看和更新、es数据迁移、ik分词器

目录

1、Mapping字段映射概述

2、Mapping字段常用类型

3、映射中对时间类型详解

1)采取自动映射器来映射

2)手工映射提前指定日期类型

4、ES的keyword的属性ignore_above

5、Mapping映射的查看和创建

1)查看mapping信息:GET 索引名/_mapping

2)创建映射:PUT /索引名

3) 查看所有索引映射关系

4)修改索引映射关系

5)一次性创建索引和映射

6、数据迁移-reindex

7、ik_max_word、ik_smart分词器


1、Mapping字段映射概述

        映射(Mapping)相当于数据表的表结构。ElasticSearch中的映射(Mapping)用来定义一个文档,可以定义所包含的字段以及字段的类型、分词器及属性等等

        映射可以分为动态映射和静态映射:

  1. 动态映射(dynamic mapping):在关系数据库中,需要事先创建数据库,然后在该数据库实例下创建数据表,然后才能在该数据表中插入数据。而ElasticSearch中不需要事先定义映射(Mapping),文档写入ElasticSearch时,会根据文档字段自动识别类型。这种机制称之为动态映射
  2. 静态映射 :在ElasticSearch中也可以事先定义好映射,包含文档的各个字段及其类型等,这种方式称之为静态映射

2、Mapping字段常用类型

1)字符串(text、keyword)

  • text:可分词,不可参与聚合
  • keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合

 2)整数

 3)浮点类型

 4)date类型

  • 日期格式的字符串,比如"2018-01-13"或"2018-01-13 12:10:30"
  • long类型的毫秒数(从1970年开始)
  • integer的秒数

5) boolean类型

        逻辑类型(布尔类型)可以接受true/false

6) binary类型

        二进制字段是指base64来表示索引中储存的二进制数据,可用来储存二进制形式的数据,例如图像。默认情况下,该类型的字段只储存不索引。二进制只支持index_name属性

7)array类型

8)object类型

        JSON天生具有层级关系,文档会包含嵌套的对象

3、映射中对时间类型详解

        假如我们有如下索引tax,保存了一些公司的纳税或资产信息,单位为"万元"。当前这个例子里。索引达的含义并不重要,关键点在于字段的内容格式。我们看到date字段其中包含了多种日期的格式:“yyyy-MM-dd”,"yyyy-MM-dd"还有时间戳。

1)采取自动映射器来映射

        如果按照dynamic mapping,采取自动映射器来映射索引,我们自然而然的都会感觉字段应该是一个date类型;我们把数据存入索引然后查看tax索引的mapping;

POST tax/_bulk
{"index":{}}
{"date": "2021-01-27 10:01:54","company": "腾讯","ratal": 6500}
{"index":{}}
{"date": "2021-01-28 10:01:32","company": "蚂蚁金服","ratal": 5000}
{"index":{}}
{"date": "2021-01-29 10:01:21","company": "字节跳动","ratal": 10000}
{"index":{}}
{"date": "2021-01-30 10:02:07","company": "中国石油","ratal": 18302097}
{"index":{}}
{"date": "1648100904","company": "中国石化","ratal": 32654722}
{"index":{}}
{"date": "2021-11-1 12:20:00","company": "国家电网","ratal": 82950000}

查看tax索引的mapping:

"properties" : {
  "date" : {
    "type" : "text","fields" : {
      "keyword" : {
        "type" : "keyword","ignore_above" : 256
      }
    }
  }
}

        我们可以看到date居然是一个text类型。这是为什么呢,原因就在于对时间类型的格式的要求是绝对严格的。要求必须是一个标准的UTC时间类型。上述字段的数据格式如果想要使用,就必须使用yyyy-MM-ddTHH:mm:ssZ格式(其中T个间隔符,Z代表 0 时区);

        错误的时间格式均无法被自动映射器识别为日期时间类型,例如:

  1. yyyy-MM-dd HH:mm:ss
  2. yyyy-MM-dd
  3. 时间戳
  4. 2022-4-30T20:00:00Z

2)手工映射提前指定日期类型

PUT tax
{
 "mappings": {
   "properties": {
     "date": {
       "type": "date"
     }
   }
 }
}
POST tax/_bulk
{"index":{}}
{"date": "2021-01-30 10:02:07","ratal": 32654722}
{"index":{}}
{"date": "2021-11-1T12:20:00Z","ratal": 82950000}
{"index":{}}
{"date": "2021-01-30T10:02:07Z","ratal": 18302097}
{"index":{}}
{"date": "2021-01-25","ratal": 5700000}

第一个(写入失败):2021-01-30 10:02:07

第二个(写入成功):1648100904

第三个(写入失败):2021-11-1T12:20:00Z

第四个(写入成功):2021-01-30T10:02:07Z

第五个(写入成功):2021-01-25

总结:

1.对于yyyy-MM-dd HH:mm:ss或2021-11-1T12:20:00Z,ES 的自动映射器完全无法识别,即便是事先声明日期类型,数据强行写入也会失败

2.对于时间戳和yyyy-MM-dd这样的时间格式,ES 自动映射器无法识别,但是如果事先说明了日期类型是可以正常写入的

3.对于标准的日期时间类型是可以正常自动识别为日期类型,并且也可以通过手工映射来实现声明字段类型

实际开发中的解决方法:

//只需要在字段属性中添加一个参数:
“format”: “yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”,这样就可以避免因为数据格式不统一而导致数据无法写入的窘境
PUT test_index
{
 "mappings": {
   "properties": {
     "time": {
       "type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
     }
   }
 }
}

4、ES的keyword的属性ignore_above

        在es的5.x版本,keyword类型字段可以设置ignore_above,表示最大的字段值长度,超出这个长度的字段将不会被索引,但是会存储;举个例子:设置message 的长度最长为20,超过20的不被索引,这里的不被索引是这个字段不被索引,但是其他字段有的话仍然被索引到;

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "message": {
          "type": "keyword",
          "ignore_above": 20 
        }
      }
    }
  }
}
# 下面造点数据
PUT my_index/my_type/3 
{
  "message": "123456789"
}

PUT my_index/my_type/5
{
  "message": "123456789012345678901"
}

全部查询:

 模糊匹配:

在这里插入图片描述

        我们可以发现你用模糊匹配是搜索不到的(注意上面的数据最后带个1是21位下图是20位的),同样 用精确匹配前面20个仍然搜索不到;

5、Mapping映射的查看和创建

1)查看mapping信息:GET 索引名/_mapping

 {
    "bank" : {
      "mappings" : {
        "properties" : {
          "account_number" : {
            "type" : "long" # long类型
          },
          "address" : {
            "type" : "text", # 文本类型,会进行全文检索,进行分词
            "fields" : {
              "keyword" : { # addrss.keyword
                "type" : "keyword",  # 该字段必须全部匹配到
                "ignore_above" : 256
              }
            }
          },
          "age" : {
            "type" : "long"
          },
          "balance" : {
            "type" : "long"
          },
          "city" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "email" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "employer" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "firstname" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "gender" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "lastname" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "state" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }

ElasticSearch7-去掉type概念:

1、关系型数据库中两个数据表示是独立的,即使他们里面有相同名称的列也不影响使用,但ES中不是这样的。elasticsearch是基于Lucene开发的搜索引擎,而ES中不同type下名称相同的filed最终在Lucene中的处理方式是一样的

  • (1)两个不同type下的两个user_name,在ES同一个索引下其实被认为是同一个filed,你必须在两个不同的type中定义相同的filed映射。否则,不同type中的相同字段名称就会在处理中出现冲突的情况,导致Lucene处理效率下降。
  • (2)去掉type就是为了提高ES处理数据的效率。

2、Elasticsearch 7.x URL中的type参数为可选。比如,索引一个文档不再要求提供文档类型
3、Elasticsearch 8.x 不再支持URL中的type参数

解决:将索引从多类型迁移到单类型,每种类型文档一个独立索引;将已存在的索引下的类型数据,全部迁移到指定位置即可;

2)创建映射:PUT /索引名

创建Mapping映射语法:

PUT /索引库名/_mapping
{
  "properties": {
    "字段名": {
      "type": "类型",  # type:类型,可以是text、long、short、date、integer、object等
      "index": true,  # index:是否索引,默认为true
      "store": false, # store:是否存储,默认为false
      "analyzer": "分词器" # analyzer:指定分词器
    }
  }
}
PUT /company-index/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "index": true,
      "analyzer": "ik_max_word"
    },
    "job": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "logo": {
      "type": "keyword",
      "index": false
    },
    "payment": {
      "type": "float"
    }
  }
}

1、index影响字段的索引情况:

  • true:字段会被索引,则可以用来进行搜索。默认值就是true
  • false:字段不会被索引,不能用来搜索

        index的默认值就是true,也就是说你不进行任何配置,所有字段都会被索引。但是有些字段是我们不希望被索引的,比如企业的logo图片地址,就需要手动设置index为false;

2、store:是否将数据进行独立存储:

        原始的文本会存储在_source里面,默认情况下其他提取出来的字段都不是独立存储的,是从_source里面提取出来的。当然你也可以独立的存储某个字段,只要设置store:true即可,获取独立存储的字段要比从_source中解析快得多,但是也会占用更多的空间,所以要根据实际业务需求来设置,默认为false;

3、analyzer:指定分词器:

        一般我们处理中文会选择ik分词器 ik_max_word、ik_smart

PUT /my_index
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer"
      },
      "email": {
        "type": "keyword" # 指定为keyword
      },
      "name": {
        "type": "text" # 全文检索。保存时候分词,检索时候进行分词匹配
      }
    }
  }
}
输出:
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "my_index"
}
查看映射GET /my_index
输出结果:
{
  "my_index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"
        },
        "email" : {
          "type" : "keyword"
        },
        "employee-id" : {
          "type" : "keyword",
          "index" : false
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1588410780774",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "ua0lXhtkQCOmn7Kh3iUu0w",
        "version" : {
          "created" : "7060299"
        },
        "provided_name" : "my_index"
      }
    }
  }
}
# 添加新的字段映射PUT /my_index/_mapping
PUT /my_index/_mapping
{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false # 字段不能被检索。检索
    }
  }
}
这里的 "index": false,表明新增的字段不能被检索,只是一个冗余字段。

注意:不能更新映射:对于已经存在的字段映射,我们不能更新。更新必须创建新的索引,进行数据迁移

3) 查看所有索引映射关系

  1. GET _mapping
  2. GET _all/_mapping

4)修改索引映射关系

PUT /索引库名/_mapping
{
  "properties": {
    "字段名": {
      "type": "类型",
      "index": true,
      "store": true,
      "analyzer": "分词器"
    }
  }
}

注意:这里修改映射是增加字段,做其它更改只能删除索引重新建立映射

5)一次性创建索引和映射

put /索引库名称
{
  "settings": {
    "索引库属性名": "索引库属性值"
  },
  "mappings": {
    "properties": {
      "字段名": {
        "映射属性名": "映射属性值"
      }
    }
  }
}

6、数据迁移-reindex

        先创建new_twitter的正确映射,然后使用如下方式进行数据迁移

6.0以后写法
POST reindex
{
  "source":{
      "index":"twitter"
   },
  "dest":{
      "index":"new_twitters"
   }
}


老版本写法
POST reindex
{
  "source":{
      "index":"twitter",
      "type":"twitter"
   },
  "dest":{
      "index":"new_twitters"
   }
}

案例:原来类型为account,新版本没有类型了,所以我们把他去掉;想要将年龄修改为integer,先创建新的索引;

1.原索引:

GET /bank/_search
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",//原来类型为account,新版本没有类型了,所以我们把他去掉
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "amberduke@pyrami.com",
          "city" : "Brogan",
          "state" : "IL"
        }
      },
      ...
GET /bank/_search
查出
"age":{"type":"long"}

2.创建新的索引:

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": "text"
      },
      "gender": {
        "type": "keyword"
      },
      "lastname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "state": {
        "type": "keyword"
      }
    }
  }
}
查看"newbank"的映射:
GET /newbank/_mapping
能够看到age的映射类型被修改为了integer.
"age":{"type":"integer"}

3.将bank中的数据迁移到newbank中:

POST _reindex
{
  "source": {
    "index": "bank",
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}
运行输出:
#! Deprecation: [types removal] Specifying types in reindex requests is deprecated.
{
  "took" : 768,
  "timed_out" : false,
  "total" : 1000,
  "updated" : 0,
  "created" : 1000,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

4.查看newbank中的数据:

GET /newbank/_search

输出
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "newbank",
        "_type" : "_doc", # 没有了类型

7、ik_max_word、ik_smart分词器

        一个tokenizer(分词器)接收一个字符流,将之分割为独立的tokens(词元,通常是独立的单词),然后输出tokens流;例如:whitespace tokenizer遇到空白字符时分割文本。它会将文本"Quick brown fox!"分割为(Quick,brown,fox!);

        该tokenizer(分词器)还负责记录各个terms(词条)的顺序或position位置(用于phrase短语和word proximity词近邻查询),以及term(词条)所代表的原始word(单词)的start(起始)和end(结束)的character offsets(字符串偏移量)(用于高亮显示搜索的内容)。elasticsearch提供了很多内置的分词器(标准分词器),可以用来构建custom analyzers(自定义分词器)。

        分词器的安装:根据当前es的版本,去找到对应版本的分词器,下载.zip文件,然后解压到elasticsearch/plugins文件夹下即可,注意需要将权限进行修改chmod -R 777 plugins/ik;安装完毕后,需要重启elasticsearch容器。

POST _analyze
{
  "analyzer": "standard",
  "text": "The 2 Brown-Foxes bone."
}
执行结果:
{
  "tokens" : [
    {
      "token" : "the",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "2",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<NUM>",
      "position" : 1
    },
    {
      "token" : "brown",
      "start_offset" : 6,
      "end_offset" : 11,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "foxes",
      "start_offset" : 12,
      "end_offset" : 17,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "bone",
      "start_offset" : 18,
      "end_offset" : 22,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

        ik_max_word:会将文本做最细粒度的拆分,比如会将"湖南省岳阳县"拆分为"湖南省、湖南、省、 岳阳县、岳阳、县等词语(索引的时候用ik_max_word):

GET _analyze
{
   "analyzer": "ik_max_word", 
   "text":"湖南省岳阳县"
}
{
  "tokens" : [
    {
      "token" : "湖南省",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "湖南",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "省",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "岳阳县",
      "start_offset" : 3,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "岳阳",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "县",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "CN_CHAR",
      "position" : 5
    }
  ]
}

        ik_smart:会做最粗粒度的拆分,比如会将"湖南省岳阳县"拆分为湖南省、岳阳县(前台搜索的时候用 ik_smart):

GET _analyze
{
   "analyzer": "ik_smart", 
   "text":"湖南省岳阳县"
}
 {
 "tokens" : [
   {
     "token" : "湖南省",
     "start_offset" : 0,
     "end_offset" : 3,
     "type" : "CN_WORD",
     "position" : 0
   },
   {
     "token" : "岳阳县",
     "start_offset" : 3,
     "end_offset" : 6,
     "type" : "CN_WORD",
     "position" : 1
   }
 ]
}

        用户还可以通过修改elasticsearch/plugins/ik/config中的IKAnalyzer.cfg.xml文件自定义分词器;

参考原文地址:http://t.csdn.cn/WK4mn

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等到鸡吃完米

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值