Elasticsearch 映射Mappings (三)


前言

本文主要记录映射Mappings的作用、类型、ES常用数据类型、常见参数、重要的数据类型、映射配置、映射模板。

静态参数使不可修改的,所以mappings里的配置需要谨慎行事


一、Mapping简介

mapping类似Mysql中的表结构,在Maping里包含了一些属性,比如字段名称、类型、字段使用的分词器、是否评分、是否创建索引等属性,并且在ES中一份字段可以有多个类型。

查看索引映射

所有(默认创建keyword映射)

GET <索引名>/_mapping
{
    
    
  "text_create": {
    
    
    "mappings": {
    
    
      "properties": {
    
    
        "name": {
    
    
          "type": "text",
          "fields": {
    
    
            "keyword": {
    
    
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

查看指定

GET <索引名>/_mapping/field/<字段名称>
{
    
    
  "text_create": {
    
    
    "mappings": {
    
    
      "name": {
    
    
        "full_name": "name",
        "mapping": {
    
    
          "name": {
    
    
            "type": "text",
            "fields": {
    
    
              "keyword": {
    
    
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

二、自动映射 dynamic mapping

在索引文档写入时发生自动创建mapping的机制。在写入文档时会自动为该文档创建mapping映射字段。在创建索引时不要求创建mapping映射,ES会根据字段值来推断字段类型,进而创建并指定索引类型。

自动类型推断

自动类型推断的规则为:

field type dynamic runtime
true/false boolean boolean
小数 float double
数字 long long
object object -
数组 取决于数组中的第一个非空元素类型 取决于数组中的第一个非空元素类型
日期格式字符串 date date
数字类型字符串 float/long double/long
其它字符串 text + keyword keyword

自动映射器会尽可能的把字段映射为宽字段类型

PUT text_create/_doc/1
{
    
    
  "price": 18.12,
  "display": true,
  "object":{
    
    
    "1":1,
    "2":2
  },
  "arey" :[
    "3","4"
    ],
    "time": "2011-11-12"
}

mapping对应映射

扫描二维码关注公众号,回复: 16424645 查看本文章
{
    
    
  "text_create": {
    
    
    "mappings": {
    
    
      "properties": {
    
    
        "age": {
    
    
          "type": "long"
        },
        "arey": {
    
    
          "type": "text",
          "fields": {
    
    
            "keyword": {
    
    
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "display": {
    
    
          "type": "boolean"
        },
        "name": {
    
    
          "type": "text",
          "fields": {
    
    
            "keyword": {
    
    
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "object": {
    
    
          "properties": {
    
    
            "1": {
    
    
              "type": "long"
            },
            "2": {
    
    
              "type": "long"
            }
          }
        },
        "price": {
    
    
          "type": "float"
        },
        "time": {
    
    
          "type": "text",
          "fields": {
    
    
            "keyword": {
    
    
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

mapping注意点

  • ES 没有隐式转换
  • ES 不支持类型修改
  • 生产环境尽可能避免使用 dynamic mapping

三、手动映射 Expllicit mapping

也称为显示映射,在索引文档写入前。创建索引并且指定索引中每个字段类型、分词器等参数。

mapping索引创建完成后,部分mapping的属性时不允许更改的

创建索引

PUT text_mapping
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "text_field" : {
    
    
        "type": "text",
        "fields": {
    
    
          "text_filed_keyword":{
    
    
          "type": "keyword"
        }
          
        }
      },
      "long_field": {
    
    
        "type": "long"
      }
    }
  }
}


四、自动映射模板 Dynamic Templates

在定义字段映射的时候,往往字段不一定有具体的名称,有时候希望对一类相同或者相似特征的字段定义相同的映射,此时可以使用Dynamic Templates。

定义映射模板

"dynamic_templates": [
  {
    
    
    "my_template_name": {
    
    
      ... match conditions ...
      "mapping":{
    
    ...}
    }
  },
  ...
]

规则判定:conditlons

match_mapping_type

用于匹配数据类型

例子

PUT templates_text
{
    
    
  "mappings": {
    
    
    "dynamic_templates": [
      {
    
    
        "integers":{
    
    
          "match_mapping_type": "long",
          "mapping": {
    
    
            "type": "integer"
          }
        }
      },
      {
    
    
        "integers":{
    
    
          "match_mapping_type":"string",
          "mapping": {
    
    
            "type": "keyword"
          }
        }
      }
      ]
  }
}

插入数据,对应的字段将会自动带上对应的类型。

PUT templates_text/_doc/1
{
    
    
  "my_integer": 500,
  "my_string": "smz"
}
      "properties": {
    
    
        "my_integer": {
    
    
          "type": "integer"
        },
        "my_string": {
    
    
          "type": "keyword"
        }
      }

match、unmatch

用以匹配字段名称规则,支持通配符、正则表达式。

正则:

 "match_pattern": "regex",
 "match": "^profit_\d+$"

示例:

PUT templates_text2
{
    
    
  "mappings": {
    
    
    "dynamic_templates": [
      {
    
    
        "longs_as_strings":{
    
    
          "match_mapping_type": "string",
          "match": "num_*", #以num_*开头
          "unmatch": "*_text",#不以_text结尾
          "mapping": {
    
    
            "type": "long"
          }
        }
      }
      ]
  }
}

path_match、path_unmatch

用于嵌套字段

用例:

PUT templates_text3
{
    
    
  "mappings": {
    
    
    "dynamic_templates": [
      {
    
    
        "full_name":{
    
    
          "path_match": "name.*", #路径
          "path_unmatch": "*.middle",#路径不匹配
          "mapping": {
    
    
            "type": "text",
            "copy_to": "full_name"#符合的字段拷贝到此
          }
        }
      }
      ]
  }
}

分词器变量

PUT templates_text4
{
    
    
  "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"
          }
        }
      }
      ]
  }
}

以上示例,定义的两个模板,有以下规则:

  • 所有字符串类型的字段,其分词器都会定义为和字段名称一致的分词器
  • 所有非文本类型的字段,都会关闭doc_values

总结

本文主要记录了ES映射以及映射配置、映射模板。

猜你喜欢

转载自blog.csdn.net/smznbhh/article/details/131865008