Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] Add docs for runtime fields #62653

Merged
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
1eebe84
First steps in docs for runtime fields.
Sep 14, 2020
e1c0427
Adding new page for runtime fields.
Sep 14, 2020
47bb110
Adding page for runtime fields.
Sep 15, 2020
0c97b92
Adding more to the runtime fields topic.
Sep 17, 2020
ee8a16f
Adding parameters and retrieval options for runtime fields.
Sep 18, 2020
f56bde0
Adding TESTSETUP for index creation.
Sep 18, 2020
bb82284
Incorporating review feedback.
Sep 21, 2020
b2cbf0a
Incorporating reviewer feedback.
Sep 22, 2020
d119cbd
Adding examples for runtime fields.
Sep 23, 2020
a287895
Adding more context and simplifying the example.
Sep 24, 2020
ce0fcdd
Merge branch 'master' into docs__add-runtime-fields
elasticmachine Sep 28, 2020
2b533f7
Changing timestamp to @timestamp throughout.
Sep 28, 2020
d2d8551
Removing duplicate @timestamp field.
Sep 28, 2020
282a777
Expanding example to hopefully fix CI builds.
Sep 28, 2020
50a30f6
Adding skip test for result.
Sep 28, 2020
eb4c6c7
Merge branch 'master' into docs__add-runtime-fields
elasticmachine Sep 28, 2020
f8a3d50
Adding missing callout.
Sep 28, 2020
796a10f
Merge branch 'docs__add-runtime-fields' of github.com:lockewritesdocs…
Sep 28, 2020
301abd1
Adding TESTRESPONSEs, which are currently broken.
Sep 28, 2020
62a5393
Fixing TESTRESPONSEs.
Sep 29, 2020
36c9244
Incorporating review feedback.
Sep 29, 2020
804f10f
Several clarifications, better test cases, and other changes.
Sep 30, 2020
875713c
Adding missing callout in example.
Oct 1, 2020
54d529a
Adding substitutions to TESTRESPONSE for shorter results shown.
Oct 1, 2020
7c1a7e0
Shuffling some information and adding link to script-fields.
Oct 26, 2020
d3f3e4b
Fixing typo.
Oct 26, 2020
3fe1541
Updates for API redesign -- will break builds.
Oct 30, 2020
2d158de
Merge branch 'master' into docs__add-runtime-fields
elasticmachine Nov 19, 2020
6d495f0
Updating examples and including info about overriding fields.
Nov 23, 2020
e00f80f
Updating examples.
Nov 24, 2020
c631ce9
Merge branch 'master' into docs__add-runtime-fields
elasticmachine Nov 24, 2020
8824bea
Adding info for using runtime fields in the search request.
Nov 24, 2020
282c261
Merge branch 'docs__add-runtime-fields' of github.com:lockewritesdocs…
Nov 24, 2020
72cf354
Adding that queries against runtime fields are expensive.
Nov 25, 2020
35d7b6a
Incorporating feedback from reviewers.
Nov 30, 2020
4cd5b20
Minor changes from reviews.
Dec 1, 2020
a2be37b
Merge branch 'master' into docs__add-runtime-fields
elasticmachine Dec 1, 2020
99b2720
Adding alias for test case.
Dec 1, 2020
43dd29d
Adding aliases to PUT example.
Dec 1, 2020
994b1c2
Fixing test cases, for real this time.
Dec 1, 2020
38194b2
Updating use cases and introducing overlay throughout.
Dec 1, 2020
70a82e8
Edits, adding 'shadowing', and explaining shadowing better.
Dec 2, 2020
c27ce4c
Streamlining tests and other changes.
Dec 3, 2020
d9a9271
Fix formatting in example for test.
Dec 3, 2020
4d5452d
Apply suggestions from code review
Dec 7, 2020
8175917
Incorporating reviewer feedback 7 Dec
Dec 7, 2020
4e7b0b8
Merge branch 'master' into docs__add-runtime-fields
elasticmachine Dec 8, 2020
b732ae2
Shifting structure of mapping page to fix cross links.
Dec 8, 2020
1ea874f
Merge branch 'docs__add-runtime-fields' of github.com:lockewritesdocs…
Dec 8, 2020
8870aab
Revisions for shadowing, overview, and other sections.
Dec 8, 2020
1475e9d
Removing dot notation section and incorporating review changes.
Dec 9, 2020
06241b7
Adding updated example for shadowing.
Dec 9, 2020
c1b61ee
Streamlining shadowing example and TESTRESPONSEs.
Dec 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 137 additions & 24 deletions docs/reference/mapping/runtime.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ sees runtime fields no differently.
Because runtime fields aren't indexed, adding a runtime field doesn't increase
the index size. You define runtime fields directly in the index mapping, saving
storage costs and increasing ingestion speed. You can more quickly ingest
data into the Elastic Stack and access it right away.

When you define a runtime field, you can immediately use it in search requests,
aggregations, filtering, and sorting. If you later decide that the runtime
field is increasingly used for filtering or aggregations, you can make the
runtime field an indexed field to achieve better query performance.
data into the Elastic Stack and access it right away. When you define a runtime
field, you can immediately use it in search requests, aggregations, filtering,
and sorting.

