elasticsearch 字段动态映射

        最近在学习 elasticsearch ,学到字段的动态映射,在这里做点小笔记。希望像我这 样的 初学者 有作用。

三种动态映射方式

        elasticsearch 的 mapping ,可以使用动态方式来创建字段的映射,减少手写的静态的 mapping 设置。就是在添加新数据的时候,动态的设置字段的mapping,就是动态地设置字段的类型和属性。
就是使用一定的匹配机制,把匹配上的数据字段,设置这些字段的类型和属性,不用事先手动的写上字段的 mapping。

 

1 字段类型匹配机制

        在添加数据的时候,根据es自身的推断出来的实际的数据类型来匹配。匹配成功之后就把字段的类型和属性设置成配置的

创建索引、查询索引。(运行代码之前,记得去掉注释)


运行结果

PUT my-dynamic-template-001
    {
      "mappings": {
        "dynamic_templates": [ // "dynamic_templates" 固定的写法
          {
            "integer_type": { // 名称,自定义
              "match_mapping_type": "long", // match_mapping_type 固定的,"long" 是要匹配的类型
              "mapping": { // "mapping" 这个是固定的写法
                "type": "integer" // "type" 是固定的写法,"integer" 将要设置的类型。这里就是匹配 long 类型,设置为 integer 类型
              }
            }
          },
          {
            "string_type": {
              "match_mapping_type": "string",
              "mapping": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        ],
        "properties": {
          "incom02": {
            "type": "double"
          }
        }
      }
    }

    DELETE my-dynamic-template-001
    GET my-dynamic-template-001

    POST my-dynamic-template-001/_doc
    {
      "incom": 1000,
      "incom01": "10000",
      "incom02": 1000,
      "companyName": "咕泡学院"
    }

{
  "my-dynamic-template-001" : {
    "aliases" : { },
    "mappings" : {
      "dynamic_templates" : [
        {
          "integer_type" : {
            "match_mapping_type" : "long",
            "mapping" : {
              "type" : "integer"
            }
          }
        },
        {
          "string_type" : {
            "match_mapping_type" : "string",
            "mapping" : {
              "ignore_above" : 256,
              "type" : "keyword"
            }
          }
        }
      ],
      "properties" : {
        "companyName" : {
          "type" : "keyword",
          "ignore_above" : 256
        },
        "incom" : {
          "type" : "integer"
        },
        "incom01" : {
          "type" : "keyword",
          "ignore_above" : 256
        },
        "incom02" : {
          "type" : "double"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "my-dynamic-template-001",
        "creation_date" : "1616303044155",
        "number_of_replicas" : "1",
        "uuid" : "nuq3rB0DT5GT4gYAubPzpQ",
        "version" : {
          "created" : "7110199"
        }
      }
    }
  }
}

2 字段名称匹配机制

        在添加数据的时候,es 会使用配置的字段名称正则表达式,去匹配数据字段的名称,把匹配成功的字段的类型和属性设置成配置的类型和属性。

创建索引、查询索引。(运行代码之前,记得去掉注释)

运行结果

        PUT my-dynamic-template-002
        {
          "mappings": {
            "dynamic_templates": [ // "dynamic_templates" 固定的写法
              {
                "integer_type": { // 名称,自定义
                  "match": "*come*", // "match" 固定写法,"*come*" 用于匹配字段名的正则表达式
                  "mapping": { // 固定写法
                    "type": "integer" // "type" 固定写法,"integer" 将要设置的类型。这里就是根据字段名正则匹配到的字段,设置为 "integer" 类型。
                  }
                }
              },
              {
                "string_type": {
                  "match": "company*",
                  "mapping": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            ],
            "properties": {
              "income4":{
                "type": "double"
              }
            }
          }
        }

        DELETE my-dynamic-template-002
        GET my-dynamic-template-002
        GET my-dynamic-template-002/_search

        POST my-dynamic-template-002/_doc
        {
          "income": 1000,
          "income01": "10000",
          "income02": 1000,
          "income4":2000,
          "companyName": "咕泡学院"
        }

{
  "my-dynamic-template-002" : {
    "aliases" : { },
    "mappings" : {
      "dynamic_templates" : [
        {
          "integer_type" : {
            "match" : "*come*",
            "mapping" : {
              "type" : "integer"
            }
          }
        },
        {
          "string_type" : {
            "match" : "company*",
            "mapping" : {
              "ignore_above" : 256,
              "type" : "keyword"
            }
          }
        }
      ],
      "properties" : {
        "companyName" : {
          "type" : "keyword",
          "ignore_above" : 256
        },
        "income" : {
          "type" : "integer"
        },
        "income01" : {
          "type" : "integer"
        },
        "income02" : {
          "type" : "integer"
        },
        "income4" : {
          "type" : "double"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "my-dynamic-template-002",
        "creation_date" : "1616304590742",
        "number_of_replicas" : "1",
        "uuid" : "h25GK-juTsSBgQaoZQRSSQ",
        "version" : {
          "created" : "7110199"
        }
      }
    }
  }
}
 
  

3 字段路径匹配机制

        在添加数据的时候,es 会使用配置的字段路径正则表达式,去匹配数据字段的路径,把匹配成功的字段的类型和属性设置成配置的类型和属性

创建索引、查询索引。(运行代码之前,记得去掉注释)

运行结果

        PUT my-dynamic-template-003
        {
          "mappings": {
            "dynamic_templates": [ // "dynamic_templates" 固定的写法
              {
                "incomaaa": { // 名称,自定义
                  "path_match": "finace.*", // "path_match" 固定写法,"finace.*" 正则表达式,用于匹配路径
                  "mapping": { // 固定写法
                    "type": "integer" // "type" 固定写法,"integer" 将要设置的类型。这里就是根据 "finace.*" 正则表达式匹配字段
                  }
                }
              }
            ]
          }
        }

        DELETE my-dynamic-template-003
        GET my-dynamic-template-003
        GET my-dynamic-template-003/_search

        POST my-dynamic-template-003/_doc
        {
          "companyName": "咕泡学院",
          "finace":{
            "income": 100,
            "outcom": "1001",
            "tax1": 10
          }
        }

{
  "my-dynamic-template-003" : {
    "aliases" : { },
    "mappings" : {
      "dynamic_templates" : [
        {
          "incomaaa" : {
            "path_match" : "finace.*",
            "mapping" : {
              "type" : "integer"
            }
          }
        }
      ],
      "properties" : {
        "companyName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "finace" : {
          "properties" : {
            "income" : {
              "type" : "integer"
            },
            "outcom" : {
              "type" : "integer"
            },
            "tax1" : {
              "type" : "integer"
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "my-dynamic-template-003",
        "creation_date" : "1616319657366",
        "number_of_replicas" : "1",
        "uuid" : "YVAC60reTq-he1We39lHVQ",
        "version" : {
          "created" : "7110199"
        }
      }
    }
  }
}
 
  

字段类型匹配机制的范围最广,最不好控制,很容易出错。
字段名称匹配机制的范围比字段类型 匹配机制的范围要小,相对字段类型匹配机制要好控制一些,没有那容易出错。

字段路径匹配机制的范围比字段名称机制的范围要小,相对字段名称匹配机制要好控制一些,没有那么容易出错。

所以,推荐使用字段路径匹配机制。当然,三种匹配机制是可以混搭的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值