一览

配置项 作用 注意事项 默认值 使用场景 示例
_source(元数据属性) 存储post json内容到ES的原始文档 关闭后,存储在倒排索引中,无法在_source中查看,且无法reindex,update和update_by_query 开启 不存储原始数据,或者原始数据在其他地方有存储,或者开启了store,从store中查看数据。如果磁盘空间是一个问题,而是增加 压缩级别而不是禁用_source
index 是否加入倒排索引 关闭后无法对其进行搜索,字段仍会存储到_source和doc_values,字段可以被排序和聚合 开启 如果字段只需要聚合,不需要检索
doc_values 支持排序、聚合 会占用额外存储空间,与_source独立,同时开启doc_values和_source则会将该字段原始内容保存两份,数据在磁盘上采用列式存储,关闭后无法使用排序和聚合 开启 如果字段不需要聚合,可以关闭
enabled 是否对该字段进行处理 关闭后,只在_source中存储,类似index与doc_value的总开关 开启 不需要检索和聚合,只需要存储
store 是否单独存储该field 会占用额外存储空间,与_source独立,同时开启store和_source则会将该字段原始内容保存两份,字段单独存储,数据在磁盘上不连续,若读取多个字段需要seek多次,如需读取多个字段,需权衡比较_source与store效率 关闭
analyzer 指定分词器 standard 自定义分词器参考:https://www.yuque.com/timeasving/xovt29/ud3e8c
boost 建索引时,指定字段的权重 不建议建索引时指定,可查询时动态指定
coerce 强制数据类型转换 当设置false时:必须严格按mapping类型存储。比如设置integer类型,存储”10”将报错。设置为true时,允许存储10,10.1,”10”,但是存”10.1”时,倒排索引中是10,_source中是10.1,需要注意 true 一般不严格限制,默认true就可以
copy_to 将A和B字段索引时,拼接复制到C字段,查询时查C,可以查询A和B的内容。 1:对象类型不支持
2:copy_to后的值,在_source中和store都看不到,如果想看,可在query同级设置”stored_fields”: [“name”]。
3:拼接后的数据,是一个数组,不可进行折叠查看,折叠只支持单类型 collapse
使用效果和muity_match时类型使用cross_field效果一致。但是可以解决长尾数据问题和数据不均为分配时的召回顺序,其实底层就是新加一个字段,拼接两个字段到倒排索引中。通过 mapping加新字段和代码录入时处理效果一样。另外,新增的索引字段,同样生效
dynamic 是否支持动态增加字段 true:新字段被添加到映射中(默认)。
runtime:新字段作为运行时字段添加到映射中。这些字段未编入索引,并_source在查询时加载。
false:这些字段不会添加到映射中,必须显式添加新字段。
strict:新字段必须显式添加到映射中,动态插入未提前定义的字段报错
true 一般用于使用动态模板时使用,防止自定义产生一些垃圾字段。
eager_global_ordinals 全局唯一序号,提高聚合速度 1:千万不能在frozen index上使用,全局序数在每次搜索后都会被丢弃,并在被请求时重新构建。2:数据量巨大时慎用,高基数字段上的聚合可能会使用大量内存并触发字段数据断路器 false 聚合keyword,ip和flattened,这包括terms上面提到的聚合,以及 diversified_sampler和 significant_terms。
text需要启用fielddata以使用桶聚合
format 日期设置格式 格式需要对应才能索引数据 ISO的一些格式,包括毫秒和strict_date_optional_time等等 一般使用时,指定需要的格式即可,比如:”format”:”yyyy-MM-dd HH:mm:ss || yyyy-MM-dd ||epoch_millis”
ignore_above keyword过长时,限制长度,不会被索引和store 1:限制长度后,多余的部分不会存在于store模块和doc value中,但是会在_source中保存原始数据。
2:ES限制的是字节数量,但是Lucene使用的是字节,如果使用utf-8(最多4个字符),则最大长度支持:32766/4=8191
默认最大:32766,超过后会索引报错 keyword类型且可能很大的数据
ignore_malformed 忽略插入时类型错误 1:可以动态修改
2:支持的类型:number(统称),date,date_nanos,geo_point,geo_shape,ip
3:不能处理 nested,object,range 数据中的类型错误
false 可能发生类型错误,且不影响插入的场景,可以设置此参数 1:索引级别设置:”settings”: {
“index.mapping.ignore_malformed”: true
},
2:字段级别设置:”ignore_malformed”: false
index_options 1:此设置只支持text类型
2:倒排索引记录哪些值,支持:docs(编号),freqs(编号+词频),positions(默认,编号+词频+位置),offsets(词项开始和结束字符位置)
positions 不需要match_phase查询时,可考虑使用freqs,将不记录位置信息
index_phrases 优化match_phase查询,将词term两个合并成一个词组后进行查询 1:是text的属性
2:适用于match_phase
false 需要使用短语查询的地方,使用此设置,可提高一倍的查询性能。
index_prefixes 优化prefix查询,通过设置min_charts和max_charts,可以减少字段过少时,匹配过多的情况 1:适用于优化prefix查询 1:需要前缀查询的业务场景
2:对于suggest查询也有一定的场景使用,防止召回数据过多
meta ES 7.x版本下测试,不支持的类型
fields 子字段 非常常用,用于建立一个字段的不同子字段,比如:animal字段下有cat和dog
normalizer normalizer是 keyword的一个属性,可以对 keyword生成的单一 Term再做进一步的处理,比如 lowercase,即做小写变换 是keyword的属性 1:对keyword的进一步处理时有用
norms 是否text开启或关闭长度打分 1:text的属性
2:一般不需要设置
true 主要用于优化,暂未找到使用场景
null_value 空数据段时的默认值 1:数据为空需要设置默认值的业务场景。
position_increment_gap 设置数组中,每个元素之间的间隙,默认100。 适用于match_phase查询 100 1:值是数组,且短语查询时,根据业务需要,指定间隙长度。
properties 为nested或者object类型设置子类型时使用 适用于 nested或者object,子类(子字段)可以是任意类型。 很常用,当字段类型是nested或者object时使用
search_analyzer 搜索时指定分析器 适用于索引和搜索,使用不同分析器时使用。更加灵活。比如索引使用ngram,搜索可以设置成ngram类似模糊搜索,也可以指定成ik查询,或其他查询
similarity 指定相似度模型 1:建索引时设置 bm25 1:默认bm25可满足大部分场景
2:可调整bm25的一些参数
3:可自定义打分模型(需要进行插件开发,不常用)
term_vector no 不存储术语向量。(默认)
yes 只存储字段中的术语。
with_positions 存储条款和位置。
with_offsets 存储术语和字符偏移。
with_positions_offsets 存储术语、位置和字符偏移。
with_positions_payloads 存储术语、位置和有效载荷。
with_positions_offsets_payloads 存储术语、位置、偏移量和有效载荷。
1:
设with_positions_offset将使字段索引的大小加倍
2:可召回数据的向量信息
no 暂为找到使用场景