If you make a runtime field an indexed field, you don't need to modify any
queries that refer to the runtime field. Better yet, you can refer to some
Expand Down Expand Up @@ -193,48 +190,164 @@ script, and returns the value as part of the query. Because the runtime field
shadows the mapped field, you can modify the value returned in search without
modifying the mapped field.

For example, index the following documents into `my-index`:
For example, let's say you indexed the following documents into `my-index`:

[source, console]
[source,console]
----
POST my-index/_bulk?refresh=true
{"index":{}}
{"name":{"first":"Jose","last":"Hickman"}}
{"timestamp":1516729294000,"model_number":"QVKC92Q","measures":{"voltage":5.2}}
{"index":{}}
{"timestamp":1516642894000,"model_number":"PW83H7X","measures":{"voltage":5.8}}
{"index":{}}
{"timestamp":1516556494000,"model_number":"ELF7YV2","measures":{"voltage":5.1}}
{"index":{}}
{"timestamp":1516470094000,"model_number":"5NMDTMF","measures":{"voltage":5.6}}
{"index":{}}
{"name":{"first":"India","last":"Avila","suffix":"II"}}
{"timestamp":1516383694000,"model_number":"1N0TH44","measures":{"voltage":4.2}}
{"index":{}}
{"timestamp":1516297294000,"model_number":"HG537PU","measures":{"voltage":4.0}}
lockewritesdocs marked this conversation as resolved.
Show resolved Hide resolved
----

In the `runtime_mappings` section of the `_search` request, you can define a
script that operates on the `name.first` field. The values you specify will
shadow field values in the index mapping for the duration of the query without
modifying the field itself.
You later realize that the voltage for the sensor matching model number
`HG537PU` is incorrect. The indexed value is `4.0`, but is supposed to be 1.7
lockewritesdocs marked this conversation as resolved.
Show resolved Hide resolved
times higher. Instead of reindexing your data, you can define a script in the
`runtime_mappings` section of the `_search` request to shadow the `voltage`
field and calculate a new value at search time.

The following request defines a runtime field that retrieves values based on
the script valuation. The field defined in the search request shadows the
indexed `name.first` field and substitutes a value for that field based on the
logic defined in the script:
If you search for documents where the model number matches `HG537PU`:

[source,console]
----
GET my-index/_search
{
"query": {
"match": {
"model_number": "HG537PU"
}
}
}
----
//TEST[continued]

The response shows that the voltage is indeed `4.0`:

[source,console-result]
----
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.540445,
"hits" : [
{
"_index" : "my-index",
"_id" : "F1BeSXYBg_szTodcYCmk",
"_score" : 1.540445,
"_source" : {
"timestamp" : 1516297294000,
"model_number" : "HG537PU",
"measures" : {
"voltage" : 4.0
}
}
}
]
}
}
----
// TESTRESPONSE[s/"took" : 5/"took": $body.took/]
// TESTRESPONSE[s/"_id" : "F1BeSXYBg_szTodcYCmk"/"_id": $body.hits.hits.0._id/]

The following request defines a runtime field where the script evaluates the
`model_number` field where the value is `HG537PU`. For each match, the script
multiplies the value for the `voltage` field by `1.7`. The original voltage was
`4.0`, so the resulting voltage should be `6.8` (`4.0*1.7`).

Using the <<search-fields,`fields`>> parameter on the `_search` API, you can
retrieve the value that the script calculates for the `measures.voltage` field
for documents matching the search request:

javanna marked this conversation as resolved.
Show resolved Hide resolved
[source,console]
----
POST my-index/_search
{
"runtime_mappings": {
"name.first": {
"type": "keyword",
"measures.voltage": {
"type": "double",
"script": {
"source": "if (\"Hickman\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Guile\");} else if (\"Avila\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Anastasia\");}"
"source":
"""if (doc['model_number.keyword'].value.equals('HG537PU'))
{emit(1.7 * params._source['measures']['voltage']);}
else{emit(params._source['measures']['voltage']);}"""
}
}
},
"query": {
"match": {
"name.first": "Anastasia"
"model_number": "HG537PU"
}
}
},
"fields": ["measures.voltage"]
}
----
// TEST[continued]
//TEST[continued]

Looking at the response, the value for `measures.voltage` is `6.8`. The runtime
field calculated this value as part of the search request without modifying
the mapped value, which still returns in the response as `4.0`:

[source,console-result]
----
{
"took" : 41,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.540445,
"hits" : [
{
"_index" : "my-index",
"_id" : "F1BeSXYBg_szTodcYCmk",
"_score" : 1.540445,
"_source" : {
"timestamp" : 1516297294000,
"model_number" : "HG537PU",
"measures" : {
"voltage" : 4.0
}
},
"fields" : {
"measures.voltage" : [
6.8
]
}
}
]
}
}
----
// TESTRESPONSE[s/"took" : 41/"took": $body.took/]
// TESTRESPONSE[s/"_id" : "F1BeSXYBg_szTodcYCmk"/"_id": $body.hits.hits.0._id/]

[[runtime-retrieving-fields]]
=== Retrieving a runtime field
Expand Down