Elasticsearch实战(六)-mapping映射

网友投稿 695 2022-05-29

1 mapping 作用

类似数据库中的表结构定义,主要作用如下:

定义Index下的字段名( Field Name )

定义字段的类型,比如数值型、字符串型、布尔型等

定义倒排索弓|相关的配置,比如是否索引、记录position等

2 查看 mapping

{ "index" : { "mappings" : { "properties" : { "JSON 数据" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "公众号" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } }

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

3 自定义 mapping

类似 MySQL,Mapping中的字段类型一旦设定后,禁止直接修改,原因如下:

Lucene实现的倒排索引生成后不允许修改

重新建立新的索引,然后做reindex操作

允许新增字段

通过dynamic参数来控制字段的新增

true (默认)允许自动新增字段

false不允许自动新增字段,但是文档可以正常写入,但无法对字段进行查询等操作

strict文档不能写入,报错

copy_to

将该字段的值复制到目标字段,实现类似 _all 的作用,不会出现在 _source 中,只用来搜索

index

控制当前字段是否索引,默认为true,即记录索引, false 不记录, 即不可搜索

index_options

控制倒排索弓引|记录的内容,有如下4种配置

docs只记录doc id

freqs记录doc id和term frequencies

positions 记录doc id、term frequencies和term position

offsets记录doc id、 term frequencies、 term position和character offsets

text 类型默认配置为positions,其他默认为docs。记录内容越多,占用空间越大。

null_value

当字段遇到null值时的处理策略,默认为null,即空值,此时es会忽略该值。可以通过设定该值设定字段的默认值。

4 数据类型

字符串

text、keyword

数值型

long、integer、short, byte, double, float half_float, scaled_float

布尔

boolean

日期

date

二进制

binary

范围

integer_range, float_range, long_range, double_range, date_ range

复杂数据类型

数组类型array、对象类型object、嵌套类型nested object

地理位置

geo_point、geo_shape

专用类型

记录ip地址 ip

实现自动补全 completion

记录分词数 token_count

记录字符串hash值 murmur3

percolator

join

多字段特性multi-fields

允许对同一个字段采用不同的配置,比如分词,常见例子如对人名实现拼音搜索,

只需要在人名中新增一个子字段为pinyin即可

ES可以自动识别文档字段类型,从而降低用户使用成本,如下所示

PUT /test_index/doc/1 { "username": "java", "age": "18" } GET /test_index/_mapping

1

2

3

4

5

6

7

{ "test_index" : { "mappings" : { "properties" : { "age" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "username" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } }

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

ES是依靠JSON文档的字段类型来实现自动识别字段类型,支持的类型如下:

PUT /test_index/doc/1 {"username":" alfred", "age":14, "birth":"1988-10-10", "married":false, "year":18, "tags":["boy","fashion"], "money":100.1 } GET /test_index/_mapping

1

2

3

4

5

6

7

8

9

10

11

{ "test_index" : { "mappings" : { "properties" : { "age" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "birth" : { "type" : "date" }, "married" : { "type" : "boolean" }, "money" : { "type" : "float" }, "tags" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "username" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "year" : { "type" : "long" } } } } }

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

37

38

39

40

41

42

43

44

45

46

47

48

日期的自动识别可以自行配置日期格式,

默认是[ “strict_date_optional_time”,“yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z”]

strict_date_optional_time 是 ISO datetime 的格式,完整格式类似下面:

YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)

dynamic_date_formats可以自定义日期类型

date_detection 可关闭日期自动识别的机制

dynamic template

允许根据es自动识别的数据类型、字段名等来动态设定字段类型,可以实现如下效果:

所有字符串类型都设定为keyword类型,即默认不分词

所有以message开头的字段都设定为text类型,即分词

所有以long_开头的字段都设定为long类型

所有自动匹配为double类型的都设定为float类型,以节省空间

匹配规则一般有如下几个参数:

match_ mapping _type 匹配 es 自动识别的字段类型,如boolean,long,string

match,unmatch 匹配字段名

path_ match,path_ unmatch 匹配路径

自定义Mapping的操作步骤

Elasticsearch实战(六)-mapping映射

写一条文档到es的临时索引中,获取es自动生成的mapping

修改步骤1得到的mapping ,自定义相关配置

使用步骤2的mapping创建实际所需索引

索引模板

索引模板,英文为Index Template,主要用于在新建索引时自动应用预先设定的配置,简化索索引创建的操作步骤

可以设定索引的配置和mapping

可以有多个模板,根据order设置,order大的覆盖小的配置

索弓|模板API , endpoint为 _template ,如下所示:

获取与删除

参考

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

Elasticsearch 实时流计算服务 CS

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Vue.js入门之工程目录介绍
下一篇:Elasticsearch Query DSL之Compound queries(复合查询)
相关文章