[Elasticsearch] Elasticsearch 7: dynamic mapping

Insert picture description here

1 Overview

Reference: https://www.letianbiji.com/elasticsearch/es7-dynamic-mapping.html

Dynamic is used to configure the behavior of processing new fields, there are three types of true, false, and strict.

true 是默认值,会自动在 mapping 中添加字段,并在文档中保存新字段的值。
false 代表不会在 mapping 中添加字段,但会将数据存起来。
strict 代表既不会新增字段,也不会保存新字段的内容,遇到新字段直接报错。 

In addition, for nested fields, you can set them separately.

By default, we write data to the index. If some fields have not been defined before, ES will automatically guess the type and then generate it in the mapping.

for example:

PUT student
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "name": {
    
    
        "type": "keyword",
        "doc_values": false
      }
    }
  },
  "settings": {
    
    
    "number_of_shards": "8",
    "number_of_replicas": "2"
  }
}

Use bulk to insert data:

POST _bulk
{
    
     "index" : {
    
     "_index" : "student", "_id" : "1" } }
{
    
     "name" : "张三", "age": 12 }

Inquire:

GET student/_search

# 查询结果
{
    
    
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 8,
    "successful" : 8,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
    
    
          "name" : "张三",
          "age" : 12
        }
      }
    ]
  }
}

The query result has age.

View mapping:

GET student/_mapping

# 查询结果
{
    
    
  "student" : {
    
    
    "mappings" : {
    
    
      "dynamic" : "true",
      "properties" : {
    
    
        "age" : {
    
    
          "type" : "long"
        },
        "name" : {
    
    
          "type" : "keyword",
          "doc_values" : false
        }
      }
    }
  }
}

The age appears in the mapping and is automatically judged to be of the long type.

Sometimes, the automatically judged type may not meet our expectations, but once the type is determined, it cannot be changed. So we have to be cautious about this feature.

dynamic can be used to configure related behaviors.

Test: when dynamic uses the default value of true
When we do not configure dynamic, the default value of true is used. That is, the following two methods have the same effect:

Way 1:

PUT student
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "name": {
    
    
        "type": "keyword",
        "doc_values": false
      }
    }
  },
  "settings": {
    
    
    "number_of_shards": "8",
    "number_of_replicas": "2"
  }
}

Way 2:

PUT student
{
    
    
  "mappings": {
    
    
    "dynamic": "true",
    "properties": {
    
    
      "name": {
    
    
        "type": "keyword",
        "doc_values": false
      }
    }
  },
  "settings": {
    
    
    "number_of_shards": "8",
    "number_of_replicas": "2"
  }
}

Test: when dynamic is false

PUT student
{
    
    
  "mappings": {
    
    
    "dynamic": "false",
    "properties": {
    
    
      "name": {
    
    
        "type": "keyword",
        "doc_values": false
      }
    }
  },
  "settings": {
    
    
    "number_of_shards": "8",
    "number_of_replicas": "2"
  }
}

Insert data:

POST _bulk
{
    
     "index" : {
    
     "_index" : "student", "_id" : "1" } }
{
    
     "name" : "张三", "age": 12 }

Inquire:

GET student/_search

# 查询结果
{
    
    
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 8,
    "successful" : 8,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
    
    
          "name" : "张三",
          "age" : 12
        }
      }
    ]
  }
}

View mapping:

GET student/_mapping

# 查询结果
{
    
    
  "student" : {
    
    
    "mappings" : {
    
    
      "dynamic" : "false",
      "properties" : {
    
    
        "name" : {
    
    
          "type" : "keyword",
          "doc_values" : false
        }
      }
    }
  }
}

age does not appear in the mapping.

Test: when dynamic is strict

PUT student
{
    
    
  "mappings": {
    
    
    "dynamic": "strict",
    "properties": {
    
    
      "name": {
    
    
        "type": "keyword",
        "doc_values": false
      }
    }
  },
  "settings": {
    
    
    "number_of_shards": "8",
    "number_of_replicas": "2"
  }
}

Insert data:

POST _bulk
{
    
     "index" : {
    
     "_index" : "student", "_id" : "1" } }
{
    
     "name" : "张三", "age": 12 }

Will report an error:

{
    
    
  "took" : 29,
  "errors" : true,
  "items" : [
    {
    
    
      "index" : {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1",
        "status" : 400,
        "error" : {
    
    
          "type" : "strict_dynamic_mapping_exception",
          "reason" : "mapping set to strict, dynamic introduction of [age] within [_doc] is not allowed"
        }
      }
    }
  ]
}

Inquire:

GET student/_search

# 查询结果为空

View mapping:

GET student/_mapping

# 查询结果
{
    
    
  "student" : {
    
    
    "mappings" : {
    
    
      "dynamic" : "strict",
      "properties" : {
    
    
        "name" : {
    
    
          "type" : "keyword",
          "doc_values" : false
        }
      }
    }
  }
}

Guess you like

Origin blog.csdn.net/qq_21383435/article/details/109387538