script.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import "errors"
  6. // Script holds all the paramaters necessary to compile or find in cache
  7. // and then execute a script.
  8. //
  9. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting.html
  10. // for details of scripting.
  11. type Script struct {
  12. script string
  13. typ string
  14. lang string
  15. params map[string]interface{}
  16. }
  17. // NewScript creates and initializes a new Script.
  18. func NewScript(script string) *Script {
  19. return &Script{
  20. script: script,
  21. typ: "", // default type is "inline"
  22. params: make(map[string]interface{}),
  23. }
  24. }
  25. // NewScriptInline creates and initializes a new Script of type "inline".
  26. func NewScriptInline(script string) *Script {
  27. return NewScript(script).Type("inline")
  28. }
  29. // NewScriptId creates and initializes a new Script of type "id".
  30. func NewScriptId(script string) *Script {
  31. return NewScript(script).Type("id")
  32. }
  33. // NewScriptFile creates and initializes a new Script of type "file".
  34. func NewScriptFile(script string) *Script {
  35. return NewScript(script).Type("file")
  36. }
  37. // Script is either the cache key of the script to be compiled/executed
  38. // or the actual script source code for inline scripts. For indexed
  39. // scripts this is the id used in the request. For file scripts this is
  40. // the file name.
  41. func (s *Script) Script(script string) *Script {
  42. s.script = script
  43. return s
  44. }
  45. // Type sets the type of script: "inline", "id", or "file".
  46. func (s *Script) Type(typ string) *Script {
  47. s.typ = typ
  48. return s
  49. }
  50. // Lang sets the language of the script. Permitted values are "groovy",
  51. // "expression", "mustache", "mvel" (default), "javascript", "python".
  52. // To use certain languages, you need to configure your server and/or
  53. // add plugins. See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/modules-scripting.html
  54. // for details.
  55. func (s *Script) Lang(lang string) *Script {
  56. s.lang = lang
  57. return s
  58. }
  59. // Param adds a key/value pair to the parameters that this script will be executed with.
  60. func (s *Script) Param(name string, value interface{}) *Script {
  61. if s.params == nil {
  62. s.params = make(map[string]interface{})
  63. }
  64. s.params[name] = value
  65. return s
  66. }
  67. // Params sets the map of parameters this script will be executed with.
  68. func (s *Script) Params(params map[string]interface{}) *Script {
  69. s.params = params
  70. return s
  71. }
  72. // Source returns the JSON serializable data for this Script.
  73. func (s *Script) Source() (interface{}, error) {
  74. source := make(map[string]interface{})
  75. // In 5.5 and earlier, the type can "inline", "id", or "file".
  76. // In 5.6+, the type can be "source", "id", or "file".
  77. // So we use "inline" here to keep compatibility with 5.5 and earlier.
  78. // Notice that this will trigger a deprecation warning in 5.6.
  79. if s.typ == "" || s.typ == "inline" {
  80. source["inline"] = s.script
  81. } else {
  82. // "id" or "file"
  83. source[s.typ] = s.script
  84. }
  85. if s.lang != "" {
  86. source["lang"] = s.lang
  87. }
  88. if len(s.params) > 0 {
  89. source["params"] = s.params
  90. }
  91. return source, nil
  92. }
  93. // -- Script Field --
  94. // ScriptField is a single script field.
  95. type ScriptField struct {
  96. FieldName string // name of the field
  97. script *Script
  98. }
  99. // NewScriptField creates and initializes a new ScriptField.
  100. func NewScriptField(fieldName string, script *Script) *ScriptField {
  101. return &ScriptField{FieldName: fieldName, script: script}
  102. }
  103. // Source returns the serializable JSON for the ScriptField.
  104. func (f *ScriptField) Source() (interface{}, error) {
  105. if f.script == nil {
  106. return nil, errors.New("ScriptField expects script")
  107. }
  108. source := make(map[string]interface{})
  109. src, err := f.script.Source()
  110. if err != nil {
  111. return nil, err
  112. }
  113. source["script"] = src
  114. return source, nil
  115. }