elasticsearch映射

阅读说明:

  1. 如果有排版格式问题,请移步www.yuque.com/mrhuang-ire… 《elasticsearch映射》,选择宽屏模式效果更佳。
  2. 本文为原创文章,转发请注明出处。

Mapping也叫映射,是elasticsearch中定义文档极其包含的字段如何存储和如何索引的流程。类似于Mysql中的表结构中字段定义(字段名、字段类型等)以及索引。在Mapping中,定义字段名,字段类型,字段所用分词器、索引等属性。

映射基础操作

下面有一个简单的设置和查看mapping的例子。

创建映射

PUT /my-index      //对my-index显示建立映射
{
  "mappings": {
    "properties": {
      "age":    { "type": "integer" },   //包含字段age, 类型为integer
      "email":  { "type": "keyword"  },  //包含字段email, 类型为keyword, 不进行分词
      "name":   { "type": "text"  }      //包含字段name, 类型为text
    }
  }
}

更新映射

支持新增映射字段。

PUT /my-index/_mapping
{
  "properties": {
    "employee-id": {   //新增employee-id
      "type": "keyword",  
      "index": false
    }
  }
}

因为更新现有字段可能导致索引无效,所以不支持更新现有字段。如果只是想要更改字段名,可以使用alias参数设置别名指向原始字段。

PUT /my-index/_mapping
{
    "properties": {
      "name_alias": {
        "type": "alias",
        "path": "name" 
      }
    }
}

如果确实需要更改现有字段,需要新建索引设置映射,使用reindex指令在新索引上进行重建。

POST _reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}

查看映射

GET /my-index/_mapping

返回映射配置

{"my-index":{"mappings":{"properties":{"age":{"type":"integer"},"email":{"type":"keyword"},"employee-id":{"type":"keyword","index":false},"name":{"type":"text"},"name_alias":{"type":"alias","path":"name"}}}}}

查看指定字段

GET /my-index/_mapping/field/employee-id

返回

{"my-index":{"mappings":{"employee-id":{"full_name":"employee-id","mapping":{"employee-id":{"type":"keyword","index":false}}}}}}

字段数据类型

在映射配置中,type定义了字段的类型。elasticsearch定义了数字类型,布尔,字符串、时间类型,json对象,除此之外还定义空间数据、文档排名、文本搜索类型。对于字符串类型,有一个点需要注意,,eleasticsearch会进行分词建立索引。

大类类型名称说明对应java类型
字符串text字段内容会被分词,适合match模糊搜索String
keyword跟text的区别是keyword保留完整字段,适合term精确查找String
数值long64 位有符号整形long
integer32 位有符号整形int
short16 位有符号整形short
byte8 位有符号整形。byte
double双精度 64位浮点类型double
float单精度 32位浮点类型float
half_float半精度16位浮点类型, 范围从2-24~65504float
scaled_float缩放类型浮点数, 配合缩放因子scaling_factor一起使用。
ES索引时,原始值会乘以该缩放因子并四舍五入得到新整形值,ES内部储存的是这个新值,但返回结果仍是原始值,好处是整型比浮点型更易压缩,节省磁盘空间。
比如:假设scale_factor为100, scaled_float = 10.25, 在es内部存储时为1025

| float | | 日期 | date | 存储最高精度为毫秒。 配合format使用: strict_date_optional_time||epoch_millis。 | | | | date_nanos | date的补充,最高精度是纳秒,高精度意味着可存储的日期范围小,即:从大约 1970 到 2262。 | | | 布尔类型 | boolean | 布尔类型。 | boolean | | 二进制 | binary | 存储二级制数据。 | | | 对象类型 | object | 简单json类型。 | json对象 | | | nested | 嵌套类型,解决object下数组内部对象之间没有关联的问题。 比如: {"user":[{"first":"John","last":"Smith"},{"first":"Alice","last":"White"}]} 在object下转换成:{"user.first":["alice","john"],"user.last":["smith","white"]},丢失了John和Smith, Alice和White是配套的关系。 | json对象 | | 地理类型 | geo_point | 纬度和经度点。 | | | | geo_shape | 复杂的形状,例如多边形 | | | | ip | ipv4或者ipv6地址 | |

动态映射

