dynamic

编辑

当你索引一个包含新字段的文档时,Elasticsearch 会动态地将该字段添加到文档或文档中的内部对象。以下文档添加了字符串字段 username、对象字段 name 以及 name 对象下的两个字符串字段。

resp = client.index(
    index="my-index-000001",
    id="1",
    document={
        "username": "johnsmith",
        "name": {
            "first": "John",
            "last": "Smith"
        }
    },
)
print(resp)

resp1 = client.indices.get_mapping(
    index="my-index-000001",
)
print(resp1)
response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    username: 'johnsmith',
    name: {
      first: 'John',
      last: 'Smith'
    }
  }
)
puts response

response = client.indices.get_mapping(
  index: 'my-index-000001'
)
puts response
const response = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    username: "johnsmith",
    name: {
      first: "John",
      last: "Smith",
    },
  },
});
console.log(response);

const response1 = await client.indices.getMapping({
  index: "my-index-000001",
});
console.log(response1);
PUT my-index-000001/_doc/1
{
  "username": "johnsmith",
  "name": { 
    "first": "John",
    "last": "Smith"
  }
}

GET my-index-000001/_mapping 

name 对象下的字段称为 name.firstname.last

检查映射以查看更改。

以下文档添加了两个字符串字段:emailname.middle

resp = client.index(
    index="my-index-000001",
    id="2",
    document={
        "username": "marywhite",
        "email": "[email protected]",
        "name": {
            "first": "Mary",
            "middle": "Alice",
            "last": "White"
        }
    },
)
print(resp)

resp1 = client.indices.get_mapping(
    index="my-index-000001",
)
print(resp1)
response = client.index(
  index: 'my-index-000001',
  id: 2,
  body: {
    username: 'marywhite',
    email: '[email protected]',
    name: {
      first: 'Mary',
      middle: 'Alice',
      last: 'White'
    }
  }
)
puts response

response = client.indices.get_mapping(
  index: 'my-index-000001'
)
puts response
const response = await client.index({
  index: "my-index-000001",
  id: 2,
  document: {
    username: "marywhite",
    email: "[email protected]",
    name: {
      first: "Mary",
      middle: "Alice",
      last: "White",
    },
  },
});
console.log(response);

const response1 = await client.indices.getMapping({
  index: "my-index-000001",
});
console.log(response1);
PUT my-index-000001/_doc/2
{
  "username": "marywhite",
  "email": "[email protected]",
  "name": {
    "first": "Mary",
    "middle": "Alice",
    "last": "White"
  }
}

GET my-index-000001/_mapping

在内部对象上设置 dynamic

编辑

内部对象继承其父对象的 dynamic 设置。在以下示例中,动态映射在类型级别被禁用,因此不会动态添加新的顶层字段。

但是,user.social_networks 对象启用了动态映射,因此你可以向此内部对象添加字段。

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "dynamic": False,
        "properties": {
            "user": {
                "properties": {
                    "name": {
                        "type": "text"
                    },
                    "social_networks": {
                        "dynamic": True,
                        "properties": {}
                    }
                }
            }
        }
    },
)
print(resp)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      dynamic: false,
      properties: {
        user: {
          properties: {
            name: {
              type: 'text'
            },
            social_networks: {
              dynamic: true,
              properties: {}
            }
          }
        }
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    dynamic: false,
    properties: {
      user: {
        properties: {
          name: {
            type: "text",
          },
          social_networks: {
            dynamic: true,
            properties: {},
          },
        },
      },
    },
  },
});
console.log(response);
PUT my-index-000001
{
  "mappings": {
    "dynamic": false, 
    "properties": {
      "user": { 
        "properties": {
          "name": {
            "type": "text"
          },
          "social_networks": {
            "dynamic": true, 
            "properties": {}
          }
        }
      }
    }
  }
}

禁用类型级别的动态映射。

user 对象继承类型级别的设置。

为此内部对象启用动态映射。

dynamic 的参数

编辑

dynamic 参数控制是否动态添加新字段,并接受以下参数

true

新字段将添加到映射中(默认)。

runtime

新字段作为运行时字段添加到映射中。这些字段不被索引,而是在查询时从 _source 加载。

false

新字段将被忽略。这些字段不会被索引或可搜索,但仍会出现在返回命中的 _source 字段中。这些字段不会被添加到映射中,必须显式添加新字段。

strict

如果检测到新字段,则会抛出异常并拒绝该文档。必须显式地将新字段添加到映射中。

达到字段限制时的行为

编辑

dynamic 设置为 trueruntime 只会添加动态字段,直到达到 index.mapping.total_fields.limit。默认情况下,超出字段限制的文档的索引请求将失败,除非 index.mapping.total_fields.ignore_dynamic_beyond_limit 设置为 true。在这种情况下,被忽略的字段将被添加到 _ignored 元数据字段