Package org.elasticsearch.script
Class Script
java.lang.Object
org.elasticsearch.script.Script
- All Implemented Interfaces:
Writeable,ToXContent,ToXContentObject
Script represents used-defined input that can be used to
compile and execute a script from the ScriptService
based on the ScriptType.
There are three types of scripts specified by ScriptType.
The following describes the expected parameters for each type of script:
-
ScriptType.INLINE-
lang- specifies the language, defaults toDEFAULT_SCRIPT_LANG -
idOrCode- specifies the code to be compiled, must not benull -
options- specifies the compiler options for this script; must not benull, use an emptyMapto specify no options -
params-Mapof user-defined parameters; must not benull, use an emptyMapto specify no params
-
-
ScriptType.STORED-
lang- the language will be specified when storing the script, so this should benull -
idOrCode- specifies the id of the stored script to be looked up, must not benull -
options- compiler options will be specified when a stored script is stored, so they have no meaning here and must benull -
params-Mapof user-defined parameters; must not benull, use an emptyMapto specify no params
-
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.elasticsearch.xcontent.ToXContent
ToXContent.DelegatingMapParams, ToXContent.MapParams, ToXContent.ParamsNested classes/interfaces inherited from interface org.elasticsearch.common.io.stream.Writeable
Writeable.Reader<V>, Writeable.Writer<V> -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringCompiler option forXContentTypeused for templates.static final StringThe name of the of the default scripting language.static final ScriptTypeThe defaultScriptType.static final StringThe name of the default template language.static final ParseFieldStandardParseFieldfor lang on the inner level.static final ParseFieldStandardParseFieldfor options on the inner level.static final ParseFieldStandardParseFieldfor params on the inner level.static final ParseFieldStandardParseFieldfor outer level of script queries.static final ParseFieldStandardParseFieldfor source on the inner level.Fields inherited from interface org.elasticsearch.xcontent.ToXContent
EMPTY_PARAMS -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for simple script using the default language and default type.Script(StreamInput in) Creates aScriptread from an input stream.Constructor for a script that does not need to use compiler options.Script(ScriptType type, String lang, String idOrCode, Map<String, String> options, Map<String, Object> params) Constructor for a script that requires the use of compiler options. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voiddeclareScript(AbstractObjectParser<T, ?> parser, BiConsumer<T, Script> consumer) Declare a script field on anObjectParserwith the standard name (script).static <T> voiddeclareScript(AbstractObjectParser<T, ?> parser, BiConsumer<T, Script> consumer, ParseField parseField) Declare a script field on anObjectParser.booleangetLang()getType()inthashCode()static Scriptstatic ScriptParse the script configured in the given settings.static Scriptparse(XContentParser parser) Convenience method to callparse(XContentParser, String)using the default scripting language.static Scriptparse(XContentParser parser, String defaultLang) This will parse XContent into aScript.toString()toXContent(XContentBuilder builder, ToXContent.Params builderParams) This will build scripts into the following XContent structure:{ "<(id, source)>" : "<idOrCode>", "lang" : "<lang>", "options" : { "option0" : "<option0>", "option1" : "<option1>", ... }, "params" : { "param0" : "<param0>", "param1" : "<param1>", ... } }Example:{ "source" : "return Math.log(doc.popularity) * params.multiplier;", "lang" : "painless", "params" : { "multiplier" : 100.0 } }Note that lang, options, and params will only be included if there have been any specified.voidwriteTo(StreamOutput out) Write this into the StreamOutput.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.elasticsearch.xcontent.ToXContentObject
isFragment
-
Field Details
-
DEFAULT_SCRIPT_LANG
The name of the of the default scripting language.- See Also:
-
DEFAULT_TEMPLATE_LANG
The name of the default template language.- See Also:
-
DEFAULT_SCRIPT_TYPE
The defaultScriptType. -
CONTENT_TYPE_OPTION
Compiler option forXContentTypeused for templates.- See Also:
-
SCRIPT_PARSE_FIELD
StandardParseFieldfor outer level of script queries. -
SOURCE_PARSE_FIELD
StandardParseFieldfor source on the inner level. -
LANG_PARSE_FIELD
StandardParseFieldfor lang on the inner level. -
OPTIONS_PARSE_FIELD
StandardParseFieldfor options on the inner level. -
PARAMS_PARSE_FIELD
StandardParseFieldfor params on the inner level.
-
-
Constructor Details
-
Script
Constructor for simple script using the default language and default type.- Parameters:
idOrCode- The id or code to use dependent on the default script type.
-
Script
Constructor for a script that does not need to use compiler options.- Parameters:
type- TheScriptType.lang- The language for thisScriptif theScriptTypeisScriptType.INLINE. ForScriptType.STOREDscripts this should be null, but can be specified to access scripts stored as part of the stored scripts deprecated API.idOrCode- The id for thisScriptif theScriptTypeisScriptType.STORED. The code for thisScriptif theScriptTypeisScriptType.INLINE.params- The user-defined params to be bound for script execution.
-
Script
public Script(ScriptType type, String lang, String idOrCode, Map<String, String> options, Map<String, Object> params) Constructor for a script that requires the use of compiler options.- Parameters:
type- TheScriptType.lang- The language for thisScriptif theScriptTypeisScriptType.INLINE. ForScriptType.STOREDscripts this should be null, but can be specified to access scripts stored as part of the stored scripts deprecated API.idOrCode- The id for thisScriptif theScriptTypeisScriptType.STORED. The code for thisScriptif theScriptTypeisScriptType.INLINE.options- The map of compiler options for thisScriptif theScriptTypeisScriptType.INLINE,nullotherwise.params- The user-defined params to be bound for script execution.
-
Script
Creates aScriptread from an input stream.- Throws:
IOException
-
-
Method Details
-
declareScript
public static <T> void declareScript(AbstractObjectParser<T, ?> parser, BiConsumer<T, Script> consumer) Declare a script field on anObjectParserwith the standard name (script).- Type Parameters:
T- Whatever type the ObjectParser is parsing.- Parameters:
parser- the parser itselfconsumer- the consumer for the script
-
declareScript
public static <T> void declareScript(AbstractObjectParser<T, ?> parser, BiConsumer<T, Script> consumer, ParseField parseField) Declare a script field on anObjectParser.- Type Parameters:
T- Whatever type the ObjectParser is parsing.- Parameters:
parser- the parser itselfconsumer- the consumer for the scriptparseField- the field name
-
parse
Convenience method to callparse(XContentParser, String)using the default scripting language.- Throws:
IOException
-
parse
Parse the script configured in the given settings. -
parse
This will parse XContent into aScript. The following formats can be parsed: The simple format defaults to anScriptType.INLINEwith no compiler options or user-defined params: Example:"return Math.log(doc.popularity) * 100;"The complex format whereScriptTypeand idOrCode are required while lang, options and params are not required.{ // Exactly one of "id" or "source" must be specified "id" : "<id>", // OR "source": "<source>", "lang" : "<lang>", "options" : { "option0" : "<option0>", "option1" : "<option1>", ... }, "params" : { "param0" : "<param0>", "param1" : "<param1>", ... } }Example:{ "source" : "return Math.log(doc.popularity) * params.multiplier", "lang" : "painless", "params" : { "multiplier" : 100.0 } }This also handles templates in a special way. If a complexly formatted query is specified as another complex JSON object the query is assumed to be a template, and the format will be preserved.{ "source" : { "query" : ... }, "lang" : "<lang>", "options" : { "option0" : "<option0>", "option1" : "<option1>", ... }, "params" : { "param0" : "<param0>", "param1" : "<param1>", ... } }- Parameters:
parser- TheXContentParserto be used.defaultLang- The default language to use if no language is specified. The default language isn't necessarily the one defined byDEFAULT_SCRIPT_LANGdue to backwards compatibility requirements related to stored queries using previously default languages.- Returns:
- The parsed
Script. - Throws:
IOException
-
parse
- Parameters:
config- The object to parse the script from.- Returns:
- The parsed
Script. - See Also:
-
writeTo
Description copied from interface:WriteableWrite this into the StreamOutput.- Specified by:
writeToin interfaceWriteable- Throws:
IOException
-
toXContent
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params builderParams) throws IOException This will build scripts into the following XContent structure:{ "<(id, source)>" : "<idOrCode>", "lang" : "<lang>", "options" : { "option0" : "<option0>", "option1" : "<option1>", ... }, "params" : { "param0" : "<param0>", "param1" : "<param1>", ... } }Example:{ "source" : "return Math.log(doc.popularity) * params.multiplier;", "lang" : "painless", "params" : { "multiplier" : 100.0 } }Note that lang, options, and params will only be included if there have been any specified. This also handles templates in a special way. If theCONTENT_TYPE_OPTIONoption is provided and theScriptType.INLINEis specified then the template will be preserved as a raw field.{ "source" : { "query" : ... }, "lang" : "<lang>", "options" : { "option0" : "<option0>", "option1" : "<option1>", ... }, "params" : { "param0" : "<param0>", "param1" : "<param1>", ... } }- Specified by:
toXContentin interfaceToXContent- Throws:
IOException
-
getType
- Returns:
- The
ScriptTypefor thisScript.
-
getLang
- Returns:
- The language for this
Scriptif theScriptTypeisScriptType.INLINE. ForScriptType.STOREDscripts this should be null, but can be specified to access scripts stored as part of the stored scripts deprecated API.
-
getIdOrCode
- Returns:
- The id for this
Scriptif theScriptTypeisScriptType.STORED. The code for thisScriptif theScriptTypeisScriptType.INLINE.
-
getOptions
- Returns:
- The map of compiler options for this
Scriptif theScriptTypeisScriptType.INLINE,nullotherwise.
-
getParams
- Returns:
- The map of user-defined params for this
Script.
-
equals
-
hashCode
public int hashCode() -
toString
-