transform.asciidoc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. [[mapping-transform]]
  2. == Transform
  3. The document can be transformed before it is indexed by registering a
  4. script in the `transform` element of the mapping. The result of the
  5. transform is indexed but the original source is stored in the `_source`
  6. field. Example:
  7. [source,js]
  8. --------------------------------------------------
  9. {
  10. "example" : {
  11. "transform" : {
  12. "script" : {
  13. "inline": "if (ctx._source['title']?.startsWith('t')) ctx._source['suggest'] = ctx._source['content']",
  14. "params" : {
  15. "variable" : "not used but an example anyway"
  16. },
  17. "lang": "groovy"
  18. }
  19. },
  20. "properties": {
  21. "title": { "type": "string" },
  22. "content": { "type": "string" },
  23. "suggest": { "type": "string" }
  24. }
  25. }
  26. }
  27. --------------------------------------------------
  28. Its also possible to specify multiple transforms:
  29. [source,js]
  30. --------------------------------------------------
  31. {
  32. "example" : {
  33. "transform" : [
  34. {"script": "ctx._source['suggest'] = ctx._source['content']"}
  35. {"script": "ctx._source['foo'] = ctx._source['bar'];"}
  36. ]
  37. }
  38. }
  39. --------------------------------------------------
  40. Because the result isn't stored in the source it can't normally be fetched by
  41. source filtering. It can be highlighted if it is marked as stored.
  42. === Get Transformed
  43. The get endpoint will retransform the source if the `_source_transform`
  44. parameter is set. Example:
  45. [source,sh]
  46. --------------------------------------------------
  47. curl -XGET "http://localhost:9200/test/example/3?pretty&_source_transform"
  48. --------------------------------------------------
  49. The transform is performed before any source filtering but it is mostly
  50. designed to make it easy to see what was passed to the index for debugging.
  51. === Immutable Transformation
  52. Once configured the transform script cannot be modified. This is not
  53. because that is technically impossible but instead because madness lies
  54. down that road.