Package org.elasticsearch.script
Class Script
java.lang.Object
org.elasticsearch.script.Script
- All Implemented Interfaces:
Writeable
,org.elasticsearch.common.xcontent.ToXContent
,org.elasticsearch.common.xcontent.ToXContentObject
public final class Script extends java.lang.Object implements org.elasticsearch.common.xcontent.ToXContentObject, Writeable
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 emptyMap
to specify no options -
params
-Map
of user-defined parameters; must not benull
, use an emptyMap
to 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
-Map
of user-defined parameters; must not benull
, use an emptyMap
to specify no params
-
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.elasticsearch.common.xcontent.ToXContent
org.elasticsearch.common.xcontent.ToXContent.DelegatingMapParams, org.elasticsearch.common.xcontent.ToXContent.MapParams, org.elasticsearch.common.xcontent.ToXContent.Params
Nested classes/interfaces inherited from interface org.elasticsearch.common.io.stream.Writeable
Writeable.Reader<V>, Writeable.Writer<V>
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CONTENT_TYPE_OPTION
Compiler option forXContentType
used for templates.static java.lang.String
DEFAULT_SCRIPT_LANG
The name of the of the default scripting language.static ScriptType
DEFAULT_SCRIPT_TYPE
The defaultScriptType
.static java.lang.String
DEFAULT_TEMPLATE_LANG
The name of the default template language.static org.elasticsearch.common.ParseField
LANG_PARSE_FIELD
StandardParseField
for lang on the inner level.static org.elasticsearch.common.ParseField
OPTIONS_PARSE_FIELD
StandardParseField
for options on the inner level.static org.elasticsearch.common.ParseField
PARAMS_PARSE_FIELD
StandardParseField
for params on the inner level.static org.elasticsearch.common.ParseField
SCRIPT_PARSE_FIELD
StandardParseField
for outer level of script queries.static org.elasticsearch.common.ParseField
SOURCE_PARSE_FIELD
StandardParseField
for source on the inner level.Fields inherited from interface org.elasticsearch.common.xcontent.ToXContent
EMPTY_PARAMS
-
Constructor Summary
Constructors Constructor Description Script(java.lang.String idOrCode)
Constructor for simple script using the default language and default type.Script(StreamInput in)
Creates aScript
read from an input stream.Script(ScriptType type, java.lang.String lang, java.lang.String idOrCode, java.util.Map<java.lang.String,java.lang.Object> params)
Constructor for a script that does not need to use compiler options.Script(ScriptType type, java.lang.String lang, java.lang.String idOrCode, java.util.Map<java.lang.String,java.lang.String> options, java.util.Map<java.lang.String,java.lang.Object> params)
Constructor for a script that requires the use of compiler options. -
Method Summary
Modifier and Type Method Description static <T> void
declareScript(org.elasticsearch.common.xcontent.AbstractObjectParser<T,?> parser, java.util.function.BiConsumer<T,Script> consumer)
Declare a script field on anObjectParser
with the standard name (script
).static <T> void
declareScript(org.elasticsearch.common.xcontent.AbstractObjectParser<T,?> parser, java.util.function.BiConsumer<T,Script> consumer, org.elasticsearch.common.ParseField parseField)
Declare a script field on anObjectParser
.boolean
equals(java.lang.Object o)
java.lang.String
getIdOrCode()
java.lang.String
getLang()
java.util.Map<java.lang.String,java.lang.String>
getOptions()
java.util.Map<java.lang.String,java.lang.Object>
getParams()
ScriptType
getType()
int
hashCode()
static Script
parse(java.lang.Object config)
static Script
parse(Settings settings)
Parse the script configured in the given settings.static Script
parse(org.elasticsearch.common.xcontent.XContentParser parser)
Convenience method to callparse(XContentParser, String)
using the default scripting language.static Script
parse(org.elasticsearch.common.xcontent.XContentParser parser, java.lang.String defaultLang)
This will parse XContent into aScript
.java.lang.String
toString()
org.elasticsearch.common.xcontent.XContentBuilder
toXContent(org.elasticsearch.common.xcontent.XContentBuilder builder, org.elasticsearch.common.xcontent.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.void
writeTo(StreamOutput out)
Write this into the StreamOutput.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.elasticsearch.common.xcontent.ToXContentObject
isFragment
-
Field Details
-
DEFAULT_SCRIPT_LANG
public static final java.lang.String DEFAULT_SCRIPT_LANGThe name of the of the default scripting language.- See Also:
- Constant Field Values
-
DEFAULT_TEMPLATE_LANG
public static final java.lang.String DEFAULT_TEMPLATE_LANGThe name of the default template language.- See Also:
- Constant Field Values
-
DEFAULT_SCRIPT_TYPE
The defaultScriptType
. -
CONTENT_TYPE_OPTION
public static final java.lang.String CONTENT_TYPE_OPTIONCompiler option forXContentType
used for templates.- See Also:
- Constant Field Values
-
SCRIPT_PARSE_FIELD
public static final org.elasticsearch.common.ParseField SCRIPT_PARSE_FIELDStandardParseField
for outer level of script queries. -
SOURCE_PARSE_FIELD
public static final org.elasticsearch.common.ParseField SOURCE_PARSE_FIELDStandardParseField
for source on the inner level. -
LANG_PARSE_FIELD
public static final org.elasticsearch.common.ParseField LANG_PARSE_FIELDStandardParseField
for lang on the inner level. -
OPTIONS_PARSE_FIELD
public static final org.elasticsearch.common.ParseField OPTIONS_PARSE_FIELDStandardParseField
for options on the inner level. -
PARAMS_PARSE_FIELD
public static final org.elasticsearch.common.ParseField PARAMS_PARSE_FIELDStandardParseField
for params on the inner level.
-
-
Constructor Details
-
Script
public Script(java.lang.String idOrCode)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
public Script(ScriptType type, java.lang.String lang, java.lang.String idOrCode, java.util.Map<java.lang.String,java.lang.Object> params)Constructor for a script that does not need to use compiler options.- Parameters:
type
- TheScriptType
.lang
- The language for thisScript
if theScriptType
isScriptType.INLINE
. ForScriptType.STORED
scripts this should be null, but can be specified to access scripts stored as part of the stored scripts deprecated API.idOrCode
- The id for thisScript
if theScriptType
isScriptType.STORED
. The code for thisScript
if theScriptType
isScriptType.INLINE
.params
- The user-defined params to be bound for script execution.
-
Script
public Script(ScriptType type, java.lang.String lang, java.lang.String idOrCode, java.util.Map<java.lang.String,java.lang.String> options, java.util.Map<java.lang.String,java.lang.Object> params)Constructor for a script that requires the use of compiler options.- Parameters:
type
- TheScriptType
.lang
- The language for thisScript
if theScriptType
isScriptType.INLINE
. ForScriptType.STORED
scripts this should be null, but can be specified to access scripts stored as part of the stored scripts deprecated API.idOrCode
- The id for thisScript
if theScriptType
isScriptType.STORED
. The code for thisScript
if theScriptType
isScriptType.INLINE
.options
- The map of compiler options for thisScript
if theScriptType
isScriptType.INLINE
,null
otherwise.params
- The user-defined params to be bound for script execution.
-
Script
Creates aScript
read from an input stream.- Throws:
java.io.IOException
-
-
Method Details
-
declareScript
public static <T> void declareScript(org.elasticsearch.common.xcontent.AbstractObjectParser<T,?> parser, java.util.function.BiConsumer<T,Script> consumer)Declare a script field on anObjectParser
with 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(org.elasticsearch.common.xcontent.AbstractObjectParser<T,?> parser, java.util.function.BiConsumer<T,Script> consumer, org.elasticsearch.common.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
public static Script parse(org.elasticsearch.common.xcontent.XContentParser parser) throws java.io.IOExceptionConvenience method to callparse(XContentParser, String)
using the default scripting language.- Throws:
java.io.IOException
-
parse
Parse the script configured in the given settings. -
parse
public static Script parse(org.elasticsearch.common.xcontent.XContentParser parser, java.lang.String defaultLang) throws java.io.IOExceptionThis will parse XContent into aScript
. The following formats can be parsed: The simple format defaults to anScriptType.INLINE
with no compiler options or user-defined params: Example:"return Math.log(doc.popularity) * 100;"
The complex format whereScriptType
and 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
- TheXContentParser
to be used.defaultLang
- The default language to use if no language is specified. The default language isn't necessarily the one defined byDEFAULT_SCRIPT_LANG
due to backwards compatibility requirements related to stored queries using previously default languages.- Returns:
- The parsed
Script
. - Throws:
java.io.IOException
-
parse
- Parameters:
config
- The object to parse the script from.- Returns:
- The parsed
Script
. - See Also:
parse(XContentParser, String)
-
writeTo
Description copied from interface:Writeable
Write this into the StreamOutput. -
toXContent
public org.elasticsearch.common.xcontent.XContentBuilder toXContent(org.elasticsearch.common.xcontent.XContentBuilder builder, org.elasticsearch.common.xcontent.ToXContent.Params builderParams) throws java.io.IOExceptionThis 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_OPTION
option is provided and theScriptType.INLINE
is 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:
toXContent
in interfaceorg.elasticsearch.common.xcontent.ToXContent
- Throws:
java.io.IOException
-
getType
- Returns:
- The
ScriptType
for thisScript
.
-
getLang
public java.lang.String getLang()- Returns:
- The language for this
Script
if theScriptType
isScriptType.INLINE
. ForScriptType.STORED
scripts this should be null, but can be specified to access scripts stored as part of the stored scripts deprecated API.
-
getIdOrCode
public java.lang.String getIdOrCode()- Returns:
- The id for this
Script
if theScriptType
isScriptType.STORED
. The code for thisScript
if theScriptType
isScriptType.INLINE
.
-
getOptions
public java.util.Map<java.lang.String,java.lang.String> getOptions()- Returns:
- The map of compiler options for this
Script
if theScriptType
isScriptType.INLINE
,null
otherwise.
-
getParams
public java.util.Map<java.lang.String,java.lang.Object> getParams()- Returns:
- The map of user-defined params for this
Script
.
-
equals
public boolean equals(java.lang.Object o)- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-