Add examples of useful dynamic templates to the docs. #17413

This commit is contained in:
Adrien Grand 2016-03-30 15:56:43 +02:00
parent 0b1b366fe8
commit 26a0fb37a4
1 changed files with 142 additions and 0 deletions

View File

@ -249,6 +249,148 @@ PUT my_index/my_type/1
<1> The `english` field is mapped as a `string` field with the `english` analyzer.
<2> The `count` field is mapped as a `long` field with `doc_values` disabled
[[template-examples]]
==== Template examples
Here are some examples of potentially useful dynamic templates:
===== Structured search
By default elasticsearch will map string fields as a `text` field with a sub
`keyword` field. However if you are only indexing structured content and not
interested in full text search, you can make elasticsearch map your fields
only as `keyword`s. Note that this means that in order to search those fields,
you will have to search on the exact same value that was indexed.
[source,js]
--------------------------------------------------
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
}
--------------------------------------------------
===== `text`-only mappings for strings
On the contrary to the previous example, if the only thing that you care about
on your string fields is full-text search, and if you don't plan on running
aggregations, sorting or exact search on your string fields, you could tell
elasticsearch to map it only as a text field (which was the default behaviour
before 5.0):
[source,js]
--------------------------------------------------
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"strings_as_text": {
"match_mapping_type": "string",
"mapping": {
"type": "text"
}
}
}
]
}
}
}
--------------------------------------------------
===== Disabled norms
Norms are index-time scoring factors. If you do not care about scoring, which
would be the case for instance if you never sort documents by score, you could
disable the storage of these scoring factors in the index and save some space.
[source,js]
--------------------------------------------------
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "text",
"norms": false,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
]
}
}
}
--------------------------------------------------
The sub `keyword` field appears in this template to be consistent with the
default rules of dynamic mappings. Of course if you do not need them because
you don't need to perform exact search or aggregate on this field, you could
remove it as described in the previous section.
===== Time-series
When doing time series analysis with elastisearch, it is common to have many
numeric fields that you will often aggregate on but never filter on. In such a
case, you could disable indexing on those fields to save disk space and also
maybe gain some indexing speed:
[source,js]
--------------------------------------------------
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"unindexed_longs": {
"match_mapping_type": "long",
"mapping": {
"type": "long",
"index": false
}
}
},
{
"unindexed_doubles": {
"match_mapping_type": "double",
"mapping": {
"type": "float", <1>
"index": false
}
}
}
]
}
}
}
--------------------------------------------------
<1> Like the default dynamic mapping rules, doubles are mapped as floats, which
are usually accurate enough, yet require half the disk space.
[[override-default-template]]
=== Override default template