ingest.asciidoc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. [[ingest]]
  2. = Ingest pipelines
  3. Ingest pipelines let you perform common transformations on your data before
  4. indexing. For example, you can use pipelines to remove fields, extract values
  5. from text, and enrich your data.
  6. A pipeline consists of a series of configurable tasks called
  7. <<processors,processors>>. Each processor runs sequentially, making specific
  8. changes to incoming documents. After the processors have run, {es} adds the
  9. transformed documents to your data stream or index.
  10. image::images/ingest/ingest-process.svg[Ingest pipeline diagram,align="center"]
  11. You can create and manage ingest pipelines using {kib}'s **Ingest Pipelines**
  12. feature or the <<ingest-apis,ingest APIs>>. {es} stores pipelines in the
  13. <<cluster-state,cluster state>>.
  14. [discrete]
  15. [[ingest-prerequisites]]
  16. === Prerequisites
  17. * Nodes with the <<node-ingest-node,`ingest`>> node role handle pipeline
  18. processing. To use ingest pipelines, your cluster must have at least one node
  19. with the `ingest` role. For heavy ingest loads, we recommend creating
  20. <<node-ingest-node,dedicated ingest nodes>>.
  21. * If the {es} security features are enabled, you must have the `manage_pipeline`
  22. <<privileges-list-cluster,cluster privilege>> to manage ingest pipelines. To use
  23. {kib}'s **Ingest Pipelines** feature, you also need the
  24. `cluster:monitor/nodes/info` cluster privileges.
  25. * Pipelines including the `enrich` processor require additional setup. See
  26. <<ingest-enriching-data>>.
  27. [discrete]
  28. [[create-manage-ingest-pipelines]]
  29. === Create and manage pipelines
  30. In {kib}, open the main menu and click **Stack Management** > **Ingest
  31. Pipelines**. From the list view, you can:
  32. * View a list of your pipelines and drill down into details
  33. * Edit or clone existing pipelines
  34. * Delete pipelines
  35. To create a new pipeline, click **Create pipeline**. For an example tutorial,
  36. see <<common-log-format-example>>.
  37. [role="screenshot"]
  38. image::images/ingest/ingest-pipeline-list.png[Kibana's Ingest Pipelines list view,align="center"]
  39. You can also use the <<ingest-apis,ingest APIs>> to create and manage pipelines.
  40. The following <<put-pipeline-api,create pipeline API>> request creates
  41. a pipeline containing two <<set-processor,`set`>> processors followed by a
  42. <<lowercase-processor,`lowercase`>> processor. The processors run sequentially
  43. in the order specified.
  44. [source,console]
  45. ----
  46. PUT _ingest/pipeline/my-pipeline
  47. {
  48. "description": "My optional pipeline description",
  49. "processors": [
  50. {
  51. "set": {
  52. "description": "My optional processor description",
  53. "field": "my-long-field",
  54. "value": 10
  55. }
  56. },
  57. {
  58. "set": {
  59. "description": "Set 'my-boolean-field' to true",
  60. "field": "my-boolean-field",
  61. "value": true
  62. }
  63. },
  64. {
  65. "lowercase": {
  66. "field": "my-keyword-field"
  67. }
  68. }
  69. ]
  70. }
  71. ----
  72. // TESTSETUP
  73. [discrete]
  74. [[manage-pipeline-versions]]
  75. === Manage pipeline versions
  76. When you create or update a pipeline, you can specify an optional `version`
  77. integer. You can use this version number with the
  78. <<put-pipeline-api-query-params,`if_version`>> parameter to conditionally
  79. update the pipeline. When the `if_version` parameter is specified, a successful
  80. update increments the pipeline's version.
  81. [source,console]
  82. ----
  83. PUT _ingest/pipeline/my-pipeline-id
  84. {
  85. "version": 1,
  86. "processors": [ ... ]
  87. }
  88. ----
  89. // TEST[s/\.\.\./{"lowercase": {"field":"my-keyword-field"}}/]
  90. To unset the `version` number using the API, replace or update the pipeline
  91. without specifying the `version` parameter.
  92. [discrete]
  93. [[test-pipeline]]
  94. === Test a pipeline
  95. Before using a pipeline in production, we recommend you test it using sample
  96. documents. When creating or editing a pipeline in {kib}, click **Add
  97. documents**. In the **Documents** tab, provide sample documents and click **Run
  98. the pipeline**.
  99. [role="screenshot"]
  100. image::images/ingest/test-a-pipeline.png[Test a pipeline in Kibana,align="center"]
  101. You can also test pipelines using the <<simulate-pipeline-api,simulate pipeline
  102. API>>. You can specify a configured pipeline in the request path. For example,
  103. the following request tests `my-pipeline`.
  104. [source,console]
  105. ----
  106. POST _ingest/pipeline/my-pipeline/_simulate
  107. {
  108. "docs": [
  109. {
  110. "_source": {
  111. "my-keyword-field": "FOO"
  112. }
  113. },
  114. {
  115. "_source": {
  116. "my-keyword-field": "BAR"
  117. }
  118. }
  119. ]
  120. }
  121. ----
  122. Alternatively, you can specify a pipeline and its processors in the request
  123. body.
  124. [source,console]
  125. ----
  126. POST _ingest/pipeline/_simulate
  127. {
  128. "pipeline": {
  129. "processors": [
  130. {
  131. "lowercase": {
  132. "field": "my-keyword-field"
  133. }
  134. }
  135. ]
  136. },
  137. "docs": [
  138. {
  139. "_source": {
  140. "my-keyword-field": "FOO"
  141. }
  142. },
  143. {
  144. "_source": {
  145. "my-keyword-field": "BAR"
  146. }
  147. }
  148. ]
  149. }
  150. ----
  151. The API returns transformed documents:
  152. [source,console-result]
  153. ----
  154. {
  155. "docs": [
  156. {
  157. "doc": {
  158. "_index": "_index",
  159. "_id": "_id",
  160. "_source": {
  161. "my-keyword-field": "foo"
  162. },
  163. "_ingest": {
  164. "timestamp": "2099-03-07T11:04:03.000Z"
  165. }
  166. }
  167. },
  168. {
  169. "doc": {
  170. "_index": "_index",
  171. "_id": "_id",
  172. "_source": {
  173. "my-keyword-field": "bar"
  174. },
  175. "_ingest": {
  176. "timestamp": "2099-03-07T11:04:04.000Z"
  177. }
  178. }
  179. }
  180. ]
  181. }
  182. ----
  183. // TESTRESPONSE[s/"2099-03-07T11:04:03.000Z"/$body.docs.0.doc._ingest.timestamp/]
  184. // TESTRESPONSE[s/"2099-03-07T11:04:04.000Z"/$body.docs.1.doc._ingest.timestamp/]
  185. [discrete]
  186. [[add-pipeline-to-indexing-request]]
  187. === Add a pipeline to an indexing request
  188. Use the `pipeline` query parameter to apply a pipeline to documents in
  189. <<docs-index_,individual>> or <<docs-bulk,bulk>> indexing requests.
  190. [source,console]
  191. ----
  192. POST my-data-stream/_doc?pipeline=my-pipeline
  193. {
  194. "@timestamp": "2099-03-07T11:04:05.000Z",
  195. "my-keyword-field": "foo"
  196. }
  197. PUT my-data-stream/_bulk?pipeline=my-pipeline
  198. { "create":{ } }
  199. { "@timestamp": "2099-03-07T11:04:06.000Z", "my-keyword-field": "foo" }
  200. { "create":{ } }
  201. { "@timestamp": "2099-03-07T11:04:07.000Z", "my-keyword-field": "bar" }
  202. ----
  203. // TEST[setup:my_data_stream]
  204. // TEST[teardown:data_stream_cleanup]
  205. You can also use the `pipeline` parameter with the <<docs-update-by-query,update
  206. by query>> or <<docs-reindex,reindex>> APIs.
  207. [source,console]
  208. ----
  209. POST my-data-stream/_update_by_query?pipeline=my-pipeline
  210. POST _reindex
  211. {
  212. "source": {
  213. "index": "my-data-stream"
  214. },
  215. "dest": {
  216. "index": "my-new-data-stream",
  217. "op_type": "create",
  218. "pipeline": "my-pipeline"
  219. }
  220. }
  221. ----
  222. // TEST[setup:my_data_stream]
  223. // TEST[teardown:data_stream_cleanup]
  224. [discrete]
  225. [[set-default-pipeline]]
  226. === Set a default pipeline
  227. Use the <<index-default-pipeline,`index.default_pipeline`>> index setting to set
  228. a default pipeline. {es} applies this pipeline if no `pipeline` parameter
  229. is specified.
  230. [discrete]
  231. [[set-final-pipeline]]
  232. === Set a final pipeline
  233. Use the <<index-final-pipeline,`index.final_pipeline`>> index setting to set a
  234. final pipeline. {es} applies this pipeline after the request or default
  235. pipeline, even if neither is specified.
  236. [discrete]
  237. [[pipelines-for-beats]]
  238. === Pipelines for {beats}
  239. To add an ingest pipeline to an Elastic Beat, specify the `pipeline`
  240. parameter under `output.elasticsearch` in `<BEAT_NAME>.yml`. For example,
  241. for {filebeat}, you'd specify `pipeline` in `filebeat.yml`.
  242. [source,yaml]
  243. ----
  244. output.elasticsearch:
  245. hosts: ["localhost:9200"]
  246. pipeline: my-pipeline
  247. ----
  248. [discrete]
  249. [[pipelines-for-fleet-elastic-agent]]
  250. === Pipelines for {fleet} and {agent}
  251. {fleet-guide}/index.html[{fleet}] automatically adds ingest pipelines for its
  252. integrations. {fleet} applies these pipelines using <<index-templates,index
  253. templates>> that include <<set-default-pipeline,pipeline index settings>>. {es}
  254. matches these templates to your {fleet} data streams based on the
  255. {fleet-guide}/data-streams.html#data-streams-naming-scheme[stream's naming
  256. scheme].
  257. WARNING: Do not change {fleet}'s ingest pipelines or use custom pipelines for
  258. your {fleet} integrations. Doing so can break your {fleet} data streams.
  259. {fleet} doesn't provide an ingest pipeline for the **Custom logs** integration.
  260. You can safely specify a pipeline for this integration in one of two ways: an
  261. <<pipeline-custom-logs-index-template,index template>> or a
  262. <<pipeline-custom-logs-configuration,custom configuration>>.
  263. [[pipeline-custom-logs-index-template]]
  264. **Option 1: Index template**
  265. // tag::create-name-custom-logs-pipeline[]
  266. . <<create-manage-ingest-pipelines,Create>> and <<test-pipeline,test>> your
  267. ingest pipeline. Name your pipeline `logs-<dataset-name>-default`. This makes
  268. tracking the pipeline for your integration easier.
  269. +
  270. --
  271. For example, the following request creates a pipeline for the `my-app` dataset.
  272. The pipeline's name is `logs-my_app-default`.
  273. [source,console]
  274. ----
  275. PUT _ingest/pipeline/logs-my_app-default
  276. {
  277. "description": "Pipeline for `my_app` dataset",
  278. "processors": [ ... ]
  279. }
  280. ----
  281. // TEST[s/\.\.\./{"lowercase": {"field":"my-keyword-field"}}/]
  282. --
  283. // end::create-name-custom-logs-pipeline[]
  284. . Create an <<index-templates,index template>> that includes your pipeline in
  285. the <<index-default-pipeline,`index.default_pipeline`>> or
  286. <<index-final-pipeline,`index.final_pipeline`>> index setting. Ensure the
  287. template is <<create-index-template,data stream enabled>>. The
  288. template's index pattern should match `logs-<dataset-name>-*`.
  289. +
  290. --
  291. You can create this template using {kib}'s <<manage-index-templates,**Index
  292. Management**>> feature or the <<indices-put-template,create index template
  293. API>>.
  294. For example, the following request creates a template matching `logs-my_app-*`.
  295. The template uses a component template that contains the
  296. `index.default_pipeline` index setting.
  297. [source,console]
  298. ----
  299. # Creates a component template for index settings
  300. PUT _component_template/logs-my_app-settings
  301. {
  302. "template": {
  303. "settings": {
  304. "index.default_pipeline": "logs-my_app-default",
  305. "index.lifecycle.name": "logs"
  306. }
  307. }
  308. }
  309. # Creates an index template matching `logs-my_app-*`
  310. PUT _index_template/logs-my_app-template
  311. {
  312. "index_patterns": ["logs-my_app-*"],
  313. "data_stream": { },
  314. "priority": 500,
  315. "composed_of": ["logs-my_app-settings", "logs-my_app-mappings"]
  316. }
  317. ----
  318. // TEST[continued]
  319. // TEST[s/, "logs-my_app-mappings"//]
  320. --
  321. // tag::name-custom-logs-dataset[]
  322. . When adding or editing your **Custom logs** integration in {fleet},
  323. click **Configure integration > Custom log file > Advanced options**.
  324. . In **Dataset name**, specify your dataset's name. {fleet} will add new data
  325. for the integration to the resulting `logs-<dataset-name>-default` data stream.
  326. +
  327. For example, if your dataset's name was `my_app`, {fleet} adds new data to the
  328. `logs-my_app-default` data stream.
  329. // end::name-custom-logs-dataset[]
  330. +
  331. [role="screenshot"]
  332. image::images/ingest/custom-logs.png[Set up custom log integration in Fleet,align="center"]
  333. . Use the <<indices-rollover-index,rollover API>> to roll over your data stream.
  334. This ensures {es} applies the index template and its pipeline settings to any
  335. new data for the integration.
  336. +
  337. --
  338. ////
  339. [source,console]
  340. ----
  341. PUT _data_stream/logs-my_app-default
  342. ----
  343. // TEST[continued]
  344. ////
  345. [source,console]
  346. ----
  347. POST logs-my_app-default/_rollover/
  348. ----
  349. // TEST[continued]
  350. ////
  351. [source,console]
  352. ----
  353. DELETE _data_stream/*
  354. DELETE _index_template/*
  355. ----
  356. // TEST[continued]
  357. ////
  358. --
  359. [[pipeline-custom-logs-configuration]]
  360. **Option 2: Custom configuration**
  361. include::ingest.asciidoc[tag=create-name-custom-logs-pipeline]
  362. include::ingest.asciidoc[tag=name-custom-logs-dataset]
  363. . In **Custom Configurations**, specify your pipeline in the `pipeline` policy
  364. setting.
  365. +
  366. [role="screenshot"]
  367. image::images/ingest/custom-logs-pipeline.png[Custom pipeline configuration for custom log integration,align="center"]
  368. **{agent} standalone**
  369. If you run {agent} standalone, you can apply pipelines using an
  370. <<index-templates,index template>> that includes the
  371. <<index-default-pipeline,`index.default_pipeline`>> or
  372. <<index-final-pipeline,`index.final_pipeline`>> index setting. Alternatively,
  373. you can specify the `pipeline` policy setting in your `elastic-agent.yml`
  374. configuration. See {fleet-guide}/install-standalone-elastic-agent.html[Install standalone {agent}s].
  375. [discrete]
  376. [[access-source-fields]]
  377. === Access source fields in a processor
  378. Processors have read and write access to an incoming document's source fields.
  379. To access a field key in a processor, use its field name. The following `set`
  380. processor accesses `my-long-field`.
  381. [source,console]
  382. ----
  383. PUT _ingest/pipeline/my-pipeline
  384. {
  385. "processors": [
  386. {
  387. "set": {
  388. "field": "my-long-field",
  389. "value": 10
  390. }
  391. }
  392. ]
  393. }
  394. ----
  395. You can also prepend the `_source` prefix.
  396. [source,console]
  397. ----
  398. PUT _ingest/pipeline/my-pipeline
  399. {
  400. "processors": [
  401. {
  402. "set": {
  403. "field": "_source.my-long-field",
  404. "value": 10
  405. }
  406. }
  407. ]
  408. }
  409. ----
  410. Use dot notation to access object fields.
  411. IMPORTANT: If your document contains flattened objects, use the
  412. <<dot-expand-processor,`dot_expander`>> processor to expand them first. Other
  413. ingest processors cannot access flattened objects.
  414. [source,console]
  415. ----
  416. PUT _ingest/pipeline/my-pipeline
  417. {
  418. "processors": [
  419. {
  420. "dot_expander": {
  421. "description": "Expand 'my-object-field.my-property'",
  422. "field": "my-object-field.my-property"
  423. }
  424. },
  425. {
  426. "set": {
  427. "description": "Set 'my-object-field.my-property' to 10",
  428. "field": "my-object-field.my-property",
  429. "value": 10
  430. }
  431. }
  432. ]
  433. }
  434. ----
  435. [[template-snippets]]
  436. Several processor parameters support https://mustache.github.io[Mustache]
  437. template snippets. To access field values in a template snippet, enclose the
  438. field name in triple curly brackets:`{{{field-name}}}`. You can use template
  439. snippets to dynamically set field names.
  440. [source,console]
  441. ----
  442. PUT _ingest/pipeline/my-pipeline
  443. {
  444. "processors": [
  445. {
  446. "set": {
  447. "description": "Set dynamic '<service>' field to 'code' value",
  448. "field": "{{{service}}}",
  449. "value": "{{{code}}}"
  450. }
  451. }
  452. ]
  453. }
  454. ----
  455. [discrete]
  456. [[access-metadata-fields]]
  457. === Access metadata fields in a processor
  458. Processors can access the following metadata fields by name:
  459. * `_index`
  460. * `_id`
  461. * `_routing`
  462. * `_dynamic_templates`
  463. [source,console]
  464. ----
  465. PUT _ingest/pipeline/my-pipeline
  466. {
  467. "processors": [
  468. {
  469. "set": {
  470. "description": "Set '_routing' to 'geoip.country_iso_code' value",
  471. "field": "_routing",
  472. "value": "{{{geoip.country_iso_code}}}"
  473. }
  474. }
  475. ]
  476. }
  477. ----
  478. Use a Mustache template snippet to access metadata field values. For example,
  479. `{{{_routing}}}` retrieves a document's routing value.
  480. [source,console]
  481. ----
  482. PUT _ingest/pipeline/my-pipeline
  483. {
  484. "processors": [
  485. {
  486. "set": {
  487. "description": "Use geo_point dynamic template for address field",
  488. "field": "_dynamic_templates",
  489. "value": {
  490. "address": "geo_point"
  491. }
  492. }
  493. }
  494. ]
  495. }
  496. ----
  497. The set processor above tells ES to use the dynamic template named `geo_point`
  498. for the field `address` if this field is not defined in the mapping of the index
  499. yet. This processor overrides the dynamic template for the field `address` if
  500. already defined in the bulk request, but has no effect on other dynamic
  501. templates defined in the bulk request.
  502. WARNING: If you <<create-document-ids-automatically,automatically generate>>
  503. document IDs, you cannot use `{{{_id}}}` in a processor. {es} assigns
  504. auto-generated `_id` values after ingest.
  505. [discrete]
  506. [[access-ingest-metadata]]
  507. === Access ingest metadata in a processor
  508. Ingest processors can add and access ingest metadata using the `_ingest` key.
  509. Unlike source and metadata fields, {es} does not index ingest metadata fields by
  510. default. {es} also allows source fields that start with an `_ingest` key. If
  511. your data includes such source fields, use `_source._ingest` to access them.
  512. Pipelines only create the `_ingest.timestamp` ingest metadata field by default.
  513. This field contains a timestamp of when {es} received the document's indexing
  514. request. To index `_ingest.timestamp` or other ingest metadata fields, use the
  515. `set` processor.
  516. [source,console]
  517. ----
  518. PUT _ingest/pipeline/my-pipeline
  519. {
  520. "processors": [
  521. {
  522. "set": {
  523. "description": "Index the ingest timestamp as 'event.ingested'",
  524. "field": "event.ingested",
  525. "value": "{{{_ingest.timestamp}}}"
  526. }
  527. }
  528. ]
  529. }
  530. ----
  531. [discrete]
  532. [[handling-pipeline-failures]]
  533. === Handling pipeline failures
  534. A pipeline's processors run sequentially. By default, pipeline processing stops
  535. when one of these processors fails or encounters an error.
  536. To ignore a processor failure and run the pipeline's remaining processors, set
  537. `ignore_failure` to `true`.
  538. [source,console]
  539. ----
  540. PUT _ingest/pipeline/my-pipeline
  541. {
  542. "processors": [
  543. {
  544. "rename": {
  545. "description": "Rename 'provider' to 'cloud.provider'",
  546. "field": "provider",
  547. "target_field": "cloud.provider",
  548. "ignore_failure": true
  549. }
  550. }
  551. ]
  552. }
  553. ----
  554. Use the `on_failure` parameter to specify a list of processors to run
  555. immediately after a processor failure. If `on_failure` is specified, {es}
  556. afterward runs the pipeline's remaining processors, even if the `on_failure`
  557. configuration is empty.
  558. [source,console]
  559. ----
  560. PUT _ingest/pipeline/my-pipeline
  561. {
  562. "processors": [
  563. {
  564. "rename": {
  565. "description": "Rename 'provider' to 'cloud.provider'",
  566. "field": "provider",
  567. "target_field": "cloud.provider",
  568. "on_failure": [
  569. {
  570. "set": {
  571. "description": "Set 'error.message'",
  572. "field": "error.message",
  573. "value": "Field 'provider' does not exist. Cannot rename to 'cloud.provider'",
  574. "override": false
  575. }
  576. }
  577. ]
  578. }
  579. }
  580. ]
  581. }
  582. ----
  583. Nest a list of `on_failure` processors for nested error handling.
  584. [source,console]
  585. ----
  586. PUT _ingest/pipeline/my-pipeline
  587. {
  588. "processors": [
  589. {
  590. "rename": {
  591. "description": "Rename 'provider' to 'cloud.provider'",
  592. "field": "provider",
  593. "target_field": "cloud.provider",
  594. "on_failure": [
  595. {
  596. "set": {
  597. "description": "Set 'error.message'",
  598. "field": "error.message",
  599. "value": "Field 'provider' does not exist. Cannot rename to 'cloud.provider'",
  600. "override": false,
  601. "on_failure": [
  602. {
  603. "set": {
  604. "description": "Set 'error.message.multi'",
  605. "field": "error.message.multi",
  606. "value": "Document encountered multiple ingest errors",
  607. "override": true
  608. }
  609. }
  610. ]
  611. }
  612. }
  613. ]
  614. }
  615. }
  616. ]
  617. }
  618. ----
  619. You can also specify `on_failure` for a pipeline. If a processor without an
  620. `on_failure` value fails, {es} uses this pipeline-level parameter as a fallback.
  621. {es} will not attempt to run the pipeline's remaining processors.
  622. [source,console]
  623. ----
  624. PUT _ingest/pipeline/my-pipeline
  625. {
  626. "processors": [ ... ],
  627. "on_failure": [
  628. {
  629. "set": {
  630. "description": "Index document to 'failed-<index>'",
  631. "field": "_index",
  632. "value": "failed-{{{ _index }}}"
  633. }
  634. }
  635. ]
  636. }
  637. ----
  638. // TEST[s/\.\.\./{"lowercase": {"field":"my-keyword-field"}}/]
  639. Additional information about the pipeline failure may be available in the
  640. document metadata fields `on_failure_message`, `on_failure_processor_type`,
  641. `on_failure_processor_tag`, and `on_failure_pipeline`. These fields are
  642. accessible only from within an `on_failure` block.
  643. The following example uses the metadata fields to include information about
  644. pipeline failures in documents.
  645. [source,console]
  646. ----
  647. PUT _ingest/pipeline/my-pipeline
  648. {
  649. "processors": [ ... ],
  650. "on_failure": [
  651. {
  652. "set": {
  653. "description": "Record error information",
  654. "field": "error_information",
  655. "value": "Processor {{ _ingest.on_failure_processor_type }} with tag {{ _ingest.on_failure_processor_tag }} in pipeline {{ _ingest.on_failure_pipeline }} failed with message {{ _ingest.on_failure_message }}"
  656. }
  657. }
  658. ]
  659. }
  660. ----
  661. // TEST[s/\.\.\./{"lowercase": {"field":"my-keyword-field"}}/]
  662. [discrete]
  663. [[conditionally-run-processor]]
  664. === Conditionally run a processor
  665. Each processor supports an optional `if` condition, written as a
  666. {painless}/painless-guide.html[Painless script]. If provided, the processor only
  667. runs when the `if` condition is `true`.
  668. IMPORTANT: `if` condition scripts run in Painless's
  669. {painless}/painless-ingest-processor-context.html[ingest processor context]. In
  670. `if` conditions, `ctx` values are read-only.
  671. [source,console]
  672. ----
  673. PUT _ingest/pipeline/my-pipeline
  674. {
  675. "processors": [
  676. {
  677. "drop": {
  678. "description": "Drop documents with 'network.name' of 'Guest'",
  679. "if": "ctx?.network?.name == 'Guest'"
  680. }
  681. }
  682. ]
  683. }
  684. ----
  685. If the <<script-painless-regex-enabled,`script.painless.regex.enabled`>> cluster
  686. setting is enabled, you can use regular expressions in your `if` condition
  687. scripts. For supported syntax, see {painless}/painless-regexes.html[Painless
  688. regular expressions].
  689. TIP: If possible, avoid using regular expressions. Expensive regular expressions
  690. can slow indexing speeds.
  691. [source,console]
  692. ----
  693. PUT _ingest/pipeline/my-pipeline
  694. {
  695. "processors": [
  696. {
  697. "set": {
  698. "description": "If 'url.scheme' is 'http', set 'url.insecure' to true",
  699. "if": "ctx.url?.scheme =~ /^http[^s]/",
  700. "field": "url.insecure",
  701. "value": true
  702. }
  703. }
  704. ]
  705. }
  706. ----
  707. You must specify `if` conditions as valid JSON on a single line. However, you
  708. can use the {kibana-ref}/console-kibana.html#configuring-console[{kib}
  709. console]'s triple quote syntax to write and debug larger scripts.
  710. TIP: If possible, avoid using complex or expensive `if` condition scripts.
  711. Expensive condition scripts can slow indexing speeds.
  712. [source,console]
  713. ----
  714. PUT _ingest/pipeline/my-pipeline
  715. {
  716. "processors": [
  717. {
  718. "drop": {
  719. "description": "Drop documents that don't contain 'prod' tag",
  720. "if": """
  721. Collection tags = ctx.tags;
  722. if(tags != null){
  723. for (String tag : tags) {
  724. if (tag.toLowerCase().contains('prod')) {
  725. return false;
  726. }
  727. }
  728. }
  729. return true;
  730. """
  731. }
  732. }
  733. ]
  734. }
  735. ----
  736. You can also specify a <<modules-scripting-stored-scripts,stored script>> as the
  737. `if` condition.
  738. [source,console]
  739. ----
  740. PUT _scripts/my-stored-script
  741. {
  742. "script": {
  743. "lang": "painless",
  744. "source": """
  745. Collection tags = ctx.tags;
  746. if(tags != null){
  747. for (String tag : tags) {
  748. if (tag.toLowerCase().contains('prod')) {
  749. return false;
  750. }
  751. }
  752. }
  753. return true;
  754. """
  755. }
  756. }
  757. PUT _ingest/pipeline/my-pipeline
  758. {
  759. "processors": [
  760. {
  761. "drop": {
  762. "description": "If 'url.scheme' is 'http', set 'url.insecure' to true",
  763. "if": { "id": "my-stored-script" }
  764. }
  765. }
  766. ]
  767. }
  768. ----
  769. Incoming documents often contain object fields. If a processor script attempts
  770. to access a field whose parent object does not exist, {es} returns a
  771. `NullPointerException`. To avoid these exceptions, use
  772. {painless}/painless-operators-reference.html#null-safe-operator[null safe
  773. operators], such as `?.`, and write your scripts to be null safe.
  774. For example, `ctx.network?.name.equalsIgnoreCase('Guest')` is not null safe.
  775. `ctx.network?.name` can return null. Rewrite the script as
  776. `'Guest'.equalsIgnoreCase(ctx.network?.name)`, which is null safe because
  777. `Guest` is always non-null.
  778. If you can't rewrite a script to be null safe, include an explicit null check.
  779. [source,console]
  780. ----
  781. PUT _ingest/pipeline/my-pipeline
  782. {
  783. "processors": [
  784. {
  785. "drop": {
  786. "description": "Drop documents that contain 'network.name' of 'Guest'",
  787. "if": "ctx.network?.name != null && ctx.network.name.contains('Guest')"
  788. }
  789. }
  790. ]
  791. }
  792. ----
  793. [discrete]
  794. [[conditionally-apply-pipelines]]
  795. === Conditionally apply pipelines
  796. Combine an `if` condition with the <<pipeline-processor,`pipeline`>> processor
  797. to apply other pipelines to documents based on your criteria. You can use this
  798. pipeline as the <<set-default-pipeline,default pipeline>> in an
  799. <<index-templates,index template>> used to configure multiple data streams or
  800. indices.
  801. [source,console]
  802. ----
  803. PUT _ingest/pipeline/one-pipeline-to-rule-them-all
  804. {
  805. "processors": [
  806. {
  807. "pipeline": {
  808. "description": "If 'service.name' is 'apache_httpd', use 'httpd_pipeline'",
  809. "if": "ctx.service?.name == 'apache_httpd'",
  810. "name": "httpd_pipeline"
  811. }
  812. },
  813. {
  814. "pipeline": {
  815. "description": "If 'service.name' is 'syslog', use 'syslog_pipeline'",
  816. "if": "ctx.service?.name == 'syslog'",
  817. "name": "syslog_pipeline"
  818. }
  819. },
  820. {
  821. "fail": {
  822. "description": "If 'service.name' is not 'apache_httpd' or 'syslog', return a failure message",
  823. "if": "ctx.service?.name != 'apache_httpd' && ctx.service?.name != 'syslog'",
  824. "message": "This pipeline requires service.name to be either `syslog` or `apache_httpd`"
  825. }
  826. }
  827. ]
  828. }
  829. ----
  830. [discrete]
  831. [[get-pipeline-usage-stats]]
  832. === Get pipeline usage statistics
  833. Use the <<cluster-nodes-stats,node stats>> API to get global and per-pipeline
  834. ingest statistics. Use these stats to determine which pipelines run most
  835. frequently or spend the most time processing.
  836. [source,console]
  837. ----
  838. GET _nodes/stats/ingest?filter_path=nodes.*.ingest
  839. ----
  840. include::ingest/common-log-format-example.asciidoc[]
  841. include::ingest/enrich.asciidoc[]
  842. include::ingest/processors.asciidoc[]