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