ingest.asciidoc 17 KB

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