Elasticsearch Mapping 学习笔记

简介

Mapping就是ES数据字段的元数据,ES在创建索引的时候dynamic mapping会自动为不同的数据指定对应的 mapping , mapping 中包含了字段的类型、搜索方式(exact value或者full text)、分词器等。可以使用GET /index_name/_mappings查看一个索引的mapping

动态索引

Dynamic mapping具有自动推导数据类型的能力,因为ES的mapping_type是有JSON分析器检测数据类型的,而JSON没有隐式类型转换(int->long,float->double),因此在推导数据类型有一个原则,dynamic mapping会选择一个比较宽的数据类型。下表给出常用的几个数据类型的动态映射:

原始数据最终转换类型
字符串text/keyword
正数long
小数double(实际测试味float,应该是一个BUG)
布尔值boolean
日前(yyyy-mm-dd)date

搜索方式

  • exact value:精确匹配,在倒排索引的过程中,分词器会将整个字段的值创建到索引中;

  • full text:全文检索,分词、近义词、混淆词、大小写、词性、过滤、时态转换等;

ES常见数据类型

  • 核心数据类型
  1. 数字类型:long integer short byte double float half_float scaled_float,在满足需求的情况下,尽可能选择范围小的数据类型。

  2. 字符串:keyword/text,适用于索引结构化的字段,可以用于过滤、排序、聚合。keyword类型的字段只能通过精确值(exact value)搜索到。Id应该用keyword;当一个字段是要被全文搜索的,比如Email内容、产品描述,这些字段应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合。(解释一下为啥不会为text创建索引:字段数据会占用大量堆空间,尤其是在加载高基数text字段时。字段数据一旦加载到堆中,就在该段的生命周期内保持在那里。同样,加载字段数据是一个昂贵的过程,可能导致用户遇到延迟问题。这就是默认情况下禁用字段数据的原因)。有时,在同一字段中同时具有全文本(text)和关键字(keyword)版本会很有用:一个用于全文本搜索,另一个用于聚合和排序。

  3. 日期类型:date,它是exact value的;

  4. 布尔类型:boolean,其值为true/false;

  5. 二进制类型:binary

  6. 区间类型:integer_range、float_range、long_range、double_range、date_range

  • 复杂数据类型
  1. object数据类型:用于单个JSON对象

  2. Nested数据类型:用于JSON对象数组

  • 地理位置
  1. geo-point:纬度/经度积分

  2. geo-shape:用于多边形等复杂形状

  • 特有类型
  1. IP地址:ip用于IPv4和IPv6地址

  2. Completion:提供自动完成建议

  3. Tocken_count:计算字符串中令牌的数量

  4. Murmur3:在索引时计算值的哈希并将其存储在索引中

  5. Annotated-text:索引包含特殊标记的文本(通常用于标识命名实体)

  6. Percolator:接受来自query-dsl的查询

  7. Join:为同一索引内的文档定义父/子关系

  8. Rank features:记录数字功能以提高查询时的点击率。

  9. Dense vector:记录浮点值的密集向量。

  10. Sparse vector:记录浮点值的稀疏向量。

  11. Search-as-you-type:针对查询优化的文本字段,以实现按需输入的完成

  12. Alias:为现有字段定义别名。

  13. Flattened:允许将整个JSON对象索引为单个字段。

  14. Shape:shape 对于任意笛卡尔几何。

  15. Histogram:histogram 用于百分位数聚合的预聚合数值。

  16. Constant keyword:keyword当所有文档都具有相同值时的情况的 专业化。

  • 数组

在Elasticsearch中,数组不需要专用的字段数据类型。默认情况下,任何字段都可以包含零个或多个值,但是,数组中的所有值都必须具有相同的数据类型。

  • ES7新增的类型
  1. date_nanos: date类型增强,支持纳秒

  2. features

  3. vector

创建索引时指定mapping

PUT /person
{
	"mappings": {
		"properties": {
      "pid": {
      	"type": "long"
      },
      "name": {
      	"type": "text",
      	"fields": {
      		"keyword": {
      			"type":"keyword",
      			"ignore_above": 1024
      		}
      	}
      },
      "age": {
      	"type": "short"
      },
      "gender": {
      	"type": "keyword"
      },
      "birthday": {
      	"type": "date"
      },
      "desc": {
      	"type": "text",
      	"analyzer": "english"
      },
      "salary": {
      	"type": "float",
      	"index": false
      },
      "tags": {
      	"type": "text",
      	"index": true
      },
      "parts": {
      	"type": "object"
      },
      "partlist": {
      	"type": "nested"
      }

		}
	}
}

mapping的详细配置参数

  • index:是否对创建对当前字段创建索引,默认true,如果不创建索引,该字段不会通过索引被搜索到,但是仍然会在source元数据中展示

  • analyzer:指定分析器(character filter、tokenizer、Token filters)

  • boost:对当前字段相关度的评分权重,默认1。

{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "boost": 2 
      },
      "content": {
        "type": "text"
      }
    }
  }
}

在5.x版本已经废弃了

  • coerce:是否允许强制类型转换
PUT my-index
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer"
      },
      "number_two": {
        "type": "integer",
        "coerce": false
      }
    }
  }
}

# 转换成功,插入数据成功
PUT my-index/_doc/1
{
  "number_one": "10" 
}

