Dynamic field mapping
editDynamic field mapping
editWhen Elasticsearch detects a new field in a document, it dynamically adds the field to
the type mapping by default. The dynamic parameter controls this behavior.
You can explicitly instruct Elasticsearch to dynamically create fields based on incoming
documents by setting the dynamic parameter to true or runtime. When
dynamic field mapping is enabled, Elasticsearch uses the rules in the following table to
determine how to map data types for each field.
The field data types in the following table are the only field data types that Elasticsearch detects dynamically. You must explicitly map all other data types.
Elasticsearch data type |
||
JSON data type |
|
|
|
No field added |
No field added |
|
|
|
|
|
|
|
|
|
|
|
No field added |
|
Depends on the first non- |
Depends on the first non- |
|
|
|
|
|
|
|
|
|
You can disable dynamic mapping, both at the document and at the
object level. Setting the dynamic parameter to
false ignores new fields, and strict rejects the document if Elasticsearch
encounters an unknown field.
Use the update mapping API to update the dynamic
setting on existing fields.
You can customize dynamic field mapping rules for
date detection and numeric detection.
To define custom mappings rules that you can apply to additional dynamic
fields, use dynamic_templates.
Date detection
editIf date_detection is enabled (default), then new string fields are checked
to see whether their contents match any of the date patterns specified in
dynamic_date_formats. If a match is found, a new date field is
added with the corresponding format.
The default value for dynamic_date_formats is:
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
For example:
resp = client.index(
index="my-index-000001",
id="1",
document={
"create_date": "2015/09/02"
},
)
print(resp)
resp1 = client.indices.get_mapping(
index="my-index-000001",
)
print(resp1)
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
create_date: '2015/09/02'
}
)
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: {
create_date: "2015/09/02",
},
});
console.log(response);
const response1 = await client.indices.getMapping({
index: "my-index-000001",
});
console.log(response1);
|
The |
Disabling date detection
editDynamic date detection can be disabled by setting date_detection to false:
resp = client.indices.create(
index="my-index-000001",
mappings={
"date_detection": False
},
)
print(resp)
resp1 = client.index(
index="my-index-000001",
id="1",
document={
"create_date": "2015/09/02"
},
)
print(resp1)
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
date_detection: false
}
}
)
puts response
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
create_date: '2015/09/02'
}
)
puts response
const response = await client.indices.create({
index: "my-index-000001",
mappings: {
date_detection: false,
},
});
console.log(response);
const response1 = await client.index({
index: "my-index-000001",
id: 1,
document: {
create_date: "2015/09/02",
},
});
console.log(response1);
PUT my-index-000001
{
"mappings": {
"date_detection": false
}
}
PUT my-index-000001/_doc/1
{
"create_date": "2015/09/02"
}
|
The |
Customizing detected date formats
editAlternatively, the dynamic_date_formats can be customized to support your
own date formats:
resp = client.indices.create(
index="my-index-000001",
mappings={
"dynamic_date_formats": [
"MM/dd/yyyy"
]
},
)
print(resp)
resp1 = client.index(
index="my-index-000001",
id="1",
document={
"create_date": "09/25/2015"
},
)
print(resp1)
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
dynamic_date_formats: [
'MM/dd/yyyy'
]
}
}
)
puts response
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
create_date: '09/25/2015'
}
)
puts response
const response = await client.indices.create({
index: "my-index-000001",
mappings: {
dynamic_date_formats: ["MM/dd/yyyy"],
},
});
console.log(response);
const response1 = await client.index({
index: "my-index-000001",
id: 1,
document: {
create_date: "09/25/2015",
},
});
console.log(response1);
PUT my-index-000001
{
"mappings": {
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
PUT my-index-000001/_doc/1
{
"create_date": "09/25/2015"
}
There is a difference between configuring an array of date patterns and
configuring multiple patterns in a single string separated by ||. When you
configure an array of date patterns, the pattern that matches the date in the
first document with an unmapped date field will determine the mapping of that
field:
resp = client.indices.create(
index="my-index-000001",
mappings={
"dynamic_date_formats": [
"yyyy/MM",
"MM/dd/yyyy"
]
},
)
print(resp)
resp1 = client.index(
index="my-index-000001",
id="1",
document={
"create_date": "09/25/2015"
},
)
print(resp1)
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
dynamic_date_formats: [
'yyyy/MM',
'MM/dd/yyyy'
]
}
}
)
puts response
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
create_date: '09/25/2015'
}
)
puts response
const response = await client.indices.create({
index: "my-index-000001",
mappings: {
dynamic_date_formats: ["yyyy/MM", "MM/dd/yyyy"],
},
});
console.log(response);
const response1 = await client.index({
index: "my-index-000001",
id: 1,
document: {
create_date: "09/25/2015",
},
});
console.log(response1);
PUT my-index-000001
{
"mappings": {
"dynamic_date_formats": [ "yyyy/MM", "MM/dd/yyyy"]
}
}
PUT my-index-000001/_doc/1
{
"create_date": "09/25/2015"
}
The resulting mapping will be:
{
"my-index-000001": {
"mappings": {
"dynamic_date_formats": [
"yyyy/MM",
"MM/dd/yyyy"
],
"properties": {
"create_date": {
"type": "date",
"format": "MM/dd/yyyy"
}
}
}
}
}
Configuring multiple patterns in a single string separated by || results in a
mapping that supports any of the date formats. This enables you to index
documents that use different formats:
resp = client.indices.create(
index="my-index-000001",
mappings={
"dynamic_date_formats": [
"yyyy/MM||MM/dd/yyyy"
]
},
)
print(resp)
resp1 = client.index(
index="my-index-000001",
id="1",
document={
"create_date": "09/25/2015"
},
)
print(resp1)
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
dynamic_date_formats: [
'yyyy/MM||MM/dd/yyyy'
]
}
}
)
puts response
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
create_date: '09/25/2015'
}
)
puts response
const response = await client.indices.create({
index: "my-index-000001",
mappings: {
dynamic_date_formats: ["yyyy/MM||MM/dd/yyyy"],
},
});
console.log(response);
const response1 = await client.index({
index: "my-index-000001",
id: 1,
document: {
create_date: "09/25/2015",
},
});
console.log(response1);
PUT my-index-000001
{
"mappings": {
"dynamic_date_formats": [ "yyyy/MM||MM/dd/yyyy"]
}
}
PUT my-index-000001/_doc/1
{
"create_date": "09/25/2015"
}
The resulting mapping will be:
{
"my-index-000001": {
"mappings": {
"dynamic_date_formats": [
"yyyy/MM||MM/dd/yyyy"
],
"properties": {
"create_date": {
"type": "date",
"format": "yyyy/MM||MM/dd/yyyy"
}
}
}
}
}
Epoch formats (epoch_millis and epoch_second) are not supported as dynamic date formats.
Numeric detection
editWhile JSON has support for native floating point and integer data types, some applications or languages may sometimes render numbers as strings. Usually the correct solution is to map these fields explicitly, but numeric detection (which is disabled by default) can be enabled to do this automatically:
resp = client.indices.create(
index="my-index-000001",
mappings={
"numeric_detection": True
},
)
print(resp)
resp1 = client.index(
index="my-index-000001",
id="1",
document={
"my_float": "1.0",
"my_integer": "1"
},
)
print(resp1)
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
numeric_detection: true
}
}
)
puts response
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
my_float: '1.0',
my_integer: '1'
}
)
puts response
const response = await client.indices.create({
index: "my-index-000001",
mappings: {
numeric_detection: true,
},
});
console.log(response);
const response1 = await client.index({
index: "my-index-000001",
id: 1,
document: {
my_float: "1.0",
my_integer: "1",
},
});
console.log(response1);