Class Script

java.lang.Object
org.elasticsearch.script.Script
All Implemented Interfaces:
Writeable, org.elasticsearch.xcontent.ToXContent, org.elasticsearch.xcontent.ToXContentObject

public final class Script extends Object implements org.elasticsearch.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 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
    • 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
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.elasticsearch.xcontent.ToXContent

    org.elasticsearch.xcontent.ToXContent.DelegatingMapParams, org.elasticsearch.xcontent.ToXContent.MapParams, org.elasticsearch.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 final String
    Compiler option for XContentType used for templates.
    static final String
    The name of the of the default scripting language.
    static final ScriptType
    The default ScriptType.
    static final String
    The name of the default template language.
    static final org.elasticsearch.xcontent.ParseField
    Standard ParseField for lang on the inner level.
    static final org.elasticsearch.xcontent.ParseField
    Standard ParseField for options on the inner level.
    static final org.elasticsearch.xcontent.ParseField
    Standard ParseField for params on the inner level.
    static final org.elasticsearch.xcontent.ParseField
    Standard ParseField for outer level of script queries.
    static final org.elasticsearch.xcontent.ParseField
    Standard ParseField for source on the inner level.

    Fields inherited from interface org.elasticsearch.xcontent.ToXContent

    EMPTY_PARAMS
  • Constructor Summary

    Constructors
    Constructor
    Description
    Script(String idOrCode)
    Constructor for simple script using the default language and default type.
    Creates a Script read from an input stream.
    Script(ScriptType type, String lang, String idOrCode, Map<String,Object> params)
    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 Type
    Method
    Description
    static <T> void
    declareScript(org.elasticsearch.xcontent.AbstractObjectParser<T,?> parser, BiConsumer<T,Script> consumer)
    Declare a script field on an ObjectParser with the standard name (script).
    static <T> void
    declareScript(org.elasticsearch.xcontent.AbstractObjectParser<T,?> parser, BiConsumer<T,Script> consumer, org.elasticsearch.xcontent.ParseField parseField)
    Declare a script field on an ObjectParser.
    boolean
     
     
     
     
     
     
    int
     
    static Script
    parse(Object config)
    Parse a Script from an Object, that can either be a String or a Map.
    static Script
    parse(Settings settings)
    Parse the script configured in the given settings.
    static Script
    parse(org.elasticsearch.xcontent.XContentParser parser)
    Convenience method to call parse(XContentParser, String) using the default scripting language.
    static Script
    parse(org.elasticsearch.xcontent.XContentParser parser, String defaultLang)
    This will parse XContent into a Script.
     
    org.elasticsearch.xcontent.XContentBuilder
    toXContent(org.elasticsearch.xcontent.XContentBuilder builder, org.elasticsearch.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
    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.xcontent.ToXContentObject

    isFragment
  • Field Details

    • DEFAULT_SCRIPT_LANG

      public static final String DEFAULT_SCRIPT_LANG
      The name of the of the default scripting language.
      See Also:
    • DEFAULT_TEMPLATE_LANG

      public static final String DEFAULT_TEMPLATE_LANG
      The name of the default template language.
      See Also:
    • DEFAULT_SCRIPT_TYPE

      public static final ScriptType DEFAULT_SCRIPT_TYPE
      The default ScriptType.
    • CONTENT_TYPE_OPTION

      public static final String CONTENT_TYPE_OPTION
      Compiler option for XContentType used for templates.
      See Also:
    • SCRIPT_PARSE_FIELD

      public static final org.elasticsearch.xcontent.ParseField SCRIPT_PARSE_FIELD
      Standard ParseField for outer level of script queries.
    • SOURCE_PARSE_FIELD

      public static final org.elasticsearch.xcontent.ParseField SOURCE_PARSE_FIELD
      Standard ParseField for source on the inner level.
    • LANG_PARSE_FIELD

      public static final org.elasticsearch.xcontent.ParseField LANG_PARSE_FIELD
      Standard ParseField for lang on the inner level.
    • OPTIONS_PARSE_FIELD

      public static final org.elasticsearch.xcontent.ParseField OPTIONS_PARSE_FIELD
      Standard ParseField for options on the inner level.
    • PARAMS_PARSE_FIELD

      public static final org.elasticsearch.xcontent.ParseField PARAMS_PARSE_FIELD
      Standard ParseField for params on the inner level.
  • Constructor Details

  • Method Details

    • declareScript

      public static <T> void declareScript(org.elasticsearch.xcontent.AbstractObjectParser<T,?> parser, BiConsumer<T,Script> consumer)
      Declare a script field on an ObjectParser with the standard name (script).
      Type Parameters:
      T - Whatever type the ObjectParser is parsing.
      Parameters:
      parser - the parser itself
      consumer - the consumer for the script
    • declareScript

      public static <T> void declareScript(org.elasticsearch.xcontent.AbstractObjectParser<T,?> parser, BiConsumer<T,Script> consumer, org.elasticsearch.xcontent.ParseField parseField)
      Declare a script field on an ObjectParser.
      Type Parameters:
      T - Whatever type the ObjectParser is parsing.
      Parameters:
      parser - the parser itself
      consumer - the consumer for the script
      parseField - the field name
    • parse

      public static Script parse(org.elasticsearch.xcontent.XContentParser parser) throws IOException
      Convenience method to call parse(XContentParser, String) using the default scripting language.
      Throws:
      IOException
    • parse

      public static Script parse(Settings settings)
      Parse the script configured in the given settings.
    • parse

      public static Script parse(org.elasticsearch.xcontent.XContentParser parser, String defaultLang) throws IOException
      This will parse XContent into a 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. { // 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 - 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 compatibility requirements related to stored queries using previously default languages.
      Returns:
      The parsed Script.
      Throws:
      IOException
    • parse

      public static Script parse(Object config)
      Parse a Script from an Object, that can either be a String or a Map.
      Parameters:
      config - The object to parse the script from.
      Returns:
      The parsed Script.
      See Also:
    • writeTo

      public void writeTo(StreamOutput out) throws IOException
      Description copied from interface: Writeable
      Write this into the StreamOutput.
      Specified by:
      writeTo in interface Writeable
      Throws:
      IOException
    • toXContent

      public org.elasticsearch.xcontent.XContentBuilder toXContent(org.elasticsearch.xcontent.XContentBuilder builder, org.elasticsearch.xcontent.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 the CONTENT_TYPE_OPTION option is provided and the ScriptType.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 interface org.elasticsearch.xcontent.ToXContent
      Throws:
      IOException
    • getType

      public ScriptType getType()
      Returns:
      The ScriptType for this Script.
    • getLang

      public String getLang()
      Returns:
      The language for this Script if the ScriptType is ScriptType.INLINE. For ScriptType.STORED scripts this should be null, but can be specified to access scripts stored as part of the stored scripts deprecated API.
    • getIdOrCode

      public String getIdOrCode()
      Returns:
      The id for this Script if the ScriptType is ScriptType.STORED. The code for this Script if the ScriptType is ScriptType.INLINE.
    • getOptions

      public Map<String,String> getOptions()
      Returns:
      The map of compiler options for this Script if the ScriptType is ScriptType.INLINE, null otherwise.
    • getParams

      public Map<String,Object> getParams()
      Returns:
      The map of user-defined params for this Script.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object