eleasticsearch支持动态mapping, 当插入文档时,不必先创建mapping, 只需建好索引,系统会根据字段自动映射。

字段类型自动映射类型
true/falseboolean
小数float
数字long
objectobject
数组取决于数组中的第一个非空元素的类型
日期格式字符串date
数字类型字符串float/long
其他字符串text + keyword

除了上述字段类型之外,其他类型都必须显式映射,在映射配置中对字段显示申明要映射的类型。如果希望对符合某类要求的特定字段制定映射,就需要用到动态映射模板。 动态映射模板语法

  "dynamic_templates": [
    {
      "my_template_name": {   //模板名
        ... match conditions ...  //匹配参数,支持match_mapping_type, match, match_pattern, unmatch, path_match, path_unmatch
        "mapping": { ... }   //字段被映射配置
      }
    },
    ...
  ]

动态映射模板参数:

  • match_mapping_type:数据类型匹配字段
  • match和unmatch:名称匹配字段,支持正则表达式, 支持数组。
  • path_match和path_unmatch :路径匹配字段, 支持数组

示例:

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "match":   "num_*",
          "unmatch": "*_text",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      {
        "full_name": {
          "path_match":   "name.*",
          "path_unmatch": "*.middle",
          "mapping": {
            "type":       "text",
            "copy_to":    "full_name"
          }
        }
      }
    ]
  }
}


//插入数据
PUT my-index-000001/_doc/1
{
  "long_num": "5", 
  "long_text": "foo" 
}

//插入数据
PUT my-index/_doc/2
{
  "one_ip":   "will not match", 
  "ip_two":   "will not match", 
  "three_ip": "12.12.12.12", 
  "ip_four":  "13.13.13.13" 
}

//插入数据
PUT my-index-000001/_doc/3
{
  "name": {
    "first":  "John",
    "middle": "Winston",
    "last":   "Lennon"
  }
}

以上代码会产生以下效果:

  • 所有 long 类型字段会默认映射为 integer。
  • 所有文本字段,如果是以 num_ 开头,并且不以 _text 结尾,会自动映射为 keyword 类型。
  • name json对象内的字段,除掉middle之外,其他 都会被映射为text类型。

更多映射参数

前面只简单介绍了type字段,elasticsearch还有很多其他映射参数可以使用。

参数名参数说明
type字段类型
index是否索引
alias设置别名,path指定要设置别名的字段绝对路径。
index_options控制将哪些信息添加到倒排索引中以进行搜索和突出显示。仅用于text字段。包含以下参数:docs:只有文档编号被索引;freqs:文档编号和 term(词条)的频率会被索引;positons:文档编号、term frequencies(词条频率)和term(词条)位置(或顺序)将被索引;offsets:文档编号、term(词条)频率,位置,起始和结尾字符偏移量(用于映射该 term (词条)到原始字符串)将被索引。被分析器分析的字符串字段使用 position 作为默认值,其他的字段使用 docs作为默认值。
index_phrases提升 exact_value 查询速度,但是要消耗更多磁盘空间。
index_prefixes前缀搜索, 加速查询。min_chars:前缀最小长度> 0,默认 2(包含)max_chars:前缀最大长度< 20,默认 5(包含)
analyzer文本分析器,对文档内容分词建立倒排索引
search_analyzer搜索文本分析器,对传入的搜索词进行分词。如果没有定义search_analyzer,默认使用analyzer。
boost对当前字段相关度的评分权重,默认1。仅适用于 term(词条)查询(前缀,范围和模糊查询不能够使用 **boost **).
normalizer该参数对于keyword字段, 与analyzer相似
meta附加元数据
null_value为null时设置默认值
store设置该字段是否仅查询
ignore_above超过长度将被忽略。
format格式化。
dynamic控制是否可以动态添加新字段。
coerce是否允许强制类型转换
ignore_above超过长度将被忽略

参考:
[1].zhuanlan.zhihu.com/p/433879838 [2].blog.csdn.net/qq_39706570… [3].www.codebaoku.com/es/es-docum… [4].www.elastic.co/guide/en/el… mp.weixin.qq.com/s?__biz=Mzg…

posted @ 2023-09-12 00:11  扎Zn了老Fe  阅读(11)  评论(0编辑  收藏  举报  来源