ingest.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 Node
  12. Pipelines** feature or the <<ingest-apis,ingest APIs>>. {es} stores pipelines in
  13. the <<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 Node 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 Node
  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 a 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 Node 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. {es} doesn't use this `version` number internally, but you can use it
  78. to track changes to a pipeline.
  79. [source,console]
  80. ----
  81. PUT _ingest/pipeline/my-pipeline-id
  82. {
  83. "version" : 1,
  84. "processors": [ ... ]
  85. }
  86. ----
  87. // TEST[s/\.\.\./{"lowercase": {"field":"my-keyword-field"}}/]
  88. To unset the `version` number using the API, replace or update the pipeline
  89. without specifying the `version` parameter.
  90. [discrete]
  91. [[test-pipeline]]
  92. === Test a pipeline
  93. Before using a pipeline in production, we recommend you test it using sample
  94. documents. When creating or editing a pipeline in {kib}, click **Add
  95. documents**. In the **Documents** tab, provide sample documents and click **Run
  96. the pipeline**.
  97. [role="screenshot"]
  98. image::images/ingest/test-a-pipeline.png[Test a pipeline in Kibana,align="center"]
  99. You can also test pipelines using the <<simulate-pipeline-api,simulate pipeline
  100. API>>. You can specify a configured pipeline in the request path. For example,
  101. the following request tests `my-pipeline`.
  102. [source,console]
  103. ----
  104. POST _ingest/pipeline/my-pipeline/_simulate
  105. {
  106. "docs": [
  107. {
  108. "_source": {
  109. "my-keyword-field": "FOO"
  110. }
  111. },
  112. {
  113. "_source": {
  114. "my-keyword-field": "BAR"
  115. }
  116. }
  117. ]
  118. }
  119. ----
  120. Alternatively, you can specify a pipeline and its processors in the request
  121. body.
  122. [source,console]
  123. ----
  124. POST _ingest/pipeline/_simulate
  125. {
  126. "pipeline" : {
  127. "processors": [
  128. {
  129. "lowercase": {
  130. "field": "my-keyword-field"
  131. }
  132. }
  133. ]
  134. },
  135. "docs": [
  136. {
  137. "_source": {
  138. "my-keyword-field": "FOO"
  139. }
  140. },
  141. {
  142. "_source": {
  143. "my-keyword-field": "BAR"
  144. }
  145. }
  146. ]
  147. }
  148. ----
  149. The API returns transformed documents:
  150. [source,console-result]
  151. ----
  152. {
  153. "docs": [
  154. {
  155. "doc": {
  156. "_index": "_index",
  157. "_id": "_id",
  158. "_source": {
  159. "my-keyword-field": "foo"
  160. },
  161. "_ingest": {
  162. "timestamp": "2099-02-30T22:30:03.187Z"
  163. }
  164. }
  165. },
  166. {
  167. "doc": {
  168. "_index": "_index",
  169. "_id": "_id",
  170. "_source": {
  171. "my-keyword-field": "bar"
  172. },
  173. "_ingest": {
  174. "timestamp": "2099-02-30T22:30:03.188Z"
  175. }
  176. }
  177. }
  178. ]
  179. }
  180. ----
  181. // TESTRESPONSE[s/"2099-02-30T22:30:03.187Z"/$body.docs.0.doc._ingest.timestamp/]
  182. // TESTRESPONSE[s/"2099-02-30T22:30:03.188Z"/$body.docs.1.doc._ingest.timestamp/]
  183. [discrete]
  184. [[add-pipeline-to-indexing-request]]
  185. === Add a pipeline to an indexing request
  186. Use the `pipeline` query parameter to apply a pipeline to documents in
  187. <<docs-index_,individual>> or <<docs-bulk,bulk>> indexing requests.
  188. [source,console]
  189. ----
  190. POST my-data-stream/_doc?pipeline=my-pipeline
  191. {
  192. "@timestamp": "2099-03-07T11:04:05.000Z",
  193. "my-keyword-field": "foo"
  194. }
  195. PUT my-data-stream/_bulk?pipeline=my-pipeline
  196. { "create":{ } }
  197. { "@timestamp": "2099-03-08T11:04:05.000Z", "my-keyword-field" : "foo" }
  198. { "create":{ } }
  199. { "@timestamp": "2099-03-08T11:06:07.000Z", "my-keyword-field" : "bar" }
  200. ----
  201. You can also use the `pipeline` parameter with the <<docs-update-by-query,update
  202. by query>> or <<docs-reindex,reindex>> APIs.
  203. [source,console]
  204. ----
  205. POST my-data-stream/_update_by_query?pipeline=my-pipeline
  206. POST _reindex
  207. {
  208. "source": {
  209. "index": "my-data-stream"
  210. },
  211. "dest": {
  212. "index": "my-new-data-stream",
  213. "op_type": "create",
  214. "pipeline": "my-pipeline"
  215. }
  216. }
  217. ----
  218. // TEST[continued]
  219. [discrete]
  220. [[set-default-pipeline]]
  221. === Set a default pipeline
  222. Use the <<index-default-pipeline,`index.default_pipeline`>> index setting to set
  223. a default pipeline. {es} applies this pipeline if no `pipeline` parameter
  224. is specified.
  225. [discrete]
  226. [[set-final-pipeline]]
  227. === Set a final pipeline
  228. Use the <<index-final-pipeline,`index.final_pipeline`>> index setting to set a
  229. final pipeline. {es} applies this pipeline after the request or default
  230. pipeline, even if neither is specified.
  231. [discrete]
  232. [[access-source-fields]]
  233. === Access source fields in a processor
  234. Processors have read and write access to an incoming document's source fields.
  235. To access a field key in a processor, use its field name. The following `set`
  236. processor accesses `my-long-field`.
  237. [source,console]
  238. ----
  239. PUT _ingest/pipeline/my-pipeline
  240. {
  241. "processors": [
  242. {
  243. "set": {
  244. "field": "my-long-field",
  245. "value": 10
  246. }
  247. }
  248. ]
  249. }
  250. ----
  251. You can also prepend the `_source` prefix.
  252. [source,console]
  253. ----
  254. PUT _ingest/pipeline/my-pipeline
  255. {
  256. "processors": [
  257. {
  258. "set": {
  259. "field": "_source.my-long-field",
  260. "value": 10
  261. }
  262. }
  263. ]
  264. }
  265. ----
  266. Use dot notation to access object fields.
  267. IMPORTANT: If your document contains flattened objects, use the
  268. <<dot-expand-processor,`dot_expander`>> processor to expand them first. Other
  269. ingest processors cannot access flattened objects.
  270. [source,console]
  271. ----
  272. PUT _ingest/pipeline/my-pipeline
  273. {
  274. "processors": [
  275. {
  276. "dot_expander": {
  277. "description": "Expand 'my-object-field.my-property'",
  278. "field": "my-object-field.my-property"
  279. }
  280. },
  281. {
  282. "set": {
  283. "description": "Set 'my-object-field.my-property' to 10",
  284. "field": "my-object-field.my-property",
  285. "value": 10
  286. }
  287. }
  288. ]
  289. }
  290. ----
  291. [[template-snippets]]
  292. To access field values, enclose the field name in double curly brackets `{{ }}`
  293. to create a https://mustache.github.io[Mustache] template snippet. You can use
  294. template snippets to dynamically set field names.
  295. [source,console]
  296. ----
  297. PUT _ingest/pipeline/my-pipeline
  298. {
  299. "processors": [
  300. {
  301. "set": {
  302. "description": "Set dynamic '<service>' field to 'code' value",
  303. "field": "{{service}}",
  304. "value": "{{code}}"
  305. }
  306. }
  307. ]
  308. }
  309. ----
  310. [discrete]
  311. [[access-metadata-fields]]
  312. === Access metadata fields in a processor
  313. Processors can access the following metadata fields by name:
  314. * `_index`
  315. * `_id`
  316. * `_routing`
  317. [source,console]
  318. ----
  319. PUT _ingest/pipeline/my-pipeline
  320. {
  321. "processors" : [
  322. {
  323. "set" : {
  324. "description": "Set '_routing' to 'geoip.country_iso_code' value",
  325. "field": "_routing",
  326. "value": "{{geoip.country_iso_code}}"
  327. }
  328. }
  329. ]
  330. }
  331. ----
  332. Use a Mustache template snippet to access metadata field values. For example,
  333. `{{_routing}}` retrieves a document's routing value.
  334. WARNING: If you <<create-document-ids-automatically,automatically generate>>
  335. document IDs, you cannot use `{{_id}}` in a processor. {es} assigns
  336. auto-generated `_id` values after ingest.
  337. [discrete]
  338. [[access-ingest-metadata]]
  339. === Access ingest metadata in a processor
  340. Ingest processors can add and access ingest metadata using the `_ingest` key.
  341. Unlike source and metadata fields, {es} does not index ingest metadata fields by
  342. default. {es} also allows source fields that start with an `_ingest` key. If
  343. your data includes such source fields, use `_source._ingest` to access them.
  344. Pipelines only create the `_ingest.timestamp` ingest metadata field by default.
  345. This field contains a timestamp of when {es} received the document's indexing
  346. request. To index `_ingest.timestamp` or other ingest metadata fields, use the
  347. `set` processor.
  348. [source,console]
  349. ----
  350. PUT _ingest/pipeline/my-pipeline
  351. {
  352. "processors": [
  353. {
  354. "set": {
  355. "description": "Index the ingest timestamp as 'event.ingested'",
  356. "field": "event.ingested",
  357. "value": "{{_ingest.timestamp}}"
  358. }
  359. }
  360. ]
  361. }
  362. ----
  363. [discrete]
  364. [[handling-pipeline-failures]]
  365. === Handing pipeline failures
  366. A pipeline's processors run sequentially. By default, pipeline processing stops
  367. when one of these processors fails or encounters an error.
  368. To ignore a processor failure and run the pipeline's remaining processors, set
  369. `ignore_failure` to `true`.
  370. [source,console]
  371. ----
  372. PUT _ingest/pipeline/my-pipeline
  373. {
  374. "processors": [
  375. {
  376. "rename": {
  377. "description": "Rename 'provider' to 'cloud.provider'",
  378. "field": "provider",
  379. "target_field": "cloud.provider",
  380. "ignore_failure": true
  381. }
  382. }
  383. ]
  384. }
  385. ----
  386. Use the `on_failure` parameter to specify a list of processors to run
  387. immediately after a processor failure. If `on_failure` is specified, {es}
  388. afterward runs the pipeline's remaining processors, even if the `on_failure`
  389. configuration is empty.
  390. [source,console]
  391. ----
  392. PUT _ingest/pipeline/my-pipeline
  393. {
  394. "processors": [
  395. {
  396. "rename": {
  397. "description": "Rename 'provider' to 'cloud.provider'",
  398. "field": "provider",
  399. "target_field": "cloud.provider",
  400. "on_failure": [
  401. {
  402. "set": {
  403. "description": "Set 'error.message'",
  404. "field": "error.message",
  405. "value": "Field 'provider' does not exist. Cannot rename to 'cloud.provider'",
  406. "override": false
  407. }
  408. }
  409. ]
  410. }
  411. }
  412. ]
  413. }
  414. ----
  415. Nest a list of `on_failure` processors for nested error handling.
  416. [source,console]
  417. ----
  418. PUT _ingest/pipeline/my-pipeline
  419. {
  420. "processors": [
  421. {
  422. "rename": {
  423. "description": "Rename 'provider' to 'cloud.provider'",
  424. "field": "provider",
  425. "target_field": "cloud.provider",
  426. "on_failure": [
  427. {
  428. "set": {
  429. "description": "Set 'error.message'",
  430. "field": "error.message",
  431. "value": "Field 'provider' does not exist. Cannot rename to 'cloud.provider'",
  432. "override": false,
  433. "on_failure": [
  434. {
  435. "set": {
  436. "description": "Set 'error.message.multi'",
  437. "field": "error.message.multi",
  438. "value": "Document encountered multiple ingest errors",
  439. "override": true
  440. }
  441. }
  442. ]
  443. }
  444. }
  445. ]
  446. }
  447. }
  448. ]
  449. }
  450. ----
  451. You can also specify `on_failure` for a pipeline. If a processor without an
  452. `on_failure` value fails, {es} uses this pipeline-level parameter as a fallback.
  453. {es} will not attempt to run the pipeline's remaining processors.
  454. [source,console]
  455. ----
  456. PUT _ingest/pipeline/my-pipeline
  457. {
  458. "processors": [ ... ],
  459. "on_failure": [
  460. {
  461. "set": {
  462. "description": "Index document to 'failed-<index>'",
  463. "field": "_index",
  464. "value": "failed-{{ _index }}"
  465. }
  466. }
  467. ]
  468. }
  469. ----
  470. // TEST[s/\.\.\./{"lowercase": {"field":"my-keyword-field"}}/]
  471. [discrete]
  472. [[conditionally-run-processor]]
  473. === Conditionally run a processor
  474. Each processor supports an optional `if` condition, written as a
  475. {painless}/painless-guide.html[Painless script]. If provided, the processor only
  476. runs when the `if` condition is `true`.
  477. IMPORTANT: `if` condition scripts run in Painless's
  478. {painless}/painless-ingest-processor-context.html[ingest processor context]. In
  479. `if` conditions, `ctx` values are read-only.
  480. [source,console]
  481. ----
  482. PUT _ingest/pipeline/my-pipeline
  483. {
  484. "processors": [
  485. {
  486. "drop": {
  487. "description": "Drop documents with 'network.name' of 'Guest'",
  488. "if": "ctx?.network?.name == 'Guest'"
  489. }
  490. }
  491. ]
  492. }
  493. ----
  494. If the static `script.painless.regex.enabled` cluster setting is enabled, you
  495. can use regular expressions in your `if` condition scripts. For supported
  496. syntax, see the {painless}/painless-regexes.html[Painless regexes]
  497. documentation.
  498. TIP: If possible, avoid using regular expressions. Expensive regular expressions
  499. can slow indexing speeds.
  500. [source,console]
  501. ----
  502. PUT _ingest/pipeline/my-pipeline
  503. {
  504. "processors": [
  505. {
  506. "set": {
  507. "description": "If 'url.scheme' is 'http', set 'url.insecure' to true",
  508. "if": "ctx.url?.scheme =~ /^http[^s]/",
  509. "field": "url.insecure",
  510. "value": true
  511. }
  512. }
  513. ]
  514. }
  515. ----
  516. You must specify `if` conditions as valid JSON on a single line. However, you
  517. can use the {kibana-ref}/console-kibana.html#configuring-console[{kib}
  518. console]'s triple quote syntax to write and debug larger scripts.
  519. TIP: If possible, avoid using complex or expensive `if` condition scripts.
  520. Expensive condition scripts can slow indexing speeds.
  521. [source,console]
  522. ----
  523. PUT _ingest/pipeline/my-pipeline
  524. {
  525. "processors": [
  526. {
  527. "drop": {
  528. "description": "Drop documents that don't contain 'prod' tag",
  529. "if": """
  530. Collection tags = ctx.tags;
  531. if(tags != null){
  532. for (String tag : tags) {
  533. if (tag.toLowerCase().contains('prod')) {
  534. return false;
  535. }
  536. }
  537. }
  538. return true;
  539. """
  540. }
  541. }
  542. ]
  543. }
  544. ----
  545. You can also specify a <<modules-scripting-stored-scripts,stored script>> as the
  546. `if` condition.
  547. [source,console]
  548. ----
  549. PUT _scripts/my-stored-script
  550. {
  551. "script": {
  552. "lang": "painless",
  553. "source": """
  554. Collection tags = ctx.tags;
  555. if(tags != null){
  556. for (String tag : tags) {
  557. if (tag.toLowerCase().contains('prod')) {
  558. return false;
  559. }
  560. }
  561. }
  562. return true;
  563. """
  564. }
  565. }
  566. PUT _ingest/pipeline/my-pipeline
  567. {
  568. "processors": [
  569. {
  570. "drop": {
  571. "description": "If 'url.scheme' is 'http', set 'url.insecure' to true",
  572. "if": { "id": "my-stored-script" }
  573. }
  574. }
  575. ]
  576. }
  577. ----
  578. Incoming documents often contain object fields. If a processor script attempts
  579. to access a field whose parent object does not exist, {es} returns a
  580. `NullPointerException`. To avoid these exceptions, use
  581. {painless}/painless-operators-reference.html#null-safe-operator[null safe
  582. operators], such as `?.`, and write your scripts to be null safe.
  583. For example, `ctx.network?.name.equalsIgnoreCase('Guest')` is not null safe.
  584. `ctx.network?.name` can return null. Rewrite the script as
  585. `'Guest'.equalsIgnoreCase(ctx.network?.name)`, which is null safe because
  586. `Guest` is always non-null.
  587. If you can't rewrite a script to be null safe, include an explicit null check.
  588. [source,console]
  589. ----
  590. PUT _ingest/pipeline/my-pipeline
  591. {
  592. "processors": [
  593. {
  594. "drop": {
  595. "description": "Drop documents that contain 'network.name' of 'Guest'",
  596. "if": "ctx.network?.name != null && ctx.network.name.contains('Guest')"
  597. }
  598. }
  599. ]
  600. }
  601. ----
  602. [discrete]
  603. [[conditionally-apply-pipelines]]
  604. === Conditionally apply pipelines
  605. Combine an `if` condition with the <<pipeline-processor,`pipeline`>> processor
  606. to apply other pipelines to documents based on your criteria. You can use this
  607. pipeline as the <<set-default-pipeline,default pipeline>> in an
  608. <<index-templates,index template>> used to configure multiple data streams or
  609. indices.
  610. [source,console]
  611. ----
  612. PUT _ingest/pipeline/one-pipeline-to-rule-them-all
  613. {
  614. "processors": [
  615. {
  616. "pipeline": {
  617. "description": "If 'service.name' is 'apache_httpd', use 'httpd_pipeline'",
  618. "if": "ctx.service?.name == 'apache_httpd'",
  619. "name": "httpd_pipeline"
  620. }
  621. },
  622. {
  623. "pipeline": {
  624. "description": "If 'service.name' is 'syslog', use 'syslog_pipeline'",
  625. "if": "ctx.service?.name == 'syslog'",
  626. "name": "syslog_pipeline"
  627. }
  628. },
  629. {
  630. "fail": {
  631. "description": "If 'service.name' is not 'apache_httpd' or 'syslog', return a failure message",
  632. "if": "ctx.service?.name != 'apache_httpd' && ctx.service?.name != 'syslog'",
  633. "message": "This pipeline requires service.name to be either `syslog` or `apache_httpd`"
  634. }
  635. }
  636. ]
  637. }
  638. ----
  639. [discrete]
  640. [[get-pipeline-usage-stats]]
  641. === Get pipeline usage statistics
  642. Use the <<cluster-nodes-stats,node stats>> API to get global and per-pipeline
  643. ingest statistics. Use these stats to determine which pipelines run most
  644. frequently or spend the most time processing.
  645. [source,console]
  646. ----
  647. GET _nodes/stats/ingest?filter_path=nodes.*.ingest
  648. ----
  649. include::ingest/common-log-format-example.asciidoc[]
  650. include::ingest/enrich.asciidoc[]
  651. include::ingest/processors.asciidoc[]