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 aSearchScriptworks as follows:- Construct a
SearchScript.FactoryusingScriptService.compile(Script, ScriptContext) - Construct a
SearchScript.LeafFactoryfor a an index usingSearchScript.Factory.newFactory(Map, SearchLookup) - Construct a
SearchScriptfor 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
runmethods:run(),runAsDouble(), orrunAsLong()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSearchScript.FactoryA factory to construct statefulSearchScriptfactories for a specific index.static interfaceSearchScript.LeafFactoryA factory to constructSearchScriptinstances.
-
Field Summary
Fields Modifier and Type Field Description static ScriptContext<SearchScript.Factory>AGGS_CONTEXTstatic ScriptContext<SearchScript.Factory>CONTEXTThe context used to compileSearchScriptfactories.static ScriptContext<SearchScript.Factory>SCRIPT_SORT_CONTEXTstatic 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 LeafDocLookupgetDoc()The doc lookup for the Lucene segment this script was created for.protected org.apache.lucene.index.LeafReaderContextgetLeafContext()The leaf context for the Lucene segment this script was created for.protected LeafSearchLookupgetLeafLookup()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.doublegetScore()Return the score of the current document.java.lang.Objectrun()Executes the script.abstract doublerunAsDouble()Return the result as a double.longrunAsLong()Return the result as a long.voidsetDocument(int docid)Set the current document to run the script on next.voidsetNextAggregationValue(java.lang.Object value)Sets per-document aggregation_value.voidsetNextVar(java.lang.String field, java.lang.Object value)Sets a runtime script parameter.voidsetScorer(org.apache.lucene.search.Scorer scorer)
-
-
-
Field Detail
-
CONTEXT
public static final ScriptContext<SearchScript.Factory> CONTEXT
The context used to compileSearchScriptfactories.
-
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:
setScorerin 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:ExecutableScriptSets a runtime script parameter.Note that this method may be slow, involving put() and get() calls to a hashmap or similar.
- Specified by:
setNextVarin 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:ExecutableScriptExecutes the script.- Specified by:
runin 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.
-
-