public final class Script extends java.lang.Object implements 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 to DEFAULT_SCRIPT_LANG
idOrCode - specifies the code to be compiled, must not be null
options - specifies the compiler options for this script; must not be null,
use an empty Map to specify no options
params - Map of user-defined parameters; must not be null,
use an empty Map to specify no params
ScriptType.STORED
lang - the language will be specified when storing the script, so this should
be null; however, this can be specified to look up a stored
script as part of the deprecated API
idOrCode - specifies the id of the stored script to be looked up, must not be null
options - compiler options will be specified when a stored script is stored,
so they have no meaning here and must be null
params - Map of user-defined parameters; must not be null,
use an empty Map to specify no params
ScriptType.FILE
lang - specifies the language for look up, defaults to DEFAULT_SCRIPT_LANG
idOrCode - specifies the id of the file script to be looked up, must not be null
options - compiler options will be specified when a file script is loaded,
so they have no meaning here and must be null
params - Map of user-defined parameters; must not be null,
use an empty Map to specify no params
ToXContent.DelegatingMapParams, ToXContent.MapParams, ToXContent.ParamsWriteable.Reader<V>, Writeable.Writer<V>| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CONTENT_TYPE_OPTION
Compiler option for
XContentType 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 default
ScriptType. |
static java.lang.String |
DEFAULT_TEMPLATE_LANG
The name of the default template language.
|
static ParseField |
LANG_PARSE_FIELD
Standard
ParseField for lang on the inner level. |
static ParseField |
OPTIONS_PARSE_FIELD
Standard
ParseField for options on the inner level. |
static ParseField |
PARAMS_PARSE_FIELD
Standard
ParseField for params on the inner level. |
static ParseField |
SCRIPT_PARSE_FIELD
Standard
ParseField for outer level of script queries. |
static Version |
V_5_1_0_UNRELEASED |
EMPTY_PARAMS| Constructor and Description |
|---|
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.
|
Script(StreamInput in)
Creates a
Script read from an input stream. |
Script(java.lang.String idOrCode)
Constructor for simple script using the default language and default type.
|
| Modifier and Type | Method and Description |
|---|---|
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(XContentParser parser)
Convenience method to call
parse(XContentParser, String)
using the default scripting language. |
static Script |
parse(XContentParser parser,
java.lang.String defaultLang)
This will parse XContent into a
Script. |
java.lang.String |
toString() |
XContentBuilder |
toXContent(XContentBuilder builder,
ToXContent.Params builderParams)
This will build scripts into the following XContent structure:
{@code
{
"
|
void |
writeTo(StreamOutput out)
Write this into the StreamOutput.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitisFragmentpublic static final Version V_5_1_0_UNRELEASED
public static final java.lang.String DEFAULT_SCRIPT_LANG
public static final java.lang.String DEFAULT_TEMPLATE_LANG
public static final ScriptType DEFAULT_SCRIPT_TYPE
ScriptType.public static final java.lang.String CONTENT_TYPE_OPTION
XContentType used for templates.public static final ParseField SCRIPT_PARSE_FIELD
ParseField for outer level of script queries.public static final ParseField LANG_PARSE_FIELD
ParseField for lang on the inner level.public static final ParseField OPTIONS_PARSE_FIELD
ParseField for options on the inner level.public static final ParseField PARAMS_PARSE_FIELD
ParseField for params on the inner level.public Script(java.lang.String idOrCode)
idOrCode - The id or code to use dependent on the default script type.public Script(ScriptType type, java.lang.String lang, java.lang.String idOrCode, java.util.Map<java.lang.String,java.lang.Object> params)
type - The ScriptType.lang - The language for this Script if the ScriptType is ScriptType.INLINE or
ScriptType.FILE. For ScriptType.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 this Script if the ScriptType is ScriptType.FILE or ScriptType.STORED.
The code for this Script if the ScriptType is ScriptType.INLINE.params - The user-defined params to be bound for script execution.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)
type - The ScriptType.lang - The language for this Script if the ScriptType is ScriptType.INLINE or
ScriptType.FILE. For ScriptType.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 this Script if the ScriptType is ScriptType.FILE or ScriptType.STORED.
The code for this Script if the ScriptType is ScriptType.INLINE.options - The map of compiler options for this Script if the ScriptType
is ScriptType.INLINE, null otherwise.params - The user-defined params to be bound for script execution.public Script(StreamInput in) throws java.io.IOException
Script read from an input stream.java.io.IOExceptionpublic static Script parse(XContentParser parser) throws java.io.IOException
parse(XContentParser, String)
using the default scripting language.java.io.IOExceptionpublic static Script parse(XContentParser parser, java.lang.String defaultLang) throws java.io.IOException
Script. The following formats can be parsed:
The simple format defaults to an ScriptType.INLINE with no compiler options or user-defined params:
Example:
"return Math.log(doc.popularity) * 100;"
The complex format where ScriptType and idOrCode are required while lang, options and params are not required.
{
"<type (inline, stored, file)>" : "<idOrCode>",
"lang" : "<lang>",
"options" : {
"option0" : "<option0>",
"option1" : "<option1>",
...
},
"params" : {
"param0" : "<param0>",
"param1" : "<param1>",
...
}
}
Example:
{
"inline" : "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.
{
"inline" : { "query" : ... },
"lang" : "<lang>",
"options" : {
"option0" : "<option0>",
"option1" : "<option1>",
...
},
"params" : {
"param0" : "<param0>",
"param1" : "<param1>",
...
}
}
parser - The XContentParser to be used.defaultLang - The default language to use if no language is specified. The default language isn't necessarily
the one defined by DEFAULT_SCRIPT_LANG due to backwards compatiblity requirements
related to stored queries using previously default languauges.Script.java.io.IOExceptionpublic void writeTo(StreamOutput out) throws java.io.IOException
Writeablepublic XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params builderParams) throws java.io.IOException
{
"<type (inline, stored, file)>" : "<idOrCode>",
"lang" : "<lang>",
"options" : {
"option0" : "<option0>",
"option1" : "<option1>",
...
},
"params" : {
"param0" : "<param0>",
"param1" : "<param1>",
...
}
}
Example:
{
"inline" : "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 the CONTENT_TYPE_OPTION option
is provided and the ScriptType.INLINE is specified then the template will be preserved as a raw field.
{
"inline" : { "query" : ... },
"lang" : "<lang>",
"options" : {
"option0" : "<option0>",
"option1" : "<option1>",
...
},
"params" : {
"param0" : "<param0>",
"param1" : "<param1>",
...
}
}
toXContent in interface ToXContentjava.io.IOExceptionpublic ScriptType getType()
ScriptType for this Script.public java.lang.String getLang()
Script if the ScriptType is ScriptType.INLINE or
ScriptType.FILE. For ScriptType.STORED scripts this should be null, but can
be specified to access scripts stored as part of the stored scripts deprecated API.public java.lang.String getIdOrCode()
Script if the ScriptType is ScriptType.FILE or ScriptType.STORED.
The code for this Script if the ScriptType is ScriptType.INLINE.public java.util.Map<java.lang.String,java.lang.String> getOptions()
Script if the ScriptType
is ScriptType.INLINE, null otherwise.public java.util.Map<java.lang.String,java.lang.Object> getParams()
Script.public boolean equals(java.lang.Object o)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Object