• Mapping类似数据库中的schema的定义,作用如下
    • 定义索引汇总字段的名称
    • 定义字段的数据类型,如字符串、数字,时间。。。
    • 字段、倒排索引的相关配置(Analyzed or Not Analyzed, Analyzer)
  • Mapping会吧JSON文档映射成Lucene所需的扁平格式
  • 一个Mapping属于一个索引的Type
    • 每个document都属于一个Type
    • 一个Type有一个Mapping定义
    • 7.0开始,不需要在Mapping定义中指定Type信息

字段的数据类型

  • 简单类型
    • Text / Keyword
    • Date
    • Integer / Floating
    • Boolean
    • IPv4 & IPv6
  • 复杂类型
    • 对象和嵌套对象
  • 特殊类型
    • geo_point & geo_shape / percolator (地理信息)

Dynamic Mapping

  • 写入文档时,如果索引不存在,会自动个创建索引
  • Dynamic Mapping的机制,使得我们无需手动定义mappings
  • 有时推断不准,如地理位置信息
  • 当类型设置不对时,会导致查询异常,如range查询(待查询数字类型,但是字段为字符串类型,range时会出错)

    类型自动识别

JSON类型 Elasticsearch类型
字符串
- 匹配日期格式,设置成Date
- 配置数字设置为float或long类型,该选项默认关闭
- 设置为Text类型,并增加keyword子字段
布尔值 boolean
浮点数 float
整数 long
对象 object
数组 由第一个非空数值的类型所决定
空值 忽略

