知识内容输出不易,请尊重他人劳动成果。严禁随意传播、复制和盗用他人成果或文章内容用以商业或盈利目的!
1、Mapping 简介
1.1 mapping 是啥?
ES 中的 mapping 有点类似与关系数据库中表结构
的概念,在 MySQL 中,表结构里包含了字段名称,字段的类型还有索引信息等。在 Mapping 里也包含了一些属性,比如字段名称、类型、字段使用的分词器、是否评分、是否创建索引等属性,并且在 ES 中一个字段可以有对个类型。
1.2 如何查看索引映射
查看完整的索引 mapping
GET /<index_name>/_mappings
查看索引中指定字段的 mapping
GET /<index_name>/_mappings/field/<field_name>
2、自动映射:dynamic mapping
自动映射也叫动态映射,是 ES 在索引文档写入发生时自动创建 mapping 的一种机制。
ES 在创建索引之前,并不强制要求创建索引的 mapping,ES 会根据字段的值来推断字段类型,进而自动创建并指定索引类型。
2.1 自动类型推断规则
下面是自动映射器推断字段类型的规则
自动映射器会尽可能的把字段映射为宽字段类型。
2.2 mapping 的使用禁忌
- ES 没有隐式类型转换
- ES 不支持类型修改
- 生产环境尽可能的避免使用 dynamic mapping
2.3 自动映射器的意义是什么?
官方的解释是为了照顾入门学习者,但实际上我更建议初学者尽量避免使用自动映射器,而尽可能多的显式声明 mapping,因为显式创建 mapping 是生产环境必须的,所以这是必须掌握的技能。
反而是老手,在非生产环境中,使用自动映射会比较方便。
3、手动映射:Explicit mapping
手动映射也叫做显式映射,即:在索引文档写入之前,认为的创建索引并且指定索引中每个字段类型、分词器等参数。
3.1 ********** mapping
PUT /<index_name>
{
"mappings": {
"properties": {
"field_a": {
"<parameter_name>": "<parameter_value>"
},
...
}
}
}
3.2 **** mapping ****
********** mapping ************。
PUT <index_name>/_mapping
{
"properties": {
"<field_name>": {
"type": "text", // ********************,**************
"analyzer":"ik_max_word", // **********************,**************
"fielddata": false
}
}
}
****:**************************。
- ****************
- ******************
4、ES ******** ★:field data type
4.1 ****
**********************************。**************:****************************************。
- **************:text、match_only_text **。
- **************:keyword、**********。
********************************,**********************************
******。
4.2 ES **************
****
- **********************************
- **** ★ ********************,******************
4.2.1 ************ ★
- Numbers:********,**************************
- binary:****** Base64 ****************。
- boolean:**********,**** true ** false。
- alias:********。
- Keywords:**** keyword ★、constant_keyword ** wildcard。
- Dates:********,**** data ★ ** data_nanos,********
4.2.2 ************(********)
- object:******************,****** json ****** object ****。
- flattened:**************,****** json ****。
- nested ★:********。
- join:**************。
4.2.3 **********
- Range:********,**** long_range,double_range,data_range **
- ip:ipv4 ** ipv6 ****
- version:******
- murmur3:******************
4.2.4 ************
- aggregate_metric_double:
- histogram:
4.2.5 ************
- text ★:************,************。
- annotated-text:
- completion ★**:**
- search_as_you_type:
- token_count:
4.2.6 ************
- dense_vector:********************。
- rank_feature:********************************。
- rank_features:********************************。
4.2.7 ************ ★
- geo_point:************。
- geo_shape:**********,**********。
- point:************。
- shape:**************。
4.2.8 ********
- percolator:**Query DSL **************。
5、********
5.1 **************
****** ES **********************
******** | **** |
---|---|
analyzer ★ | **********,**** text ************。 |
coerce | ********************,************************************。 true: “1” => 1 false: “1” =< 1 ********** ![]() ![]() |
copy_to | **************************************,******************************** |
doc_values ★ | **********************,****true,**********************************,**************************,**********doc****************(****** text ** annotated_text)********** ES 8.x:****** - ************ **************** |
dynamic ★ | **************************,****************:true:(****)************false:**********。************************,************_source****************。************************,******************。runtime:********************************,****************,**_source**************。strict:****************,**********************。****************************。 |
eager_global_ordinals | ****************,************。 ****: ![]() |
enabled | ******************,**************,****************,**************,****************_source************,********,**************。![]() |
fielddata ★ | ******************,********************、**********************,**********fielddata********,************************** |
fields ★ | ** field **********,************(************************) |
format ★ | ****************,**:![]() |
ignore_above ★ | **************** |
ignore_malformed | ************ |
index_options | **************************************************。******text **** |
index_phrases | ****exact_value********,********************** |
index_prefixes | ********:min_chars:************,>0,****2(****)max_chars:************,<20,****5(****) |
index ★ | ********************************,**** true,**************,**************************,************ source ************true ****************************。(****)false **********************。********************,**************,************_source**************。************************,******************。strict ****************,**********************。**************************** |
meta | ************ |
normalizer | ************ |
norms ★ | ************(**filter********************)。 |
null_value ★ | ** null ************ |
position_increment_gap | ******************************,slop **** 100 **:****** 1 |
properties ★ | ****mapping********object********** |
search_analyzer ★ | ********************** |
similarity | ********************,****:BM25boolean****:classic(TF-IDF)** ES 8.x ************! |
subobjects | ES 8 ****,subobjects ****** false **********,**************************。 |
store | ****************** |
term_vector | ********,******************。 |
****** 1
# slop **** 100
PUT position_increment_gap_index
{
"mappings" : {
"properties" : {
"title" : {
"type" : "text", // slop **** 100
"fields" : {
"slop_0" : {
"type": "text",
"position_increment_gap" : 0
},
"slop_10" : {
"type": "text",
"position_increment_gap" :10
}
}
}
}
}
}
# ********
POST /position_increment_gap_index/_doc/1
{
"title" : ["hello word", "Elastic Go"]
}
# **********
GET position_increment_gap_index/_search
{
"query": {
"match_phrase": {
"title": {
"query": "word hello"
}
}
}
}
# slop **** 100 ******
GET position_increment_gap_index/_search
{
"query": {
"match_phrase": {
"title": {
"query": "word elastic",
"slop": 100
}
}
}
}
# slop_0 **********
GET position_increment_gap_index/_search
{
"query": {
"match_phrase": {
"title.slop_0": {
"query": "word elastic"
}
}
}
}
# slop_10 ******** slop:10************
GET position_increment_gap_index/_search
{
"query": {
"match_phrase": {
"title.slop_10": {
"query": "word elastic",
"slop": 10
}
}
}
}
6、Text **** ★★
**************************,************、****************,**************** text ****。**** text ********,****************,******************,********************************。text ********************,************。(****************** text ************:**********,****************** text ******。**********************,******************************。****,****************************,************************。**********************************)
********,text **************,******** ES ********************************。
****:
************,******** text **********************,************,******************************,**********************************。
****:
- **************:********。
- Text ************。
- Text ************。
- ES ************** Text ****************。
****:
************,******** text **********************,************,******************************,**********************************。
7、keyword **** ★
keyword**********************************************。 *****************************************************************。*************************************************,********************************************。
**************************,**** ID、************、******、********、**************。************。** term ************************。
- keyword ******************。
- keyword ****************************
- keyword ****************************
********,********“******”******,******“********”******,******** term
********,************。
GET
8、Date **** ★
**********************************************************。** Java **********,Elasticsearch ************************************ mapping。
Elasticsearch ************************************************,**********************************,****** Elasticsearch ******************,**************************************************。****************** 50,********************** long ****,****** int 。****************************************,****************************************。
** Elasticsearch **,************************************,**************************************************
8.1 ****
****************** tax ,******************************,******“****”。****************************。********************,**************。********************。************************。********date****************************:“yyyy-MM-dd”,“yyyy-MM-dd”**********。******** dynamic mapping,************************。************************************date****。
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}
******************** tax ****** mapping,************ date ********** text ****。
8.2 ****
********************************************。******************** UTC ********。******************************,**********yyyy-MM-ddTHH:mm:ssZ
****(****T********,Z**** 0 ****),**********************(************************************):
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd
- ******
****:**********************************,****************************************************,********************************。
8.3 ************
******************,****************************,**********************?
PUT tax
{
"mappings": {
"properties": {
"date": {
"type": "date"
}
}
}
}
POST tax/_bulk
{"index":{}}
{"date": "2021-01-30 10:02:07", "company": "********", "ratal": 18302097}
{"index":{}}
{"date": "1648100904", "company": "********", "ratal": 32654722}
{"index":{}}
{"date": "2021-11-1T12:20:00Z", "company": "********", "ratal": 82950000}
{"index":{}}
{"date": "2021-01-30T10:02:07Z", "company": "********", "ratal": 18302097}
{"index":{}}
{"date": "2021-01-25", "company": "********", "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",
"_type" : "_doc",
"_id" : "gIuyun8B1ovRQq6Sn9Qg",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "tax",
"_type" : "_doc",
"_id" : "gYuyun8B1ovRQq6Sn9Qg",
"status" : 400,
"error" : {
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [date] of type [date] in document with id 'gYuyun8B1ovRQq6Sn9Qg'. Preview of field's value: '2021-11-1T12:20:00Z'",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "failed to parse date field [2021-11-1T12:20:00Z] 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",
"_type" : "_doc",
"_id" : "gouyun8B1ovRQq6Sn9Qg",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "tax",
"_type" : "_doc",
"_id" : "g4uyun8B1ovRQq6Sn9Qg",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1,
"status" : 201
}
}
]
}
********:
- ******(********):2021-01-30 10:02:07
- ******(********):1648100904
- ******(********):2021-11-1T12:20:00Z
- ******(********):2021-01-30T10:02:07Z
- ******(********):2021-01-25
8.4 ****
- ****
yyyy-MM-dd HH:mm:ss
**2021-11-1T12:20:00Z
,ES ************************,**********************,********************。 - ************
yyyy-MM-dd
**************,ES ******************,******************************************。 - **************************************************,****************************************。
8.5 ********
********************。******************************:“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"
}
}
}
}
9、Nested **** ★
****************,************ Nested **************,**************************。
****:ES ** Nested ****************
10、************:Dynamic Templates
********************,**************************。**************************************************,**************** Dynamic template。
10.1 ************
"dynamic_templates": [
{
"my_template_name": {
... match conditions ...
"mapping": { ... }
}
},
...
]
10.2 ********:conditions
10.2.1 match_mapping_type
****************
****
PUT <index>
{
"mappings": {
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
PUT <index>/_doc/1
{
"my_integer": 500,
"my_string": "elastic.org.cn"
}
**********,************ dynamic template
- ****************** long ********************** integer ****。
- ****************** string ****************** keyword ****。
10.2.2 match、unmatch
********************,**********、**********。
****************** Java ****,****
"match_pattern": "regex",
"match": "^profit_\d+$"
****
PUT <index>
{
"mappings": {
"dynamic_templates": [
{
"longs_as_strings": {
"match_mapping_type": "string",
"match": "num_*",
"unmatch": "*_text",
"mapping": {
"type": "long"
}
}
}
]
}
}
PUT <index>/_doc/1
{
"num_integer": 5
"num_long": "5",
"num_text": "foo"
}
**********,conditions ********** 3 ******,******************************long
****:
- ********************
- **************
num_
**** - ******************
_text
****
10.2.3 path_match、path_unmatch ☆
************
****
PUT <index>
{
"mappings": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
}
]
}
}
PUT <index>/_doc/1
{
"name": {
"first": "elastic",
"middle": "org",
"last": "cn"
}
}
GET <index>/_search
{
"query": {
"match": {
"full_name": "elastic"
}
}
}
10.3 ******** ☆
****
PUT <index>
{
"mappings": {
"dynamic_templates": [
{
"named_analyzers": {
"match_mapping_type": "string",
"match": "*",
"mapping": {
"type": "text",
"analyzer": "{name}"
}
}
},
{
"no_doc_values": {
"match_mapping_type":"*",
"mapping": {
"type": "{dynamic_type}",
"doc_values": false
}
}
}
]
}
}
PUT <index>/_doc/1
{
"english": "elastic.org.cn",
"count": 5
}
GET <index>/_mapping
**********,**************,******************
- ********************,******************************************。
- ********************,******** doc_values。