transform.asciidoc 2.0 KB

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