Package org.elasticsearch.script
Class SearchScript
- java.lang.Object
-
- org.elasticsearch.script.SearchScript
-
- All Implemented Interfaces:
ScorerAware
,ExecutableScript
public abstract class SearchScript extends java.lang.Object implements ScorerAware, ExecutableScript
A generic script used for per document use cases. Using aSearchScript
works as follows:- Construct a
SearchScript.Factory
usingScriptService.compile(Script, ScriptContext)
- Construct a
SearchScript.LeafFactory
for a an index usingSearchScript.Factory.newFactory(Map, SearchLookup)
- Construct a
SearchScript
for a Lucene segment usingSearchScript.LeafFactory.newInstance(LeafReaderContext)
- Call
setDocument(int)
to indicate which document in the segment the script should be run for next - Call one of the
run
methods:run()
,runAsDouble()
, orrunAsLong()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
SearchScript.Factory
A factory to construct statefulSearchScript
factories for a specific index.static interface
SearchScript.LeafFactory
A factory to constructSearchScript
instances.
-
Field Summary
Fields Modifier and Type Field Description static ScriptContext<SearchScript.Factory>
AGGS_CONTEXT
static ScriptContext<SearchScript.Factory>
CONTEXT
The context used to compileSearchScript
factories.static ScriptContext<SearchScript.Factory>
SCRIPT_SORT_CONTEXT
static ScriptContext<SearchScript.Factory>
TERMS_SET_QUERY_CONTEXT
-
Fields inherited from interface org.elasticsearch.script.ExecutableScript
UPDATE_CONTEXT
-
-
Constructor Summary
Constructors Constructor Description SearchScript(java.util.Map<java.lang.String,java.lang.Object> params, SearchLookup lookup, org.apache.lucene.index.LeafReaderContext leafContext)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description LeafDocLookup
getDoc()
The doc lookup for the Lucene segment this script was created for.protected org.apache.lucene.index.LeafReaderContext
getLeafContext()
The leaf context for the Lucene segment this script was created for.protected LeafSearchLookup
getLeafLookup()
The leaf lookup for the Lucene segment this script was created for.java.util.Map<java.lang.String,java.lang.Object>
getParams()
Return the parameters for this script.double
getScore()
Return the score of the current document.java.lang.Object
run()
Executes the script.abstract double
runAsDouble()
Return the result as a double.long
runAsLong()
Return the result as a long.void
setDocument(int docid)
Set the current document to run the script on next.void
setNextAggregationValue(java.lang.Object value)
Sets per-document aggregation_value
.void
setNextVar(java.lang.String field, java.lang.Object value)
Sets a runtime script parameter.void
setScorer(org.apache.lucene.search.Scorer scorer)
-
-
-
Field Detail
-
CONTEXT
public static final ScriptContext<SearchScript.Factory> CONTEXT
The context used to compileSearchScript
factories.
-
AGGS_CONTEXT
public static final ScriptContext<SearchScript.Factory> AGGS_CONTEXT
-
SCRIPT_SORT_CONTEXT
public static final ScriptContext<SearchScript.Factory> SCRIPT_SORT_CONTEXT
-
TERMS_SET_QUERY_CONTEXT
public static final ScriptContext<SearchScript.Factory> TERMS_SET_QUERY_CONTEXT
-
-
Constructor Detail
-
SearchScript
public SearchScript(java.util.Map<java.lang.String,java.lang.Object> params, SearchLookup lookup, org.apache.lucene.index.LeafReaderContext leafContext)
-
-
Method Detail
-
getParams
public java.util.Map<java.lang.String,java.lang.Object> getParams()
Return the parameters for this script.
-
getLeafLookup
protected final LeafSearchLookup getLeafLookup()
The leaf lookup for the Lucene segment this script was created for.
-
getLeafContext
protected final org.apache.lucene.index.LeafReaderContext getLeafContext()
The leaf context for the Lucene segment this script was created for.
-
getDoc
public final LeafDocLookup getDoc()
The doc lookup for the Lucene segment this script was created for.
-
setDocument
public void setDocument(int docid)
Set the current document to run the script on next.
-
setScorer
public void setScorer(org.apache.lucene.search.Scorer scorer)
- Specified by:
setScorer
in interfaceScorerAware
-
getScore
public double getScore()
Return the score of the current document.
-
setNextAggregationValue
public void setNextAggregationValue(java.lang.Object value)
Sets per-document aggregation_value
.The default implementation just calls
setNextVar("_value", value)
but some engines might want to handle this differently for better performance.- Parameters:
value
- per-document value, typically a String, Long, or Double
-
setNextVar
public void setNextVar(java.lang.String field, java.lang.Object value)
Description copied from interface:ExecutableScript
Sets a runtime script parameter.Note that this method may be slow, involving put() and get() calls to a hashmap or similar.
- Specified by:
setNextVar
in interfaceExecutableScript
- Parameters:
field
- parameter namevalue
- parameter value
-
runAsLong
public long runAsLong()
Return the result as a long. This is used by aggregation scripts over long fields.
-
run
public java.lang.Object run()
Description copied from interface:ExecutableScript
Executes the script.- Specified by:
run
in interfaceExecutableScript
-
runAsDouble
public abstract double runAsDouble()
Return the result as a double. This is the main use case of search script, used for document scoring.
-
-