runtime.asciidoc 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. [[runtime]]
  2. == Runtime fields
  3. A _runtime field_ is a field that is evaluated at query time. Runtime fields
  4. enable you to:
  5. * Add fields to existing documents without reindexing your data
  6. * Start working with your data without understanding how it’s structured
  7. * Override the value returned from an indexed field at query time
  8. * Define fields for a specific use without modifying the underlying schema
  9. You access runtime fields from the search API like any other field, and {es}
  10. sees runtime fields no differently. You can define runtime fields in the
  11. <<runtime-mapping-fields,index mapping>> or in the
  12. <<runtime-search-request,search request>>. Your choice, which is part of the
  13. inherent flexibility of runtime fields.
  14. Runtime fields are useful when working with log data
  15. (see <<runtime-examples,examples>>), especially when you're unsure about the
  16. data structure. Your search speed decreases, but your index size is much
  17. smaller and you can more quickly process logs without having to index them.
  18. [discrete]
  19. [[runtime-benefits]]
  20. === Benefits
  21. Because runtime fields aren't indexed, adding a runtime field doesn't increase
  22. the index size. You define runtime fields directly in the index mapping, saving
  23. storage costs and increasing ingestion speed. You can more quickly ingest
  24. data into the Elastic Stack and access it right away. When you define a runtime
  25. field, you can immediately use it in search requests, aggregations, filtering,
  26. and sorting.
  27. If you make a runtime field an indexed field, you don't need to modify any
  28. queries that refer to the runtime field. Better yet, you can refer to some
  29. indices where the field is a runtime field, and other indices where the field
  30. is an indexed field. You have the flexibility to choose which fields to index
  31. and which ones to keep as runtime fields.
  32. At its core, the most important benefit of runtime fields is the ability to
  33. add fields to documents after you've ingested them. This capability simplifies
  34. mapping decisions because you don't have to decide how to parse your data up
  35. front, and can use runtime fields to amend the mapping at any time. Using
  36. runtime fields allows for a smaller index and faster ingest time, which
  37. combined use less resources and reduce your operating costs.
  38. [discrete]
  39. [[runtime-compromises]]
  40. === Compromises
  41. Runtime fields use less disk space and provide flexibility in how you access
  42. your data, but can impact search performance based on the computation defined in
  43. the runtime script.
  44. To balance search performance and flexibility, index fields that you'll
  45. commonly search for and filter on, such as a timestamp. {es} automatically uses
  46. these indexed fields first when running a query, resulting in a fast response
  47. time. You can then use runtime fields to limit the number of fields that {es}
  48. needs to calculate values for. Using indexed fields in tandem with runtime
  49. fields provides flexibility in the data that you index and how you define
  50. queries for other fields.
  51. Use the <<async-search,asynchronous search API>> to run searches that include
  52. runtime fields. This method of search helps to offset the performance impacts
  53. of computing values for runtime fields in each document containing that field.
  54. If the query can't return the result set synchronously, you'll get results
  55. asynchronously as they become available.
  56. IMPORTANT: Queries against runtime fields are considered expensive. If
  57. <<query-dsl-allow-expensive-queries,`search.allow_expensive_queries`>> is set
  58. to `false`, expensive queries are not allowed and {es} will reject any queries
  59. against runtime fields.
  60. [[runtime-mapping-fields]]
  61. === Map a runtime field
  62. You map runtime fields by adding a `runtime` section under the mapping
  63. definition and defining
  64. <<modules-scripting-using,a Painless script>>. This script has access to the
  65. entire context of a document, including the original `_source` and any mapped
  66. fields plus their values. At query time, the script runs and generates values
  67. for each scripted field that is required for the query.
  68. .Emitting runtime field values
  69. ****
  70. When defining a Painless script to use with runtime fields, you must include
  71. the {painless}/painless-runtime-fields-context.html[`emit` method] to emit
  72. calculated values.
  73. ****
  74. For example, the script in the following request calculates the day of the week
  75. from the `@timestamp` field, which is defined as a `date` type. The script
  76. calculates the day of the week based on the value of `timestamp`, and uses
  77. `emit` to return the calculated value.
  78. [source,console]
  79. ----
  80. PUT my-index/
  81. {
  82. "mappings": {
  83. "runtime": {
  84. "day_of_week": {
  85. "type": "keyword",
  86. "script": {
  87. "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
  88. }
  89. }
  90. },
  91. "properties": {
  92. "@timestamp": {"type": "date"}
  93. }
  94. }
  95. }
  96. ----
  97. The `runtime` section can be any of these data types:
  98. // tag::runtime-data-types[]
  99. * `boolean`
  100. * `date`
  101. * `double`
  102. * `geo_point`
  103. * `ip`
  104. * `keyword`
  105. * `long`
  106. // end::runtime-data-types[]
  107. Runtime fields with a `type` of `date` can accept the
  108. <<mapping-date-format,`format`>> parameter exactly as the `date` field type.
  109. If <<dynamic-field-mapping,dynamic field mapping>> is enabled where the
  110. `dynamic` parameter is set to `runtime`, new fields are automatically added to
  111. the index mapping as runtime fields:
  112. [source,console]
  113. ----
  114. PUT my-index
  115. {
  116. "mappings": {
  117. "dynamic": "runtime",
  118. "properties": {
  119. "@timestamp": {
  120. "type": "date"
  121. }
  122. }
  123. }
  124. }
  125. ----
  126. [[runtime-fields-scriptless]]
  127. ==== Define runtime fields without a script
  128. You can define a runtime field in the mapping definition without a
  129. script. At query time, {es} looks in `_source` for a field with the same name
  130. and returns a value if one exists. If a field with the same name doesn’t
  131. exist, the response doesn't include any values for that runtime field.
  132. [source,console]
  133. ----
  134. PUT my-index/
  135. {
  136. "mappings": {
  137. "runtime": {
  138. "day_of_week": {
  139. "type": "keyword"
  140. }
  141. }
  142. }
  143. }
  144. ----
  145. [[runtime-updating-scripts]]
  146. ==== Updating and removing runtime fields
  147. You can update or remove runtime fields at any time. To replace an existing
  148. runtime field, add a new runtime field to the mappings with the same name. To
  149. remove a runtime field from the mappings, set the value of the runtime field to
  150. `null`:
  151. [source,console]
  152. ----
  153. PUT my-index/_mapping
  154. {
  155. "runtime": {
  156. "day_of_week": null
  157. }
  158. }
  159. ----
  160. //TEST[continued]
  161. .Downstream impacts
  162. ****
  163. Updating or removing a runtime field while a dependent query is running can return
  164. inconsistent results. Each shard might have access to different versions of the
  165. script, depending on when the mapping change takes effect.
  166. WARNING: Existing queries or visualizations in {kib} that rely on runtime fields can
  167. fail if you remove or update the field. For example, a bar chart visualization
  168. that uses a runtime field of type `ip` will fail if the type is changed
  169. to `boolean`, or if the runtime field is removed.
  170. ****
  171. [[runtime-search-request]]
  172. === Define runtime fields in a search request
  173. You can specify a `runtime_mappings` section in a search request to create
  174. runtime fields that exist only as part of the query. You specify a script
  175. as part of the `runtime_mappings` section, just as you would if
  176. <<runtime-mapping-fields,adding a runtime field to the mappings>>.
  177. Defining a runtime field in a search request uses the same format as defining
  178. a runtime field in the index mapping. Just copy the field definition from
  179. the `runtime_mappings` in the search request to the `runtime` section of the
  180. index mapping.
  181. The following search request adds a `day_of_week` field to the
  182. `runtime_mappings` section. The field values will be calculated dynamically,
  183. and only within the context of this search request:
  184. [source,console]
  185. ----
  186. GET my-index/_search
  187. {
  188. "runtime_mappings": {
  189. "day_of_week": {
  190. "type": "keyword",
  191. "script": {
  192. "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
  193. }
  194. }
  195. },
  196. "aggs": {
  197. "day_of_week": {
  198. "terms": {
  199. "field": "day_of_week"
  200. }
  201. }
  202. }
  203. }
  204. ----
  205. //TEST[continued]
  206. [[runtime-search-request-examples]]
  207. [discrete]
  208. === Create runtime fields that use other runtime fields
  209. You can even define runtime fields in a search request that return values from
  210. other runtime fields. For example, let's say you bulk index some sensor data:
  211. [source,console]
  212. ----
  213. POST my-index/_bulk?refresh=true
  214. {"index":{}}
  215. {"@timestamp":1516729294000,"model_number":"QVKC92Q","measures":{"voltage":"5.2","start": "300","end":"8675309"}}
  216. {"index":{}}
  217. {"@timestamp":1516642894000,"model_number":"QVKC92Q","measures":{"voltage":"5.8","start": "300","end":"8675309"}}
  218. {"index":{}}
  219. {"@timestamp":1516556494000,"model_number":"QVKC92Q","measures":{"voltage":"5.1","start": "300","end":"8675309"}}
  220. {"index":{}}
  221. {"@timestamp":1516470094000,"model_number":"QVKC92Q","measures":{"voltage":"5.6","start": "300","end":"8675309"}}
  222. {"index":{}}
  223. {"@timestamp":1516383694000,"model_number":"HG537PU","measures":{"voltage":"4.2","start": "400","end":"8625309"}}
  224. {"index":{}}
  225. {"@timestamp":1516297294000,"model_number":"HG537PU","measures":{"voltage":"4.0","start": "400","end":"8625309"}}
  226. ----
  227. You realize after indexing that your numeric data was mapped as type `text`.
  228. You want to aggregate on the `measures.start` and `measures.end` fields, but
  229. the aggregation fails because you can't aggregate on fields of type `text`.
  230. Runtime fields to the rescue! You can add runtime fields with the same name as
  231. your indexed fields and modify the data type:
  232. [source,console]
  233. ----
  234. PUT my-index/_mapping
  235. {
  236. "runtime": {
  237. "measures.start": {
  238. "type": "long"
  239. },
  240. "measures.end": {
  241. "type": "long"
  242. }
  243. }
  244. }
  245. ----
  246. // TEST[continued]
  247. Runtime fields take precedence over fields defined with the same name in the
  248. index mappings. This flexibility allows you to shadow existing fields and
  249. calculate a different value, without modifying the field itself. If you made a
  250. mistake in your index mapping, you can use runtime fields to calculate values
  251. that <<runtime-override-values,override values>> in the mapping during the
  252. search request.
  253. Now, you can easily run an
  254. <<search-aggregations-metrics-avg-aggregation,average aggregation>> on the
  255. `measures.start` and `measures.end` fields:
  256. [source,console]
  257. ----
  258. GET my-index/_search
  259. {
  260. "aggs": {
  261. "avg_start": {
  262. "avg": {
  263. "field": "measures.start"
  264. }
  265. },
  266. "avg_end": {
  267. "avg": {
  268. "field": "measures.end"
  269. }
  270. }
  271. }
  272. }
  273. ----
  274. // TEST[continued]
  275. // TEST[s/_search/_search\?filter_path=aggregations/]
  276. The response includes the aggregation results without changing the values for
  277. the underlying data:
  278. [source,console-result]
  279. ----
  280. {
  281. "aggregations" : {
  282. "avg_start" : {
  283. "value" : 333.3333333333333
  284. },
  285. "avg_end" : {
  286. "value" : 8658642.333333334
  287. }
  288. }
  289. }
  290. ----
  291. Further, you can define a runtime field as part of a search query that
  292. calculates a value, and then run a
  293. <<search-aggregations-metrics-stats-aggregation,stats aggregation>> on that
  294. field _in the same query_.
  295. The `duration` runtime field doesn't exist in the index mapping, but we can
  296. still search and aggregate on that field. The following query returns the
  297. calculated value for the `duration` field and runs a stats aggregation to
  298. compute statistics over numeric values extracted from the aggregated documents.
  299. [source,console]
  300. ----
  301. GET my-index/_search
  302. {
  303. "runtime_mappings": {
  304. "duration": {
  305. "type": "long",
  306. "script": {
  307. "source": """
  308. emit(doc['measures.end'].value - doc['measures.start'].value);
  309. """
  310. }
  311. }
  312. },
  313. "aggs": {
  314. "duration_stats": {
  315. "stats": {
  316. "field": "duration"
  317. }
  318. }
  319. }
  320. }
  321. ----
  322. // TEST[continued]
  323. // TEST[s/_search/_search\?filter_path=aggregations/]
  324. Even though the `duration` runtime field only exists in the context of a search
  325. query, you can search and aggregate on that field. This flexibility is
  326. incredibly powerful, enabling you to rectify mistakes in your index mappings
  327. and dynamically complete calculations all within a single search request.
  328. [source,console-result]
  329. ----
  330. {
  331. "aggregations" : {
  332. "duration_stats" : {
  333. "count" : 6,
  334. "min" : 8624909.0,
  335. "max" : 8675009.0,
  336. "avg" : 8658309.0,
  337. "sum" : 5.1949854E7
  338. }
  339. }
  340. }
  341. ----
  342. [[runtime-override-values]]
  343. === Override field values at query time
  344. If you create a runtime field with the same name as a field that
  345. already exists in the mapping, the runtime field shadows the mapped field. At
  346. query time, {es} evaluates the runtime field, calculates a value based on the
  347. script, and returns the value as part of the query. Because the runtime field
  348. shadows the mapped field, you can override the value returned in search without
  349. modifying the mapped field.
  350. For example, let's say you indexed the following documents into `my-index`:
  351. [source,console]
  352. ----
  353. POST my-index/_bulk?refresh=true
  354. {"index":{}}
  355. {"@timestamp":1516729294000,"model_number":"QVKC92Q","measures":{"voltage":5.2}}
  356. {"index":{}}
  357. {"@timestamp":1516642894000,"model_number":"QVKC92Q","measures":{"voltage":5.8}}
  358. {"index":{}}
  359. {"@timestamp":1516556494000,"model_number":"QVKC92Q","measures":{"voltage":5.1}}
  360. {"index":{}}
  361. {"@timestamp":1516470094000,"model_number":"QVKC92Q","measures":{"voltage":5.6}}
  362. {"index":{}}
  363. {"@timestamp":1516383694000,"model_number":"HG537PU","measures":{"voltage":4.2}}
  364. {"index":{}}
  365. {"@timestamp":1516297294000,"model_number":"HG537PU","measures":{"voltage":4.0}}
  366. ----
  367. You later realize that the `HG537PU` sensors aren't reporting their true
  368. voltage. The indexed values are supposed to be 1.7 times higher than
  369. the reported values! Instead of reindexing your data, you can define a script in
  370. the `runtime_mappings` section of the `_search` request to shadow the `voltage`
  371. field and calculate a new value at query time.
  372. If you search for documents where the model number matches `HG537PU`:
  373. [source,console]
  374. ----
  375. GET my-index/_search
  376. {
  377. "query": {
  378. "match": {
  379. "model_number": "HG537PU"
  380. }
  381. }
  382. }
  383. ----
  384. //TEST[continued]
  385. The response includes indexed values for documents matching model number
  386. `HG537PU`:
  387. [source,console-result]
  388. ----
  389. {
  390. ...
  391. "hits" : {
  392. "total" : {
  393. "value" : 2,
  394. "relation" : "eq"
  395. },
  396. "max_score" : 1.0296195,
  397. "hits" : [
  398. {
  399. "_index" : "my-index",
  400. "_id" : "F1BeSXYBg_szTodcYCmk",
  401. "_score" : 1.0296195,
  402. "_source" : {
  403. "@timestamp" : 1516383694000,
  404. "model_number" : "HG537PU",
  405. "measures" : {
  406. "voltage" : 4.2
  407. }
  408. }
  409. },
  410. {
  411. "_index" : "my-index",
  412. "_id" : "l02aSXYBkpNf6QRDO62Q",
  413. "_score" : 1.0296195,
  414. "_source" : {
  415. "@timestamp" : 1516297294000,
  416. "model_number" : "HG537PU",
  417. "measures" : {
  418. "voltage" : 4.0
  419. }
  420. }
  421. }
  422. ]
  423. }
  424. }
  425. ----
  426. // TESTRESPONSE[s/\.\.\./"took" : $body.took,"timed_out" : $body.timed_out,"_shards" : $body._shards,/]
  427. // TESTRESPONSE[s/"_id" : "F1BeSXYBg_szTodcYCmk"/"_id": $body.hits.hits.0._id/]
  428. // TESTRESPONSE[s/"_id" : "l02aSXYBkpNf6QRDO62Q"/"_id": $body.hits.hits.1._id/]
  429. The following request defines a runtime field where the script evaluates the
  430. `model_number` field where the value is `HG537PU`. For each match, the script
  431. multiplies the value for the `voltage` field by `1.7`.
  432. Using the <<search-fields,`fields`>> parameter on the `_search` API, you can
  433. retrieve the value that the script calculates for the `measures.voltage` field
  434. for documents matching the search request:
  435. [source,console]
  436. ----
  437. POST my-index/_search
  438. {
  439. "runtime_mappings": {
  440. "measures.voltage": {
  441. "type": "double",
  442. "script": {
  443. "source":
  444. """if (doc['model_number.keyword'].value.equals('HG537PU'))
  445. {emit(1.7 * params._source['measures']['voltage']);}
  446. else{emit(params._source['measures']['voltage']);}"""
  447. }
  448. }
  449. },
  450. "query": {
  451. "match": {
  452. "model_number": "HG537PU"
  453. }
  454. },
  455. "fields": ["measures.voltage"]
  456. }
  457. ----
  458. //TEST[continued]
  459. Looking at the response, the calculated values for `measures.voltage` on each
  460. result are `7.14` and `6.8`. That's more like it! The runtime field calculated
  461. this value as part of the search request without modifying the mapped value,
  462. which still returns in the response:
  463. [source,console-result]
  464. ----
  465. {
  466. ...
  467. "hits" : {
  468. "total" : {
  469. "value" : 2,
  470. "relation" : "eq"
  471. },
  472. "max_score" : 1.0296195,
  473. "hits" : [
  474. {
  475. "_index" : "my-index",
  476. "_id" : "F1BeSXYBg_szTodcYCmk",
  477. "_score" : 1.0296195,
  478. "_source" : {
  479. "@timestamp" : 1516383694000,
  480. "model_number" : "HG537PU",
  481. "measures" : {
  482. "voltage" : 4.2
  483. }
  484. },
  485. "fields" : {
  486. "measures.voltage" : [
  487. 7.14
  488. ]
  489. }
  490. },
  491. {
  492. "_index" : "my-index",
  493. "_id" : "l02aSXYBkpNf6QRDO62Q",
  494. "_score" : 1.0296195,
  495. "_source" : {
  496. "@timestamp" : 1516297294000,
  497. "model_number" : "HG537PU",
  498. "measures" : {
  499. "voltage" : 4.0
  500. }
  501. },
  502. "fields" : {
  503. "measures.voltage" : [
  504. 6.8
  505. ]
  506. }
  507. }
  508. ]
  509. }
  510. }
  511. ----
  512. // TESTRESPONSE[s/\.\.\./"took" : $body.took,"timed_out" : $body.timed_out,"_shards" : $body._shards,/]
  513. // TESTRESPONSE[s/"_id" : "F1BeSXYBg_szTodcYCmk"/"_id": $body.hits.hits.0._id/]
  514. // TESTRESPONSE[s/"_id" : "l02aSXYBkpNf6QRDO62Q"/"_id": $body.hits.hits.1._id/]
  515. [[runtime-retrieving-fields]]
  516. === Retrieve a runtime field
  517. Use the <<search-fields,`fields`>> parameter on the `_search` API to retrieve
  518. the values of runtime fields. Runtime fields won't display in `_source`, but
  519. the `fields` API works for all fields, even those that were not sent as part of
  520. the original `_source`.
  521. [discrete]
  522. [[runtime-define-field-dayofweek]]
  523. ==== Define a runtime field to calculate the day of week
  524. For example, the following request adds a runtime field called `day_of_week`.
  525. The runtime field includes a script that calculates the day of the week based
  526. on the value of the `@timestamp` field. We'll include `"dynamic":"runtime"` in
  527. the request so that new fields are added to the mapping as runtime fields.
  528. [source,console]
  529. ----
  530. PUT my-index/
  531. {
  532. "mappings": {
  533. "dynamic": "runtime",
  534. "runtime": {
  535. "day_of_week": {
  536. "type": "keyword",
  537. "script": {
  538. "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
  539. }
  540. }
  541. },
  542. "properties": {
  543. "@timestamp": {"type": "date"}
  544. }
  545. }
  546. }
  547. ----
  548. [discrete]
  549. [[runtime-ingest-data]]
  550. ==== Ingest some data
  551. Let's ingest some sample data, which will result in two indexed fields:
  552. `@timestamp` and `message`.
  553. [source,console]
  554. ----
  555. POST /my-index/_bulk?refresh
  556. { "index": {}}
  557. { "@timestamp": "2020-06-21T15:00:01-05:00", "message" : "211.11.9.0 - - [2020-06-21T15:00:01-05:00] \"GET /english/index.html HTTP/1.0\" 304 0"}
  558. { "index": {}}
  559. { "@timestamp": "2020-06-21T15:00:01-05:00", "message" : "211.11.9.0 - - [2020-06-21T15:00:01-05:00] \"GET /english/index.html HTTP/1.0\" 304 0"}
  560. { "index": {}}
  561. { "@timestamp": "2020-04-30T14:30:17-05:00", "message" : "40.135.0.0 - - [2020-04-30T14:30:17-05:00] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  562. { "index": {}}
  563. { "@timestamp": "2020-04-30T14:30:53-05:00", "message" : "232.0.0.0 - - [2020-04-30T14:30:53-05:00] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  564. { "index": {}}
  565. { "@timestamp": "2020-04-30T14:31:12-05:00", "message" : "26.1.0.0 - - [2020-04-30T14:31:12-05:00] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  566. { "index": {}}
  567. { "@timestamp": "2020-04-30T14:31:19-05:00", "message" : "247.37.0.0 - - [2020-04-30T14:31:19-05:00] \"GET /french/splash_inet.html HTTP/1.0\" 200 3781"}
  568. { "index": {}}
  569. { "@timestamp": "2020-04-30T14:31:27-05:00", "message" : "252.0.0.0 - - [2020-04-30T14:31:27-05:00] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  570. { "index": {}}
  571. { "@timestamp": "2020-04-30T14:31:29-05:00", "message" : "247.37.0.0 - - [2020-04-30T14:31:29-05:00] \"GET /images/hm_brdl.gif HTTP/1.0\" 304 0"}
  572. { "index": {}}
  573. { "@timestamp": "2020-04-30T14:31:29-05:00", "message" : "247.37.0.0 - - [2020-04-30T14:31:29-05:00] \"GET /images/hm_arw.gif HTTP/1.0\" 304 0"}
  574. { "index": {}}
  575. { "@timestamp": "2020-04-30T14:31:32-05:00", "message" : "247.37.0.0 - - [2020-04-30T14:31:32-05:00] \"GET /images/nav_bg_top.gif HTTP/1.0\" 200 929"}
  576. { "index": {}}
  577. { "@timestamp": "2020-04-30T14:31:43-05:00", "message" : "247.37.0.0 - - [2020-04-30T14:31:43-05:00] \"GET /french/images/nav_venue_off.gif HTTP/1.0\" 304 0"}
  578. ----
  579. //TEST[continued]
  580. [discrete]
  581. [[runtime-search-dayofweek]]
  582. ==== Search for the calculated day of week
  583. The following request uses the search API to retrieve the `day_of_week` field
  584. that the original request defined as a runtime field in the mapping. The value
  585. for this field is calculated dynamically at query time without reindexing
  586. documents or indexing the `day_of_week` field. This flexibility allows you to
  587. modify the mapping without changing any field values.
  588. [source,console]
  589. ----
  590. GET my-index/_search
  591. {
  592. "fields": [
  593. "@timestamp",
  594. "day_of_week"
  595. ],
  596. "_source": false
  597. }
  598. ----
  599. // TEST[continued]
  600. The previous request returns the `day_of_week` field for all matching documents.
  601. We can define another runtime field called `client_ip` that also operates on
  602. the `message` field and will further refine the query:
  603. [source,console]
  604. ----
  605. PUT /my-index/_mapping
  606. {
  607. "runtime": {
  608. "client_ip": {
  609. "type": "ip",
  610. "script" : {
  611. "source" : "String m = doc[\"message\"].value; int end = m.indexOf(\" \"); emit(m.substring(0, end));"
  612. }
  613. }
  614. }
  615. }
  616. ----
  617. //TEST[continued]
  618. Run another query, but search for a specific IP address using the `client_ip`
  619. runtime field:
  620. [source,console]
  621. ----
  622. GET my-index/_search
  623. {
  624. "size": 1,
  625. "query": {
  626. "match": {
  627. "client_ip": "211.11.9.0"
  628. }
  629. },
  630. "fields" : ["*"]
  631. }
  632. ----
  633. //TEST[continued]
  634. This time, the response includes only two hits. The value for `day_of_week`
  635. (`Sunday`) was calculated at query time using the runtime script defined in the
  636. mapping, and the result includes only documents matching the `211.11.9.0` IP
  637. address.
  638. [source,console-result]
  639. ----
  640. {
  641. ...
  642. "hits" : {
  643. "total" : {
  644. "value" : 2,
  645. "relation" : "eq"
  646. },
  647. "max_score" : 1.0,
  648. "hits" : [
  649. {
  650. "_index" : "my-index",
  651. "_id" : "oWs5KXYB-XyJbifr9mrz",
  652. "_score" : 1.0,
  653. "_source" : {
  654. "@timestamp" : "2020-06-21T15:00:01-05:00",
  655. "message" : "211.11.9.0 - - [2020-06-21T15:00:01-05:00] \"GET /english/index.html HTTP/1.0\" 304 0"
  656. },
  657. "fields" : {
  658. "@timestamp" : [
  659. "2020-06-21T20:00:01.000Z"
  660. ],
  661. "client_ip" : [
  662. "211.11.9.0"
  663. ],
  664. "message" : [
  665. "211.11.9.0 - - [2020-06-21T15:00:01-05:00] \"GET /english/index.html HTTP/1.0\" 304 0"
  666. ],
  667. "day_of_week" : [
  668. "Sunday"
  669. ]
  670. }
  671. }
  672. ]
  673. }
  674. }
  675. ----
  676. // TESTRESPONSE[s/\.\.\./"took" : $body.took,"timed_out" : $body.timed_out,"_shards" : $body._shards,/]
  677. // TESTRESPONSE[s/"_id" : "oWs5KXYB-XyJbifr9mrz"/"_id": $body.hits.hits.0._id/]
  678. // TESTRESPONSE[s/"day_of_week" : \[\n\s+"Sunday"\n\s\]/"day_of_week": $body.hits.hits.0.fields.day_of_week/]
  679. [[runtime-indexed]]
  680. === Index a runtime field
  681. Runtime fields are defined by the context where they run. For example, you
  682. can define runtime fields in the
  683. <<runtime-search-request,context of a search query>> or within the
  684. <<runtime-mapping-fields,`runtime` section>> of an index mapping. If you
  685. decide to index a runtime field for greater performance, just move the full
  686. runtime field definition (including the script) to the context of an index
  687. mapping. This capability means you can write a script only once, and apply
  688. it to any context that supports runtime fields.
  689. IMPORTANT: After indexing a runtime field, you cannot update the included
  690. script. If you need to change the script, create a new field with the updated
  691. script.
  692. For example, let's say your company wants to replace some old pressure
  693. valves. The connected sensors are only capable of reporting a fraction of
  694. the true readings. Rather than outfit the pressure valves with new sensors,
  695. you decide to calculate the values based on reported readings. Based on the
  696. reported data, you define the following fields in your mapping for
  697. `my-index`:
  698. [source,console]
  699. ----
  700. PUT my-index/
  701. {
  702. "mappings": {
  703. "properties": {
  704. "timestamp": {
  705. "type": "date"
  706. },
  707. "temperature": {
  708. "type": "long"
  709. },
  710. "voltage": {
  711. "type": "double"
  712. },
  713. "node": {
  714. "type": "keyword"
  715. }
  716. }
  717. }
  718. }
  719. ----
  720. You then bulk index some sample data from your sensors. This data includes
  721. `voltage` readings for each sensor:
  722. [source,console]
  723. ----
  724. POST my-index/_bulk?refresh=true
  725. {"index":{}}
  726. {"timestamp": 1516729294000, "temperature": 200, "voltage": 5.2, "node": "a"}
  727. {"index":{}}
  728. {"timestamp": 1516642894000, "temperature": 201, "voltage": 5.8, "node": "b"}
  729. {"index":{}}
  730. {"timestamp": 1516556494000, "temperature": 202, "voltage": 5.1, "node": "a"}
  731. {"index":{}}
  732. {"timestamp": 1516470094000, "temperature": 198, "voltage": 5.6, "node": "b"}
  733. {"index":{}}
  734. {"timestamp": 1516383694000, "temperature": 200, "voltage": 4.2, "node": "c"}
  735. {"index":{}}
  736. {"timestamp": 1516297294000, "temperature": 202, "voltage": 4.0, "node": "c"}
  737. ----
  738. // TEST[continued]
  739. After talking to a few site engineers, you realize that the sensors should
  740. be reporting at least _double_ the current values, but potentially higher.
  741. You create a runtime field named `voltage_corrected` that retrieves the current
  742. voltage and multiplies it by `2`:
  743. [source,console]
  744. ----
  745. PUT my-index/_mapping
  746. {
  747. "runtime": {
  748. "voltage_corrected": {
  749. "type": "double",
  750. "script": {
  751. "source": """
  752. emit(doc['voltage'].value * params['multiplier'])
  753. """,
  754. "params": {
  755. "multiplier": 2
  756. }
  757. }
  758. }
  759. }
  760. }
  761. ----
  762. // TEST[continued]
  763. You retrieve the calculated values using the <<search-fields,`fields`>>
  764. parameter on the `_search` API:
  765. [source,console]
  766. ----
  767. GET my-index/_search
  768. {
  769. "fields": [
  770. "voltage_corrected",
  771. "node"
  772. ],
  773. "size": 2
  774. }
  775. ----
  776. // TEST[continued]
  777. // TEST[s/_search/_search\?filter_path=hits/]
  778. //
  779. ////
  780. [source,console-result]
  781. ----
  782. {
  783. "hits" : {
  784. "total" : {
  785. "value" : 6,
  786. "relation" : "eq"
  787. },
  788. "max_score" : 1.0,
  789. "hits" : [
  790. {
  791. "_index" : "my-index",
  792. "_id" : "z4TCrHgBdg9xpPrU6z9k",
  793. "_score" : 1.0,
  794. "_source" : {
  795. "timestamp" : 1516729294000,
  796. "temperature" : 200,
  797. "voltage" : 5.2,
  798. "node" : "a"
  799. },
  800. "fields" : {
  801. "voltage_corrected" : [
  802. 10.4
  803. ],
  804. "node" : [
  805. "a"
  806. ]
  807. }
  808. },
  809. {
  810. "_index" : "my-index",
  811. "_id" : "0ITCrHgBdg9xpPrU6z9k",
  812. "_score" : 1.0,
  813. "_source" : {
  814. "timestamp" : 1516642894000,
  815. "temperature" : 201,
  816. "voltage" : 5.8,
  817. "node" : "b"
  818. },
  819. "fields" : {
  820. "voltage_corrected" : [
  821. 11.6
  822. ],
  823. "node" : [
  824. "b"
  825. ]
  826. }
  827. }
  828. ]
  829. }
  830. }
  831. ----
  832. // TESTRESPONSE[s/"_id" : "z4TCrHgBdg9xpPrU6z9k"/"_id": $body.hits.hits.0._id/]
  833. // TESTRESPONSE[s/"_id" : "0ITCrHgBdg9xpPrU6z9k"/"_id": $body.hits.hits.1._id/]
  834. ////
  835. //
  836. After reviewing the sensor data and running some tests, you determine that the
  837. multiplier for reported sensor data should be `4`. To gain greater performance,
  838. you decide to index the `voltage_corrected` runtime field with the new
  839. `multiplier` parameter.
  840. In a new index named `my-index-00001`, copy the `voltage_corrected` runtime
  841. field definition into the mappings of the new index. It's that simple! You can
  842. add an optional parameter named `on_script_error` that determines whether to
  843. reject the entire document if the script throws an error at index time
  844. (default).
  845. [source,console]
  846. ----
  847. PUT my-index-00001/
  848. {
  849. "mappings": {
  850. "properties": {
  851. "timestamp": {
  852. "type": "date"
  853. },
  854. "temperature": {
  855. "type": "long"
  856. },
  857. "voltage": {
  858. "type": "double"
  859. },
  860. "node": {
  861. "type": "keyword"
  862. },
  863. "voltage_corrected": {
  864. "type": "double",
  865. "on_script_error": "fail", <1>
  866. "script": {
  867. "source": """
  868. emit(doc['voltage'].value * params['multiplier'])
  869. """,
  870. "params": {
  871. "multiplier": 4
  872. }
  873. }
  874. }
  875. }
  876. }
  877. }
  878. ----
  879. <1> Causes the entire document to be rejected if the script throws an error at
  880. index time. Setting the value to `ignore` will register the field in the
  881. document’s `_ignored` metadata field and continue indexing.
  882. Bulk index some sample data from your sensors into the `my-index-00001` index:
  883. [source,console]
  884. ----
  885. POST my-index-00001/_bulk?refresh=true
  886. { "index": {}}
  887. { "timestamp": 1516729294000, "temperature": 200, "voltage": 5.2, "node": "a"}
  888. { "index": {}}
  889. { "timestamp": 1516642894000, "temperature": 201, "voltage": 5.8, "node": "b"}
  890. { "index": {}}
  891. { "timestamp": 1516556494000, "temperature": 202, "voltage": 5.1, "node": "a"}
  892. { "index": {}}
  893. { "timestamp": 1516470094000, "temperature": 198, "voltage": 5.6, "node": "b"}
  894. { "index": {}}
  895. { "timestamp": 1516383694000, "temperature": 200, "voltage": 4.2, "node": "c"}
  896. { "index": {}}
  897. { "timestamp": 1516297294000, "temperature": 202, "voltage": 4.0, "node": "c"}
  898. ----
  899. // TEST[continued]
  900. You can now retrieve calculated values in a search query, and find documents
  901. based on precise values. The following range query returns all documents where
  902. the calculated `voltage_corrected` is greater than or equal to `10`, but less
  903. than or equal to `16`. Again, use the <<search-fields,`fields`>> parameter on
  904. the `_search` API to retrieve the fields you want:
  905. [source,console]
  906. ----
  907. POST my-index-00001/_search
  908. {
  909. "query": {
  910. "range": {
  911. "voltage_corrected": {
  912. "gte": 16,
  913. "lte": 20,
  914. "boost": 1.0
  915. }
  916. }
  917. },
  918. "fields": [
  919. "voltage_corrected", "node"]
  920. }
  921. ----
  922. // TEST[continued]
  923. // TEST[s/_search/_search\?filter_path=hits/]
  924. The response includes the `voltage_corrected` field for the documents that
  925. match the range query, based on the calculated value of the included script:
  926. [source,console-result]
  927. ----
  928. {
  929. "hits" : {
  930. "total" : {
  931. "value" : 2,
  932. "relation" : "eq"
  933. },
  934. "max_score" : 1.0,
  935. "hits" : [
  936. {
  937. "_index" : "my-index-00001",
  938. "_id" : "yoSLrHgBdg9xpPrUZz_P",
  939. "_score" : 1.0,
  940. "_source" : {
  941. "timestamp" : 1516383694000,
  942. "temperature" : 200,
  943. "voltage" : 4.2,
  944. "node" : "c"
  945. },
  946. "fields" : {
  947. "voltage_corrected" : [
  948. 16.8
  949. ],
  950. "node" : [
  951. "c"
  952. ]
  953. }
  954. },
  955. {
  956. "_index" : "my-index-00001",
  957. "_id" : "y4SLrHgBdg9xpPrUZz_P",
  958. "_score" : 1.0,
  959. "_source" : {
  960. "timestamp" : 1516297294000,
  961. "temperature" : 202,
  962. "voltage" : 4.0,
  963. "node" : "c"
  964. },
  965. "fields" : {
  966. "voltage_corrected" : [
  967. 16.0
  968. ],
  969. "node" : [
  970. "c"
  971. ]
  972. }
  973. }
  974. ]
  975. }
  976. }
  977. ----
  978. // TESTRESPONSE[s/"_id" : "yoSLrHgBdg9xpPrUZz_P"/"_id": $body.hits.hits.0._id/]
  979. // TESTRESPONSE[s/"_id" : "y4SLrHgBdg9xpPrUZz_P"/"_id": $body.hits.hits.1._id/]
  980. [[runtime-examples]]
  981. === Explore your data with runtime fields
  982. Consider a large set of log data that you want to extract fields from.
  983. Indexing the data is time consuming and uses a lot of disk space, and you just
  984. want to explore the data structure without committing to a schema up front.
  985. You know that your log data contains specific fields that you want to extract.
  986. In this case, we want to focus on the `@timestamp` and `message` fields. By
  987. using runtime fields, you can define scripts to calculate values at search
  988. time for these fields.
  989. [[runtime-examples-define-fields]]
  990. ==== Define indexed fields as a starting point
  991. You can start with a simple example by adding the `@timestamp` and `message`
  992. fields to the `my-index` mapping as indexed fields. To remain flexible, use
  993. `wildcard` as the field type for `message`:
  994. [source,console]
  995. ----
  996. PUT /my-index/
  997. {
  998. "mappings": {
  999. "properties": {
  1000. "@timestamp": {
  1001. "format": "strict_date_optional_time||epoch_second",
  1002. "type": "date"
  1003. },
  1004. "message": {
  1005. "type": "wildcard"
  1006. }
  1007. }
  1008. }
  1009. }
  1010. ----
  1011. [[runtime-examples-ingest-data]]
  1012. ==== Ingest some data
  1013. After mapping the fields you want to retrieve, index a few records from
  1014. your log data into {es}. The following request uses the <<docs-bulk,bulk API>>
  1015. to index raw log data into `my-index`. Instead of indexing all of your log
  1016. data, you can use a small sample to experiment with runtime fields.
  1017. The final document is not a valid Apache log format, but we can account for
  1018. that scenario in our script.
  1019. [source,console]
  1020. ----
  1021. POST /my-index/_bulk?refresh
  1022. {"index":{}}
  1023. {"timestamp":"2020-04-30T14:30:17-05:00","message":"40.135.0.0 - - [30/Apr/2020:14:30:17 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  1024. {"index":{}}
  1025. {"timestamp":"2020-04-30T14:30:53-05:00","message":"232.0.0.0 - - [30/Apr/2020:14:30:53 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  1026. {"index":{}}
  1027. {"timestamp":"2020-04-30T14:31:12-05:00","message":"26.1.0.0 - - [30/Apr/2020:14:31:12 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  1028. {"index":{}}
  1029. {"timestamp":"2020-04-30T14:31:19-05:00","message":"247.37.0.0 - - [30/Apr/2020:14:31:19 -0500] \"GET /french/splash_inet.html HTTP/1.0\" 200 3781"}
  1030. {"index":{}}
  1031. {"timestamp":"2020-04-30T14:31:22-05:00","message":"247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] \"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0"}
  1032. {"index":{}}
  1033. {"timestamp":"2020-04-30T14:31:27-05:00","message":"252.0.0.0 - - [30/Apr/2020:14:31:27 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"}
  1034. {"index":{}}
  1035. {"timestamp":"2020-04-30T14:31:28-05:00","message":"not a valid apache log"}
  1036. ----
  1037. // TEST[continued]
  1038. At this point, you can view how {es} stores your raw data.
  1039. [source,console]
  1040. ----
  1041. GET /my-index
  1042. ----
  1043. // TEST[continued]
  1044. The mapping contains two fields: `@timestamp` and `message`.
  1045. [source,console-result]
  1046. ----
  1047. {
  1048. "my-index" : {
  1049. "aliases" : { },
  1050. "mappings" : {
  1051. "properties" : {
  1052. "@timestamp" : {
  1053. "type" : "date",
  1054. "format" : "strict_date_optional_time||epoch_second"
  1055. },
  1056. "message" : {
  1057. "type" : "wildcard"
  1058. },
  1059. "timestamp" : {
  1060. "type" : "date"
  1061. }
  1062. }
  1063. },
  1064. ...
  1065. }
  1066. }
  1067. ----
  1068. // TESTRESPONSE[s/\.\.\./"settings": $body.my-index.settings/]
  1069. [[runtime-examples-grok]]
  1070. ==== Define a runtime field with a grok pattern
  1071. If you want to retrieve results that include `clientip`, you can add that
  1072. field as a runtime field in the mapping. The following runtime script defines a
  1073. grok pattern that extracts structured fields out of a single text
  1074. field within a document. A grok pattern is like a regular expression that
  1075. supports aliased expressions that you can reuse. See <<grok-basics,Grok basics>> to learn more about grok syntax.
  1076. The script matches on the `%{COMMONAPACHELOG}` log pattern, which understands
  1077. the structure of Apache logs. If the pattern matches, the script emits the
  1078. value matching IP address. If the pattern doesn't match
  1079. (`clientip != null`), the script just returns the field value without crashing.
  1080. [source,console]
  1081. ----
  1082. PUT my-index/_mappings
  1083. {
  1084. "runtime": {
  1085. "http.clientip": {
  1086. "type": "ip",
  1087. "script": """
  1088. String clientip=grok('%{COMMONAPACHELOG}').extract(doc["message"].value)?.clientip;
  1089. if (clientip != null) emit(clientip); <1>
  1090. """
  1091. }
  1092. }
  1093. }
  1094. ----
  1095. // TEST[continued]
  1096. <1> This condition ensures that the script doesn't crash even if the pattern of
  1097. the message doesn't match.
  1098. [[runtime-examples-grok-ip]]
  1099. ===== Search for a specific IP address
  1100. Using the `http.clientip` runtime field, you can define a simple query to run a
  1101. search for a specific IP address and return all related fields.
  1102. [source,console]
  1103. ----
  1104. GET my-index/_search
  1105. {
  1106. "query": {
  1107. "match": {
  1108. "http.clientip": "40.135.0.0"
  1109. }
  1110. },
  1111. "fields" : ["*"]
  1112. }
  1113. ----
  1114. // TEST[continued]
  1115. The API returns the following result. Without building your data structure in
  1116. advance, you can search and explore your data in meaningful ways to experiment
  1117. and determine which fields to index.
  1118. Also, remember that `if` statement in the script?
  1119. [source,painless]
  1120. ----
  1121. if (clientip != null) emit(clientip);
  1122. ----
  1123. If the script didn't include this condition, the query would fail on any shard
  1124. that doesn't match the pattern. By including this condition, the query skips
  1125. data that doesn't match the grok pattern.
  1126. [source,console-result]
  1127. ----
  1128. {
  1129. ...
  1130. "hits" : {
  1131. "total" : {
  1132. "value" : 1,
  1133. "relation" : "eq"
  1134. },
  1135. "max_score" : 1.0,
  1136. "hits" : [
  1137. {
  1138. "_index" : "my-index",
  1139. "_id" : "FdLqu3cBhqheMnFKd0gK",
  1140. "_score" : 1.0,
  1141. "_source" : {
  1142. "timestamp" : "2020-04-30T14:30:17-05:00",
  1143. "message" : "40.135.0.0 - - [30/Apr/2020:14:30:17 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"
  1144. },
  1145. "fields" : {
  1146. "http.clientip" : [
  1147. "40.135.0.0"
  1148. ],
  1149. "message" : [
  1150. "40.135.0.0 - - [30/Apr/2020:14:30:17 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"
  1151. ],
  1152. "timestamp" : [
  1153. "2020-04-30T19:30:17.000Z"
  1154. ]
  1155. }
  1156. }
  1157. ]
  1158. }
  1159. }
  1160. ----
  1161. // TESTRESPONSE[s/\.\.\./"took" : $body.took,"timed_out" : $body.timed_out,"_shards" : $body._shards,/]
  1162. // TESTRESPONSE[s/"_id" : "FdLqu3cBhqheMnFKd0gK"/"_id": $body.hits.hits.0._id/]
  1163. [[runtime-examples-grok-range]]
  1164. ===== Search for documents in a specific range
  1165. You can also run a <<query-dsl-range-query,range query>> that operates on the
  1166. `timestamp` field. The following query returns any documents where the
  1167. `timestamp` is greater than or equal to `2020-04-30T14:31:27-05:00`:
  1168. [source,console]
  1169. ----
  1170. GET my-index/_search
  1171. {
  1172. "query": {
  1173. "range": {
  1174. "timestamp": {
  1175. "gte": "2020-04-30T14:31:27-05:00"
  1176. }
  1177. }
  1178. }
  1179. }
  1180. ----
  1181. // TEST[continued]
  1182. The response includes the document where the log format doesn't match, but the
  1183. timestamp falls within the defined range.
  1184. [source,console-result]
  1185. ----
  1186. {
  1187. ...
  1188. "hits" : {
  1189. "total" : {
  1190. "value" : 2,
  1191. "relation" : "eq"
  1192. },
  1193. "max_score" : 1.0,
  1194. "hits" : [
  1195. {
  1196. "_index" : "my-index",
  1197. "_id" : "hdEhyncBRSB6iD-PoBqe",
  1198. "_score" : 1.0,
  1199. "_source" : {
  1200. "timestamp" : "2020-04-30T14:31:27-05:00",
  1201. "message" : "252.0.0.0 - - [30/Apr/2020:14:31:27 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"
  1202. }
  1203. },
  1204. {
  1205. "_index" : "my-index",
  1206. "_id" : "htEhyncBRSB6iD-PoBqe",
  1207. "_score" : 1.0,
  1208. "_source" : {
  1209. "timestamp" : "2020-04-30T14:31:28-05:00",
  1210. "message" : "not a valid apache log"
  1211. }
  1212. }
  1213. ]
  1214. }
  1215. }
  1216. ----
  1217. // TESTRESPONSE[s/\.\.\./"took" : $body.took,"timed_out" : $body.timed_out,"_shards" : $body._shards,/]
  1218. // TESTRESPONSE[s/"_id" : "hdEhyncBRSB6iD-PoBqe"/"_id": $body.hits.hits.0._id/]
  1219. // TESTRESPONSE[s/"_id" : "htEhyncBRSB6iD-PoBqe"/"_id": $body.hits.hits.1._id/]
  1220. [[runtime-examples-dissect]]
  1221. ==== Define a runtime field with a dissect pattern
  1222. If you don't need the power of regular expressions, you can use
  1223. <<dissect-processor,dissect patterns>> instead of grok patterns. Dissect
  1224. patterns match on fixed delimiters but are typically faster that grok.
  1225. You can use dissect to achieve the same results as parsing the Apache logs with
  1226. a <<runtime-examples-grok,grok pattern>>. Instead of matching on a log
  1227. pattern, you include the parts of the string that you want to discard. Paying
  1228. special attention to the parts of the string you want to discard will help build
  1229. successful dissect patterns.
  1230. [source,console]
  1231. ----
  1232. PUT my-index/_mappings
  1233. {
  1234. "runtime": {
  1235. "http.client.ip": {
  1236. "type": "ip",
  1237. "script": """
  1238. String clientip=dissect('%{clientip} %{ident} %{auth} [%{@timestamp}] "%{verb} %{request} HTTP/%{httpversion}" %{status} %{size}').extract(doc["message"].value)?.clientip;
  1239. if (clientip != null) emit(clientip);
  1240. """
  1241. }
  1242. }
  1243. }
  1244. ----
  1245. // TEST[continued]
  1246. Similarly, you can define a dissect pattern to extract the https://developer.mozilla.org/en-US/docs/Web/HTTP/Status[HTTP response code]:
  1247. [source,console]
  1248. ----
  1249. PUT my-index/_mappings
  1250. {
  1251. "runtime": {
  1252. "http.response": {
  1253. "type": "long",
  1254. "script": """
  1255. String response=dissect('%{clientip} %{ident} %{auth} [%{@timestamp}] "%{verb} %{request} HTTP/%{httpversion}" %{response} %{size}').extract(doc["message"].value)?.response;
  1256. if (response != null) emit(Integer.parseInt(response));
  1257. """
  1258. }
  1259. }
  1260. }
  1261. ----
  1262. // TEST[continued]
  1263. You can then run a query to retrieve a specific HTTP response using the
  1264. `http.response` runtime field:
  1265. [source,console]
  1266. ----
  1267. GET my-index/_search
  1268. {
  1269. "query": {
  1270. "match": {
  1271. "http.response": "304"
  1272. }
  1273. },
  1274. "fields" : ["*"]
  1275. }
  1276. ----
  1277. // TEST[continued]
  1278. The response includes a single document where the HTTP response is `304`:
  1279. [source,console-result]
  1280. ----
  1281. {
  1282. ...
  1283. "hits" : {
  1284. "total" : {
  1285. "value" : 1,
  1286. "relation" : "eq"
  1287. },
  1288. "max_score" : 1.0,
  1289. "hits" : [
  1290. {
  1291. "_index" : "my-index",
  1292. "_id" : "A2qDy3cBWRMvVAuI7F8M",
  1293. "_score" : 1.0,
  1294. "_source" : {
  1295. "timestamp" : "2020-04-30T14:31:22-05:00",
  1296. "message" : "247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] \"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0"
  1297. },
  1298. "fields" : {
  1299. "http.clientip" : [
  1300. "247.37.0.0"
  1301. ],
  1302. "http.response" : [
  1303. 304
  1304. ],
  1305. "message" : [
  1306. "247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] \"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0"
  1307. ],
  1308. "http.client.ip" : [
  1309. "247.37.0.0"
  1310. ],
  1311. "timestamp" : [
  1312. "2020-04-30T19:31:22.000Z"
  1313. ]
  1314. }
  1315. }
  1316. ]
  1317. }
  1318. }
  1319. ----
  1320. // TESTRESPONSE[s/\.\.\./"took" : $body.took,"timed_out" : $body.timed_out,"_shards" : $body._shards,/]
  1321. // TESTRESPONSE[s/"_id" : "A2qDy3cBWRMvVAuI7F8M"/"_id": $body.hits.hits.0._id/]