script.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.2/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. if s.typ == "" && s.lang == "" && len(s.params) == 0 {
  75. return s.script, nil
  76. }
  77. source := make(map[string]interface{})
  78. if s.typ == "" {
  79. source["inline"] = s.script
  80. } else {
  81. source[s.typ] = s.script
  82. }
  83. if s.lang != "" {
  84. source["lang"] = s.lang
  85. }
  86. if len(s.params) > 0 {
  87. source["params"] = s.params
  88. }
  89. return source, nil
  90. }
  91. // -- Script Field --
  92. // ScriptField is a single script field.
  93. type ScriptField struct {
  94. FieldName string // name of the field
  95. script *Script
  96. }
  97. // NewScriptField creates and initializes a new ScriptField.
  98. func NewScriptField(fieldName string, script *Script) *ScriptField {
  99. return &ScriptField{FieldName: fieldName, script: script}
  100. }
  101. // Source returns the serializable JSON for the ScriptField.
  102. func (f *ScriptField) Source() (interface{}, error) {
  103. if f.script == nil {
  104. return nil, errors.New("ScriptField expects script")
  105. }
  106. source := make(map[string]interface{})
  107. src, err := f.script.Source()
  108. if err != nil {
  109. return nil, err
  110. }
  111. source["script"] = src
  112. return source, nil
  113. }