coerce
编辑coerce
编辑数据并不总是干净的。根据数据的生成方式,数字可能在 JSON 主体中呈现为真正的 JSON 数字,例如 5
,但也可能呈现为字符串,例如 "5"
。或者,一个应该是整数的数字可能会呈现为浮点数,例如 5.0
,甚至 "5.0"
。
强制转换尝试清理脏值以使其符合字段的数据类型。例如
- 字符串将被强制转换为数字。
- 浮点数将被截断为整数值。
例如
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "number_one": { "type": "integer" }, "number_two": { "type": "integer", "coerce": False } } }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", document={ "number_one": "10" }, ) print(resp1) resp2 = client.index( index="my-index-000001", id="2", document={ "number_two": "10" }, ) print(resp2)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { number_one: { type: 'integer' }, number_two: { type: 'integer', coerce: false } } } } ) puts response response = client.index( index: 'my-index-000001', id: 1, body: { number_one: '10' } ) puts response response = client.index( index: 'my-index-000001', id: 2, body: { number_two: '10' } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { number_one: { type: "integer", }, number_two: { type: "integer", coerce: false, }, }, }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, document: { number_one: "10", }, }); console.log(response1); const response2 = await client.index({ index: "my-index-000001", id: 2, document: { number_two: "10", }, }); console.log(response2);
PUT my-index-000001 { "mappings": { "properties": { "number_one": { "type": "integer" }, "number_two": { "type": "integer", "coerce": false } } } } PUT my-index-000001/_doc/1 { "number_one": "10" } PUT my-index-000001/_doc/2 { "number_two": "10" }
可以使用更新映射 API更新现有字段上的coerce
设置值。
索引级默认值
编辑可以在索引级别设置 index.mapping.coerce
设置,以全局禁用所有映射类型的强制转换
resp = client.indices.create( index="my-index-000001", settings={ "index.mapping.coerce": False }, mappings={ "properties": { "number_one": { "type": "integer", "coerce": True }, "number_two": { "type": "integer" } } }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", document={ "number_one": "10" }, ) print(resp1) resp2 = client.index( index="my-index-000001", id="2", document={ "number_two": "10" }, ) print(resp2)
response = client.indices.create( index: 'my-index-000001', body: { settings: { 'index.mapping.coerce' => false }, mappings: { properties: { number_one: { type: 'integer', coerce: true }, number_two: { type: 'integer' } } } } ) puts response response = client.index( index: 'my-index-000001', id: 1, body: { number_one: '10' } ) puts response response = client.index( index: 'my-index-000001', id: 2, body: { number_two: '10' } ) puts response
const response = await client.indices.create({ index: "my-index-000001", settings: { "index.mapping.coerce": false, }, mappings: { properties: { number_one: { type: "integer", coerce: true, }, number_two: { type: "integer", }, }, }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, document: { number_one: "10", }, }); console.log(response1); const response2 = await client.index({ index: "my-index-000001", id: 2, document: { number_two: "10", }, }); console.log(response2);