Skip to content

Commit 07fade1

Browse files
authored
[DOCS] EQL/SQL: Document runtime_fields parameter (#71487)
1 parent 749120e commit 07fade1

File tree

4 files changed

+136
-10
lines changed

4 files changed

+136
-10
lines changed

docs/reference/eql/eql-search-api.asciidoc

+2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ command].
294294
NOTE: This parameter may change the set of returned hits. However, it does not
295295
change the sort order of hits in the response.
296296

297+
include::{es-repo-dir}/search/search.asciidoc[tag=runtime-mappings-def]
298+
297299
`size`::
298300
(Optional, integer or float)
299301
For <<eql-basic-syntax,basic queries>>, the maximum number of matching events to

docs/reference/eql/eql.asciidoc

+70-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ GET /my-data-stream/_eql/search?filter_path=-hits.events._source
442442
"process.*", <1>
443443
{
444444
"field": "@timestamp", <2>
445-
"format": "epoch_millis"
445+
"format": "epoch_millis"
446446
}
447447
]
448448
}
@@ -532,6 +532,75 @@ The values are returned as a flat list in the `fields` section of each hit:
532532
// TESTRESPONSE[s/"_id": "OQmfCaduce8zoHT93o4H"/"_id": $body.hits.events.0._id/]
533533
// TESTRESPONSE[s/"_id": "xLkCaj4EujzdNSxfYLbO"/"_id": $body.hits.events.1._id/]
534534

535+
[discrete]
536+
[[eql-use-runtime-fields]]
537+
=== Use runtime fields
538+
539+
Use the `runtime_mappings` parameter to extract and create <<runtime,runtime
540+
fields>> during a search. Use the `fields` parameter to include runtime fields
541+
in the response.
542+
543+
The following search creates a `day_of_week` runtime field from the `@timestamp`
544+
and returns it in the response.
545+
546+
[source,console]
547+
----
548+
GET /my-data-stream/_eql/search?filter_path=-hits.events._source
549+
{
550+
"runtime_mappings": {
551+
"day_of_week": {
552+
"type": "keyword",
553+
"script": "emit(doc['@timestamp'].value.dayOfWeekEnum.toString())"
554+
}
555+
},
556+
"query": """
557+
process where process.name == "regsvr32.exe"
558+
""",
559+
"fields": [
560+
"@timestamp",
561+
"day_of_week"
562+
]
563+
}
564+
----
565+
// TEST[setup:sec_logs]
566+
567+
The API returns:
568+
569+
[source,console-result]
570+
----
571+
{
572+
"is_partial": false,
573+
"is_running": false,
574+
"took": 60,
575+
"timed_out": false,
576+
"hits": {
577+
"total": {
578+
"value": 2,
579+
"relation": "eq"
580+
},
581+
"events": [
582+
{
583+
"_index": ".ds-my-data-stream-2099.12.07-000001",
584+
"_id": "OQmfCaduce8zoHT93o4H",
585+
"fields": {
586+
"@timestamp": [
587+
"2099-12-07T11:07:09.000Z"
588+
],
589+
"day_of_week": [
590+
"MONDAY"
591+
]
592+
}
593+
},
594+
...
595+
]
596+
}
597+
}
598+
----
599+
// TESTRESPONSE[s/"took": 60/"took": $body.took/]
600+
// TESTRESPONSE[s/"_index": ".ds-my-data-stream-2099.12.07-000001"/"_index": $body.hits.events.0._index/]
601+
// TESTRESPONSE[s/"_id": "OQmfCaduce8zoHT93o4H"/"_id": $body.hits.events.0._id/]
602+
// TESTRESPONSE[s/\.\.\./$body.hits.events.1/]
603+
535604
[discrete]
536605
[[specify-a-timestamp-or-event-category-field]]
537606
=== Specify a timestamp or event category field

docs/reference/search/search.asciidoc

+19-8
Original file line numberDiff line numberDiff line change
@@ -428,36 +428,47 @@ Period of time used to extend the life of the PIT.
428428
<<query-dsl,Query DSL>>.
429429

