Class StatelessScriptUpdateProcessorFactory

  • All Implemented Interfaces:
    NamedListInitializedPlugin, SolrCoreAware

    public class StatelessScriptUpdateProcessorFactory
    extends UpdateRequestProcessorFactory
    implements SolrCoreAware

    An update request processor factory that enables the use of update processors implemented as scripts which can be loaded by the SolrResourceLoader (usually via the conf dir for the SolrCore).

    This factory requires at least one configuration parameter named script which may be the name of a script file as a string, or an array of multiple script files. If multiple script files are specified, they are executed sequentially in the order specified in the configuration -- as if multiple factories were configured sequentially

    Each script file is expected to declare functions with the same name as each method in UpdateRequestProcessor, using the same arguments. One slight deviation is in the optional return value from these functions: If a script function has a boolean return value, and that value is false then the processor will cleanly terminate processing of the command and return, without forwarding the command on to the next script or processor in the chain. Due to limitations in the ScriptEngine API used by this factory, it can not enforce that all functions exist on initialization, so errors from missing functions will only be generated at runtime when the chain attempts to use them.

    The factory may also be configured with an optional "params" argument, which can be an NamedList (or array, or any other simple Java object) which will be put into the global scope for each script.

    The following variables are define as global variables for each script:

    • req - The SolrQueryRequest
    • rsp - The SolrQueryResponse
    • logger - A Logger that can be used for logging purposes in the script
    • params - The "params" init argument in the factory configuration (if any)

    Internally this update processor uses JDK 6 scripting engine support, and any Invocable implementations of ScriptEngine that can be loaded using the Solr Plugin ClassLoader may be used. By default, the engine used for each script is determined by the filed extension (ie: a *.js file will be treated as a JavaScript script) but this can be overridden by specifying an explicit "engine" name init param for the factory, which identifies a registered name of a ScriptEngineFactory. (This may be particularly useful if multiple engines are available for the same scripting language, and you wish to force the usage of a particular engine because of known quirks)

    A new ScriptEngineManager is created for each SolrQueryRequest defining a "global" scope for the script(s) which is request specific. Separate ScriptEngine instances are then used to evaluate the script files, resulting in an "engine" scope that is specific to each script.

    A simple example...

     <processor class="solr.StatelessScriptUpdateProcessorFactory">
       <str name="script">updateProcessor.js</str>

    A more complex example involving multiple scripts in different languages, and a "params" NamedList that will be put into the global scope of each script...

     <processor class="solr.StatelessScriptUpdateProcessorFactory">
       <arr name="script">
         <str name="script">first-processor.js</str>
         <str name="script"></str>
       <lst name="params">
         <bool name="a_bool_value">true</bool>
         <int name="and_int_value">3</int>

    An example where the script file extensions are ignored, and an explicit script engine is used....

     <processor class="solr.StatelessScriptUpdateProcessorFactory">
       <arr name="script">
         <str name="script">first-processor.txt</str>
         <str name="script">second-processor.txt</str>
       <str name="engine">rhino</str>