示例

dynamic示例

出入数据时,如果是ES自动检测数据类型,dynamic设置为true和runtime时,映射关系

插入数据时的数据类型 索引设置:”dynamic”:”true”时 索引设置:”dynamic”:”runtime”时
null 不自动添加字段类型 不自动添加字段类型
true or false boolean boolean
double float double
integer long long
object object 不自动添加字段类型
array 根据数组中第一个非空值来判断 根据数组中第一个非空值来判断
string类型,ES检测是日期 date date
string类型,ES检测是数字 float 或者 long double 或者 long
string类型,ES检测非日期也非数字 text和keword子字段 keyword
  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "dynamic": false,
  5. "properties": {
  6. "user": {
  7. "properties": {
  8. "name": {
  9. "type": "text"
  10. },
  11. "social_networks": {
  12. "dynamic": true,
  13. "properties": {}
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

_source示例

  1. PUT my-index-000001{
  2. "mappings": {
  3. "_source": {
  4. "excludes": [
  5. "*_not_source"
  6. ]
  7. },
  8. "properties": {
  9. "user_id": {
  10. "type": "keyword"
  11. },
  12. "session_data_not_source": {
  13. "type": "object"
  14. }
  15. }
  16. }
  17. }
  18. PUT my-index-000001/_doc/session_2
  19. { "user_id":"111",
  20. "session_data_not_source": {
  21. "some_array": [
  22. {
  23. "foo": "bar"
  24. },
  25. {
  26. "baz": 2
  27. }
  28. ]
  29. }
  30. }
  31. GET my-index-000001/_doc/session_2

copy_to示例

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "first_name": {
  6. "type": "text",
  7. "copy_to": "full_name"
  8. },
  9. "last_name": {
  10. "type": "text",
  11. "copy_to": "full_name"
  12. },
  13. "full_name": {
  14. "type": "text"
  15. }
  16. }
  17. }
  18. }
  19. PUT my-index-000001/_doc/1
  20. {
  21. "first_name": "John",
  22. "last_name": "Smith"
  23. }
  24. GET my-index-000001/_search
  25. {
  26. "query": {
  27. "match": {
  28. "full_name": {
  29. "query": "John Smith",
  30. "operator": "and"
  31. }
  32. }
  33. }
  34. }

match_phrase示例

  1. POST /my-index-000001/_mapping
  2. {
  3. "properties": {
  4. "text_index_phrase6_true": {
  5. "type": "text",
  6. "index_phrases": "true"
  7. }
  8. }
  9. }
  10. POST /my-index-000001/_mapping
  11. {
  12. "properties": {
  13. "text_index_phrase6_false": {
  14. "type": "text",
  15. "index_phrases": "false"
  16. }
  17. }
  18. }
  19. PUT my-index-000001/_doc/1
  20. {
  21. "text_index_phrase6_true" : "我爱广州小蛮腰",
  22. "text_index_phrase6_false" : "我爱广州小蛮腰"
  23. }
  24. GET my-index-000001/_search
  25. {
  26. "explain": true,
  27. "query": {
  28. "match": {
  29. "text_index_phrase6_true": "广州"
  30. }
  31. }
  32. }
  33. GET my-index-000001/_search
  34. {
  35. "explain": true,
  36. "query": {
  37. "match_phrase": {
  38. "text_index_phrase6_false": "广州"
  39. }
  40. }
  41. }
  42. GET /my-index-000001/_termvectors/1?fields=text_index_phrase6_true._index_phrase

normalizer示例

  1. PUT test_normalizer
  2. {
  3. "settings": {
  4. "analysis": {
  5. "normalizer": {
  6. "lowercase": {
  7. "type": "custom",
  8. "filter": [
  9. "lowercase"
  10. ]
  11. }
  12. }
  13. }
  14. },
  15. "mappings": {
  16. "doc": {
  17. "properties": {
  18. "type": {
  19. "type": "keyword"
  20. },
  21. "type_normalizer": {
  22. "type": "keyword",
  23. "normalizer": "lowercase"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. PUT test_normalizer/doc/1
  30. {
  31. "type": "apple",
  32. "type_normalizer": "apple"
  33. }
  34. PUT test_normalizer/doc/2
  35. {
  36. "type": "Apple",
  37. "type_normalizer": "Apple"
  38. }
  39. # 查询三
  40. GET test_normalizer/_search
  41. {
  42. "query": {
  43. "term":{
  44. "type":"aPple"
  45. }
  46. }
  47. }
  48. # 查询四
  49. GET test_normalizer/_search
  50. {
  51. "query": {
  52. "term":{
  53. "type_normalizer":"aPple"
  54. }
  55. }
  56. }

null_value示例

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "status_code": {
  6. "type": "keyword",
  7. "null_value": "_es_default_null"
  8. }
  9. }
  10. }
  11. }
  12. PUT my-index-000001/_doc/1
  13. {
  14. "status_code": "null"
  15. }
  16. PUT my-index-000001/_doc/2
  17. {
  18. "status_code": null
  19. }
  20. PUT my-index-000001/_doc/3
  21. {
  22. "status_code": []
  23. }
  24. GET my-index-000001/_search
  25. {
  26. "query": {
  27. "term": {
  28. "status_code": "_es_default_null"
  29. }
  30. }
  31. }
  32. # 结果:文档2

position_increment_gap示例

  1. # 1 可以搜索到数据
  2. PUT my-index-000001
  3. {
  4. "mappings": {
  5. "properties": {
  6. "names": {
  7. "type": "text",
  8. "position_increment_gap": 0
  9. }
  10. }
  11. }
  12. }
  13. PUT my-index-000001/_doc/1
  14. {
  15. "names": [ "John Abraham", "Lincoln Smith"]
  16. }
  17. GET my-index-000001/_search
  18. {
  19. "explain": true,
  20. "query": {
  21. "match_phrase": {
  22. "names": "Abraham Lincoln"
  23. }
  24. }
  25. }
  26. DELETE my-index-000001
  27. # 2 不设置slop不能搜索到数据,设置100以上的slope竟然搜索到了,证明默认长度时100
  28. PUT my-index-000001/_doc/1
  29. {
  30. "names": [ "John Abraham", "Lincoln Smith"]
  31. }
  32. GET my-index-000001/_search
  33. {
  34. "query": {
  35. "match_phrase": {
  36. "names": {
  37. "query": "Abraham Lincoln"
  38. }
  39. }
  40. }
  41. }
  42. GET my-index-000001/_search
  43. {
  44. "query": {
  45. "match_phrase": {
  46. "names": {
  47. "query": "Abraham Lincoln",
  48. "slop": 100
  49. }
  50. }
  51. }
  52. }

参考官网位置:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/mapping-params.html