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"
} | Поле | |
| Этот документ будет отклонен, так как приведение типов отключено. |
Значение параметра coerce можно обновить для существующих полей, используя API обновления отображения.
Значение по умолчанию на уровне индекса
Параметр 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); PUT my-index-000001
{
"settings": {
"index.mapping.coerce": false
},
"mappings": {
"properties": {
"number_one": {
"type": "integer",
"coerce": true
},
"number_two": {
"type": "integer"
}
}
}
}
PUT my-index-000001/_doc/1
{ "number_one": "10" }
PUT my-index-000001/_doc/2
{ "number_two": "10" } | Поле | |
| Этот документ будет отклонен, так как поле |
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/coerce.html