1:显示映射

  1. PUT test
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 1,
  6. },
  7. "mappings": {
  8. "_doc": {
  9. "properties": {
  10. "id": {
  11. "type": "long"
  12. },
  13. "message": {
  14. "type": "keyword"
  15. },
  16. "date":{
  17. "type":"date",
  18. "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis"
  19. }
  20. }
  21. }
  22. }
  23. }

显示映射的字段参数见:字段参数 字段数据类型
录入数据时,如果是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

2:动态映射

配置项 作用 注意事项 默认值 使用场景 示例
match_mapping_type 根据ES检测到的数据类型,定义数据类型
match 和unmatch 根据名称或规则,定义数据类型

path_match 和path_unmatch 根据路径上的字段规则,定义数据类型

match_mapping_type示例

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    ]
  }
}

PUT my-index-000001/_doc/1
{
  "my_integer": 5, 
  "my_string": "Some string" 
}

match 和unmatch示例

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "longs_as_strings": {
          "match_mapping_type": "string",
          "match":   "long_*",
          "unmatch": "*_text",
          "mapping": {
            "type": "long"
          }
        }
      }
    ]
  }
}

path_match 和path_unmatch示例

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": {
          "path_match":   "name.*",
          "path_unmatch": "*.middle",
          "mapping": {
            "type":       "text",
            "copy_to":    "full_name"
          }
        }
      }
    ]
  }
}

PUT my-index-000001/_doc/1
{
  "name": {
    "first":  "John",
    "middle": "Winston",
    "last":   "Lennon"
  }
}

思考:
1:match 对子字段是否生效?

# 1 建索引,使字段名称long_开头的字段,转为keyword
PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "longs_as_strings": {
          "match":   "long_*",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ]
  }
}

# 测试 1 ,结果:long_1 变成了keyword类型 ↓
PUT my-index-000001/_doc/1
{
  "long_1":123456789
}

# 测试 2 ,父字段是long_开头,子字段是object(test2)。结果:报错,对象类型转keyword时报错 ↓
PUT my-index-000001/_doc/2
{
  "long_2": {
    "test2":"a123456789"
  }
}
# 测试3 父字段是普通数据(无索引定义的规则匹配)开头,子字段是object,且字段long_开头。
结果,long_2 变成了keyword类型 √
PUT my-index-000001/_doc/3
{
  "test2": {
    "long_2":"123456789"
  }
}

#测试4,完全不用匹配,结果:test2被ES自动识别成了text,且有keyword子字段
此测试主要是和测试3做对比,如果不匹配mapping定义的规则,则ES会自动转成数字或者text,
不会直接定义成keyword,测试3变成了keyword,说明mapping规则对子字段生效了。
PUT my-index-000001/_doc/4
{
  "test2": {
    "test_2":"a123456789"
  }
}

# 结论
match 对子字段生效

2:match 对nest子字段是否生效?

动态模板不会有neseted

3:如果同时命中path_match 和 match 规则,生效优先级?

DELETE my-index-000001
# 1 定义两个规则
PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "longs_as_strings": {
          "match":   "long_*",
          "mapping": {
            "type": "float"
          }
        }
      },
      {
        "longs_as_strings2": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "keyword"

          }
        }
      }
    ]
  }
}

# 3 录入数据,结果:long_1是float类型
PUT my-index-000001/_doc/2
{
  "long_1": 123456
}

GET my-index-000001

# 调整规则顺序
DELETE my-index-000001

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "longs_as_strings2": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "keyword"

          }
        }
      },
      {
        "longs_as_strings": {
          "match":   "long_*",
          "mapping": {
            "type": "float"
          }
        }
      }
    ]
  }
}

# 输入数据
PUT my-index-000001/_doc/2
{
  "long_1": 123456
}
# 结果,long_1是keyword类型
GET my-index-000001
#结论:规则按顺序匹配,且命中一次