430430
[[search-api-body-runtime]]
431+
// tag::runtime-mappings-def[]
431432
`runtime_mappings`::
432-
(Optional, object)
433-
Defines a <<runtime-search-request,runtime field>> in the search request that
434-
exist only as part of the query. Fields defined in the search request take
435-
precedence over fields defined with the same name in the index mappings.
433+
(Optional, object of objects)
434+
Defines one or more <<runtime-search-request,runtime fields>> in the search
435+
request. These fields take precedence over mapped fields with the same name.
436436
+
437437
.Properties of `runtime_mappings` objects
438438
[%collapsible%open]
439439
====
440+
441+
`<field-name>`::
442+
(Required, object)
443+
Configuration for the runtime field. The key is the field name.
444+
+
445+
.Properties of `<field-name>`
446+
[%collapsible%open]
447+
=====
448+
440449
`type`::
441450
(Required, string)
442-
<<mapping-types,Data type>> of the object, which can be any of the following:
451+
<<mapping-types,Field type>>, which can be any of the following:
443452
+
444453
include::{es-ref-dir}/mapping/runtime.asciidoc[tag=runtime-data-types]
445454

446455
`script`::
447456
(Optional, string)
448-
<<modules-scripting-using,Painless script>> that is executed at query time. The
457+
<<modules-scripting-using,Painless script>> executed at query time. The
449458
script has access to the entire context of a document, including the original
450459
`_source` and any mapped fields plus their values.
451460
+
452-
Your script must include `emit` to return calculated values. For
453-
example:
461+
This script must include `emit` to return calculated values. For example:
454462
+
455463
[source,js,indent=0]
456464
----
457465
include::search.asciidoc[tag=runtime-script]
458466
----
459467
// NOTCONSOLE
468+
469+
=====
460470
====
471+
// end::runtime-mappings-def[]
461472

462473
////
463474
[source,console]

docs/reference/sql/endpoints/rest.asciidoc

+45-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* <<sql-rest-filtering>>
1010
* <<sql-rest-columnar>>
1111
* <<sql-rest-params>>
12+
* <<sql-runtime-fields>>
1213
* <<sql-rest-fields>>
1314

1415
[[sql-rest-overview]]
@@ -522,6 +523,44 @@ POST /_sql?format=txt
522523
[IMPORTANT]
523524
The recommended way of passing values to a query is with question mark placeholders, to avoid any attempts of hacking or SQL injection.
524525

526+
[[sql-runtime-fields]]
527+
=== Use runtime fields
528+
529+
Use the `runtime_mappings` parameter to extract and create <<runtime,runtime
530+
fields>>, or columns, from existing ones during a search.
531+
532+
The following search creates a `release_day_of_week` runtime field from
533+
`release_date` and returns it in the response.
534+
535+
[source,console]
536+
----
537+
POST _sql?format=txt
538+
{
539+
"runtime_mappings": {
540+
"release_day_of_week": {
541+
"type": "keyword",
542+
"script": """
543+
emit(doc['release_date'].value.dayOfWeekEnum.toString())
544+
"""
545+
}
546+
},
547+
"query": """
548+
SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'
549+
"""
550+
}
551+
----
552+
// TEST[setup:library]
553+
554+
The API returns:
555+
556+
[source,txt]
557+
----
558+
author | name | page_count | release_date |release_day_of_week
559+
---------------+---------------+---------------+------------------------+-------------------
560+
Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z|TUESDAY
561+
----
562+
// TESTRESPONSE[non_json]
563+
525564
[[sql-rest-fields]]
526565
=== Supported REST parameters
527566

@@ -530,7 +569,7 @@ the request time-outs or localization information (such as timezone).
530569

531570
The table below lists the supported parameters:
532571

533-
[cols="^m,^m,^5"]
572+
[cols="<m,<m,<5"]
534573

535574
|===
536575

@@ -579,6 +618,11 @@ More information available https://docs.oracle.com/javase/8/docs/api/java/time/Z
579618
|none
580619
|Optional list of parameters to replace question mark (`?`) placeholders inside the query.
581620

621+
|runtime_mappings
622+
|none
623+
|Defines one or more <<runtime-search-request,runtime fields>> in the search
624+
request. These fields take precedence over mapped fields with the same name.
625+
582626
|===
583627

584628
Do note that most parameters (outside the timeout and `columnar` ones) make sense only during the initial query - any follow-up pagination request only requires the `cursor` parameter as explained in the <<sql-pagination, pagination>> chapter.

0 commit comments

Comments
 (0)