赞
踩
①. 映射(Mapping)相当于数据表的表结构。ElasticSearch中的映射(Mapping)用来定义一个文档,可以定义所包含的字段以及字段的类型、分词器及属性等等
②. 映射可以分为动态映射和静态映射
类型 | 描述 |
---|---|
text | 当一个字段要是被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个个的词项。text类型的字段不用排序,很少用于聚合 |
keyword | keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到 |
类型 | 取值范围 |
---|---|
byte | -128 - 127 |
short | -32768 - 32767 |
integer | -2的31次方 – 2的31-1 |
long | -2的63次方 - 2的63次方-1 |
类型 | 取值范围 |
---|---|
doule | 64位双精度浮点类型 |
float | 32位单精度浮点类型 |
half_float | 16位半精度浮点类型 |
scaled_float | 缩放类型的浮点数 |
⑤. boolean类型:逻辑类型(布尔类型)可以接受true/false
⑥. binary类型
二进制字段是指base64来表示索引中储存的二进制数据,可用来储存二进制形式的数据,例如图像。默认情况下,该类型的字段只储存不索引。二进制只支持index_name属性
⑦. array类型
⑧. object类型:JSON天生具有层级关系,文档会包含嵌套的对象
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"
}
}
}
POST tax/_bulk
{"index":{}}
{"date": "2021-01-25 10:01:12","company": "中国烟草","ratal": 5700000}
{"index":{}}
{"date": "2021-01-25 10:01:13","company": "华为","ratal": 4034113.182}
{"index":{}}
{"date": "2021-01-26 10:02:11","company": "苹果","ratal": 7784.7252}
{"index":{}}
{"date": "2021-01-26 10:02:15","company": "小米","ratal": 185000}
{"index":{}}
{"date": "2021-01-26 10:01:23","company": "阿里","ratal": 1072526}
{"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}
"properties" : {
"date" : {
"type" : "text","fields" : {
"keyword" : {
"type" : "keyword","ignore_above" : 256
}
}
}
}
PUT test_index/_doc/1
{
"time":"2022-4-30T20:00:00Z"
}
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}
# 执行以上代码,以下为完整的执行结果:
{
"took" : 17,"errors" : true,"items" : [
{
"index" : {
"_index" : "tax","_type" : "_doc","_id" : "f4uyun8B1ovRQq6Sn9Qg","status" : 400,"error" : {
"type" : "mapper_parsing_exception","reason" : "Failed to parse field [date] of type [date] in document with id 'f4uyun8B1ovRQq6Sn9Qg'. Preview of field's value: '2021-01-30 10:02:07'","caused_by" : {
"type" : "illegal_argument_exception","reason" : "Failed to parse date field [2021-01-30 10:02:07] with format [strict_date_optional_time||epoch_millis]","caused_by" : {
"type" : "date_time_parse_exception","reason" : "date_time_parse_exception: Failed to parse with all enclosed parsers"
}
}
}
}
},{
"index" : {
"_index" : "tax","_id" : "gIuyun8B1ovRQq6Sn9Qg","_version" : 1,"result" : "created","_shards" : {
"total" : 2,"successful" : 2,"Failed" : 0
},"_seq_no" : 3,"_primary_term" : 1,"status" : 201
}
},"_id" : "gYuyun8B1ovRQq6Sn9Qg","reason" : "Failed to parse field [date] of type [date] in document with id 'gYuyun8B1ovRQq6Sn9Qg'. Preview of field's value: '2021-11-1T12:20:00Z'","reason" : "Failed to parse date field [2021-11-1T12:20:00Z] with format [strict_date_optional_time||epoch_millis]","_id" : "gouyun8B1ovRQq6Sn9Qg","_seq_no" : 4,"_id" : "g4uyun8B1ovRQq6Sn9Qg","_seq_no" : 5,"status" : 201
}
}
]
}
PUT test_index
{
"mappings": {
"properties": {
"time": {
"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
①. 在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"
}
{
"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
}
}
}
}
}
}
}
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,表明新增的字段不能被检索,只是一个冗余字段。
④. 不能更新映射:对于已经存在的字段映射,我们不能更新。更新必须创建新的索引,进行数据迁移
⑤. 查看所有索引映射关系
PUT /索引库名/_mapping
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
put /索引库名称
{
"settings": {
"索引库属性名": "索引库属性值"
},
"mappings": {
"properties": {
"字段名": {
"映射属性名": "映射属性值"
}
}
}
}
PUT /employee-index
{
"settings": {},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
6.0以后写法
POST reindex
{
"source":{
"index":"twitter"
},
"dest":{
"index":"new_twitters"
}
}
老版本写法
POST reindex
{
"source":{
"index":"twitter",
"type":"twitter"
},
"dest":{
"index":"new_twitters"
}
}
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"}
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"}
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" : [ ]
}
GET /newbank/_search
输出
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "newbank",
"_type" : "_doc", # 没有了类型
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
}
]
}
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
}
]
}
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
}
]
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict"></entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">http://192.168.56.10/es/fenci.txt</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
GET _analyze
{
"analyzer": "ik_smart",
"text":"唐智谷粒商城"
}
{
"tokens" : [
{
"token" : "唐智谷粒商城",
"start_offset" : 0,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 0
}
]
}
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95de12634192 elasticsearch:7.4.2 "/usr/local/bin/dock…" 4 seconds ago Up 3 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch
a197c1d2cf05 kibana:7.4.2 "/usr/local/bin/dumb…" 30 hours ago Up About a minute 0.0.0.0:5601->5601/tcp, :::5601->5601/tcp kibana
a18680bef63e redis "docker-entrypoint.s…" 5 weeks ago Up 2 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
91e02812975d mysql:5.7 "docker-entrypoint.s…" 5 weeks ago Up 2 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ~]# cd /mydata/
[root@localhost mydata]# ls
elasticsearch mysql redis
[root@localhost mydata]# mkdir nginx
[root@localhost mydata]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 08502081bff6 8 weeks ago 105MB
mysql 5.7 09361feeb475 2 months ago 447MB
kibana 7.4.2 230d3ded1abc 22 months ago 1.1GB
elasticsearch 7.4.2 b1179d41a7b4 22 months ago 855MB
[root@localhost mydata]# docker run -p80:80 --name nginx -d nginx:1.10
Unable to find image 'nginx:1.10' locally
1.10: Pulling from library/nginx
6d827a3ef358: Pull complete
1e3e18a64ea9: Pull complete
556c62bb43ac: Pull complete
Digest: sha256:6202beb06ea61f44179e02ca965e8e13b961d12640101fca213efbfd145d7575
Status: Downloaded newer image for nginx:1.10
24c1454acf9f8419f762f3369b59557df57cd6209864ef64000f2f26d9f0d05b
[root@localhost mydata]# mkdir -p /mydata/nginx/html
[root@localhost mydata]# mkdir -p /mydata/nginx/logs
[root@localhost mydata]# mkdir -p /mydata/nginx/conf
[root@localhost mydata]# ls
elasticsearch mysql nginx redis
[root@localhost mydata]# cd nginx/
[root@localhost nginx]# ls
conf html logs
[root@localhost nginx]# cd ..
[root@localhost mydata]# rm -rf nginx/
[root@localhost mydata]# docker container cp nginx:/etc/nginx .
[root@localhost mydata]# ls
elasticsearch mysql nginx redis
[root@localhost mydata]# docker stop nginx
nginx
[root@localhost mydata]# docker rm nginx
nginx
[root@localhost mydata]# ls
elasticsearch mysql nginx redis
[root@localhost mydata]# cd nginx
[root@localhost nginx]# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
[root@localhost nginx]# cd ..
[root@localhost mydata]# mv nginx conf
[root@localhost mydata]# ls
conf elasticsearch mysql redis
[root@localhost mydata]# mkdir nginx
[root@localhost mydata]# mv conf nginx/
[root@localhost mydata]# ls
elasticsearch mysql nginx redis
[root@localhost mydata]# cd nginx/
[root@localhost nginx]# ls
conf
[root@localhost nginx]# docker run -p 80:80 --name nginx \
> -v /mydata/nginx/html:/usr/share/nginx/html \
> -v /mydata/nginx/logs:/var/log/nginx \
> -v /mydata/nginx/conf/:/etc/nginx \
> -d nginx:1.10
01bfbb6a8cd0e3f6af476793ad33fdc696740eadb125f8adad573303524adb55
[root@localhost nginx]# ls
conf html logs
[root@localhost nginx]# docker update nginx --restart=always
nginx
[root@localhost nginx]# echo '<h2>hello nginx!</h2>' >index.html
[root@localhost nginx]# ls
conf html index.html logs
[root@localhost nginx]# rm -rf index.html
[root@localhost nginx]# cd html
[root@localhost html]# echo '<h2>hello nginx!</h2>' >index.html
[root@localhost html]#
[root@localhost html]# mkdir es
[root@localhost html]# cd es
[root@localhost es]# vi fenci.text
[root@localhost es]# ls
fenci.text
[root@localhost es]# mv fenci.text fenci.txt
[root@localhost es]# cd /mydata/
[root@localhost mydata]# cd elasticsearch/
[root@localhost elasticsearch]# ls
config data plugins
[root@localhost elasticsearch]# cd plugins/
[root@localhost plugins]# ls
ik
[root@localhost plugins]# cd ik/
[root@localhost ik]# ls
commons-codec-1.9.jar config httpclient-4.5.2.jar plugin-descriptor.properties
commons-logging-1.2.jar elasticsearch-analysis-ik-7.4.2.jar httpcore-4.4.4.jar plugin-security.policy
[root@localhost ik]# cd config/
[root@localhost config]# ls
extra_main.dic extra_single_word_full.dic extra_stopword.dic main.dic quantifier.dic suffix.dic
extra_single_word.dic extra_single_word_low_freq.dic IKAnalyzer.cfg.xml preposition.dic stopword.dic surname.dic
[root@localhost config]# vi IKAnalyzer.cfg.xml
[root@localhost config]# docker restart elasticsearch
elasticsearch
[root@localhost config]# cd /mydata/nginx/
[root@localhost nginx]# ls
conf html logs
[root@localhost nginx]# cd html/es/
[root@localhost es]# ls
fenci.txt
[root@localhost es]# cat fenci.txt
唐智谷粒商城
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。