transform.asciidoc 2.0 KB

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