例子

  1. POST mapping_test/_doc
  2. {
  3. "uid": "123.123", # text, keyword
  4. "isVIP": true, # boolean
  5. "isAdmin": "true", # text, keyword
  6. "age": 18, # long
  7. "heigh": 180.3 # float
  8. "createTIme": "2019-12-26 00:00:00", # date
  9. }
  10. # Dynamic Mapping结果
  11. GET mapping_test/_mapping
  12. {
  13. "mapping_test" : {
  14. "mappings" : {
  15. "properties" : {
  16. "age" : {
  17. "type" : "long"
  18. },
  19. "createTime" : {
  20. "type" : "date"
  21. },
  22. "heigh" : {
  23. "type" : "float"
  24. },
  25. "isAdmin" : {
  26. "type" : "text",
  27. "fields" : {
  28. "keyword" : {
  29. "type" : "keyword",
  30. "ignore_above" : 256
  31. }
  32. }
  33. },
  34. "isVIP" : {
  35. "type" : "boolean"
  36. },
  37. "uid" : {
  38. "type" : "text",
  39. "fields" : {
  40. "keyword" : {
  41. "type" : "keyword",
  42. "ignore_above" : 256
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }

能否更改Mapping的字段类型

新加入的字段

  • Dynamic设置为true时,一旦有新增字段的文档写入,Mapping也会同时更新
  • Dynamic设置为false时,Mapping不会被更新,新增字段的数据无法被索引,但是信息会在 _source 中出现
  • Dynamic设置成strict, 文档写入失败

    已有字段

  • 一旦已经有数据写入,就不再支持修改字段定义

    • Lucene实现的倒排索引,一旦生成后,就不允许修改
  • 如果希望改变字段类型,必须Reindex API , 重建索引

    原因

  • 如果修改了字段的数据类型,会导致已经被索引的属性无法被搜索

  • 如果是增加了新的字段,就不会有这样的影响

    控制Dynamic Mappings更新增字段策略

true false strict
文档可索引 YES YES NO
字段可索引 YES NO NO
Mapping被更新 YES NO NO
  • 当dynamic被设置成false时,存在新增字段的数据写入,该数据可被索引,但是新增字段被丢西
  • 当设置成strict时,数据写入直接出错

    设置方式

  1. PUT mapping_test/_mapping
  2. {
  3. "dynamic": "true"
  4. }

测试

DELETE mapping_test
POST mapping_test/_doc
{
  "uid": "123.123"
}
# 查看第一次的mapping
GET mapping_test/_mapping
# 结果
{
  "mapping_test" : {
    "mappings" : {
      "properties" : {
        "uid" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

# 设置mapping 为false
PUT mapping_test/_mapping
{
  "dynamic": "false"
}

# 新增字段
POST mapping_test/_doc
{
  "uid": "123.123",
  "isVIP": true
}
# 查看结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mapping_test",
        "_type" : "_doc",
        "_id" : "pP76QG8BQ6e2WndFtrk4",
        "_score" : 1.0,
        "_source" : {
          "uid" : "123.123"
        }
      },
      {
        "_index" : "mapping_test",
        "_type" : "_doc",
        "_id" : "yf77QG8BQ6e2WndFFLlJ",
        "_score" : 1.0,
        "_source" : {
          "uid" : "123.123",
          "isVIP" : true   # 新增的字段在_source中出现
        }
      }
    ]
  }
}

# 查看mapping, mapping中没有出现isVIP
{
  "mapping_test" : {
    "mappings" : {
      "dynamic" : "false",
      "properties" : {
        "uid" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

# 设置mapping为strict
PUT mapping_test/_mapping
{
  "dynamic": "strict"
}

# 新增字段
POST mapping_test/_doc
{
  "uid": "123.123",
  "isVIP": true
}
# 结果报错
{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [isVIP] within [_doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [isVIP] within [_doc] is not allowed"
  },
  "status": 400
}

显示设置Mapping

定义

PUT mapping_test
{
  "mappings": {
    "properties": {
      "uid": {
        "type": "text"
      }
    }
  }
}
# 查看mapping
GET mapping_test/_mapping
{
  "mapping_test" : {
    "mappings" : {
      "properties" : {
        "uid" : {
          "type" : "text"
        }
      }
    }
  }
}

建议

  • 可以参考API手册,纯手写
    • 为减少输入的工作量,可以通过以下步骤
    • 创建临时的index,写入一些样本数据
    • 通过 GET tempindex/_mapping ,获取样本的动态Mapping定义
    • 修改后用,使用该配置创建的索引
    • 删除临时索引

      倒排索引控制

      控制字段是否被index

PUT users
{
  "mappings": {
    "properties": {
      "mobile": {
        "type": "text",
        "index": false   # 控制字段不被索引,查询该字段是会报错
      },
      "username": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

根据mobile查询会报如下错误:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"match\" : {\n    \"mobile\" : {\n      \"query\" : \"1111\",\n      \"operator\" : \"OR\",\n      \"prefix_length\" : 0,\n      \"max_expansions\" : 50,\n      \"fuzzy_transpositions\" : true,\n      \"lenient\" : false,\n      \"zero_terms_query\" : \"NONE\",\n      \"auto_generate_synonyms_phrase_query\" : true,\n      \"boost\" : 1.0\n    }\n  }\n}",
        "index_uuid": "LHPDjKVbQDCqBf8QizDr2g",
        "index": "users"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "users",
        "node": "griHJ7c0SkiqV9bhOUQMRQ",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"match\" : {\n    \"mobile\" : {\n      \"query\" : \"1111\",\n      \"operator\" : \"OR\",\n      \"prefix_length\" : 0,\n      \"max_expansions\" : 50,\n      \"fuzzy_transpositions\" : true,\n      \"lenient\" : false,\n      \"zero_terms_query\" : \"NONE\",\n      \"auto_generate_synonyms_phrase_query\" : true,\n      \"boost\" : 1.0\n    }\n  }\n}",
          "index_uuid": "LHPDjKVbQDCqBf8QizDr2g",
          "index": "users",
          "caused_by": {
            "type": "illegal_argument_exception", # 因为mobile字段不能被索引
            "reason": "Cannot search on field [mobile] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400
}

index options

PUT users
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer",
        "index_options": "freqs", # index options级别
      }
    }
  }
}
  • 四种不同级别的 Index Options配置。可以控制倒排索引记录的内容
    • docs - 记录 doc id
    • freqs - 记录 doc id 和 term frequencies
    • positions - 记录 doc id , term frequencies , term position
    • offsets - doc id, term frequencies , term position, character offects
  • Text类型默认记录 positions, 其他默认为docs
  • 记录内容越多,占用空间越大

    null_value 处理

  • 如果需要对Null值实现搜索

  • 只有keyword类型支持设定 Null_Value

copy_to 设置

  • 满足一些特定的搜索需求
  • copy_to将字段的数值拷贝到目标字段,实现类似_all的作用
  • copy_to的目标字段不出现在_source中

    数组类型

  • es中不提供专门的数组类型。但是任何字段,都可以包含多个相同类型的数值