# 转换失败,插入数据失败
PUT my-index/_doc/2
{
  "number_two": "10" 
}

这种索引级别的默认值可以在settings配置段中设置"index.mapping.coerce": false

  • copy_to:用于将数据写入到同一个字段,便于全文检索
PUT my-index
{
  "mappings": {
    "properties": {
      "first_name": {
        "type": "text",
        "copy_to": "full_name" 
      },
      "last_name": {
        "type": "text",
        "copy_to": "full_name" 
      },
      "full_name": {
        "type": "text"
      }
    }
  }
}

PUT my-index/_doc/1
{
  "first_name": "John",
  "last_name": "Smith"
}

GET my-index/_search
{
  "query": {
    "match": {
      "full_name": { 
        "query": "John Smith",
        "operator": "and"
      }
    }
  }
}

可以将first_name的值和last_name的值放入到full_name字段的索引中,需要注意在返回结果中full_name不会被显示,只作为查询条件的字段,不作为返回结果的字段

  • doc_values:为了提升排序和聚合效率,默认true,如果确定不需要对字段进行排序或聚合,也不需要通过脚本访问字段值,则可以禁用doc值以节省磁盘空间(不支持text和annotated_text)
PUT my-index
{
  "mappings": {
    "properties": {
      "status_code": { 
        "type": "keyword"
      },
      "session_id": { 
        "type": "keyword",
        "doc_values": false
      }
    }
  }
}
  • dynamic:控制索引是否可以动态新增mapping字段,默认true,它接受三种设置true|false|strict,false新检测到的字段将被忽略。这些字段将不会被索引,因此将无法搜索,但仍会出现在_source返回的匹配项中。这些字段不会添加到映射中,必须显式添加新字段。strict如果检测到新字段,则会引发异常并拒绝文档。必须将新字段明确添加到映射中。
PUT my-index
{
  "mappings": {
    "dynamic": false, 
    "properties": {
      "user": { 
        "properties": {
          "name": {
            "type": "text"
          },
          "social_networks": { 
            "dynamic": true,
            "properties": {}
          }
        }
      }
    }
  }
}

动态索引在index级别被禁用,user.social_networks级别开启了自动索引。

  • eager_global_ordinals:用于聚合的字段上,主要作用是优化聚合性能。

有些索引使用率很高,会被保存在内存中,有些使用率特别低,宁愿在使用的时候重新创建,在使用完毕后丢弃数据,Frozen indices的数据命中频率小,不适用于高搜索负载,数据不会被保存在内存中,堆空间占用比普通索引少得多,Frozen indices是只读的,请求可能是秒级或者分钟级。eager_global_ordinals不适用于Frozen indices。

PUT my-index-000001/_mapping
{
  "properties": {
    "tags": {
      "type": "keyword",
      "eager_global_ordinals": true
    }
  }
}
  • enable:是否创建倒排索引,可以对字段操作,也可以对索引操作,如果不创建索引,让然可以检索并在_source元数据中展示,谨慎使用,该状态无法修改。有两种使用方式:
PUT my_index
{
  "mappings": {
    "enabled": false 
  }
}

PUT my_index
{
  "mappings": {
    "properties": {
      "session_data": {
        "type": "object",
        "enabled": false
      }
    }
  }
}
  • fielddata:查询时内存数据结构,在首次用当前字段聚合、排序或者在脚本中使用时,需要字段为fielddata数据结构,并且创建倒排索引保存到堆中。设置这个字段有OOM风险

  • format:格式化

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}
  • ignore_above:超过长度将被忽略
PUT /person
{
	"mappings": {
		"properties": {
      "pid": {
      	"type": "long"
      },
      "name": {
      	"type": "text",
      	"fields": {
      		"keyword": {
      			"type":"keyword",
      			"ignore_above": 1024
      		}
      	}
      }
    }
  }
}
  • ignore_malformed:忽略类型错误
PUT my_index
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer",
        "ignore_malformed": true
      },
      "number_two": {
        "type": "integer"
      }
    }
  }
}

PUT my_index/_doc/1
{
  "text":       "Some text value",
  "number_one": "foo"
}  //虽然有异常 但是不抛出

PUT my_index/_doc/2
{
  "text":       "Some text value",
  "number_two": "foo"
}  //数据格式不对
  • index_options:控制将哪些信息添加到反向索引中以进行搜索和突出显示。仅用于text字段

  • index_phrases:提升exact_value查询速度,但是要消耗更多磁盘空间

  • index_prefixes:前缀搜索

    • min_chars:前缀最小长度,>0,默认2(包含)

    • max_chars:前缀最大长度,<20,默认5(包含)

  • meta:附加元数据

  • ormalizer:

  • norms:是否禁用评分(在filter和聚合字段上应该禁用)

  • null_value:为null值设置默认值

  • position_increment_gap:

  • proterties:除了mapping还可用于object的属性设置

  • search_analyzer:设置单独的查询时分析器

PUT my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
      }
    }
  }
}

PUT my_index/_doc/1
{
  "text": "Quick Brown Fox"
}

GET my_index/_search
{
  "query": {
    "match": {
      "text": {
        "query": "Quick Br", 
        "operator": "and"
      }
    }
  }
}

  • similarity:为字段设置相关度算法,支持BM25、claassic(TF-IDF)、boolean

  • store:设置字段是否仅查询

  • term_vector:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值