Package org.elasticsearch.script
Class SearchScript
- java.lang.Object
-
- org.elasticsearch.script.SearchScript
-
- All Implemented Interfaces:
ScorerAware
public abstract class SearchScript extends java.lang.Object implements ScorerAware
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
-
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 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()
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)
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
-
-
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.
-
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)
-
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()
-
runAsDouble
public abstract double runAsDouble()
Return the result as a double. This is the main use case of search script, used for document scoring.
-
-