Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Explanation explainScore(int docId, Explanation subQueryScore) throws IOE

@Override
public boolean needsScores() {
return script.needsScores();
return script.needs_score();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
* The <i>StatefulFactoryType</i> is an optional class which allows a stateful factory from the
* stateless factory type required by the {@link ScriptService}. If defined, the <i>StatefulFactoryType</i>
* must have a method named {@code newInstance} which returns an instance of <i>InstanceType</i>.
* <p>
* Both the <i>FactoryType</i> and <i>StatefulFactoryType</i> may have abstract methods to indicate
* whether a variable is used in a script. These method should return a {@code boolean} and their name
* should start with {@code needs}, followed by the variable name, with the first letter uppercased.
* For example, to check if a variable {@code doc} is used, a method {@code boolean needsDoc()} should be added.
* If the variable name starts with an underscore, for example, {@code _score}, the needs method would
* be {@code boolean needs_score()}.
*/
public final class ScriptContext<FactoryType> {

Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/org/elasticsearch/script/SearchScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ public Object run() {
/** A factory to construct {@link SearchScript} instances. */
public interface LeafFactory {
SearchScript newInstance(LeafReaderContext ctx) throws IOException;

/**
* Indicates if document scores may be needed by this {@link SearchScript}.
*
* @return {@code true} if scores are needed.
* Return {@code true} if the script needs {@code _score} calculated, or {@code false} otherwise.
*/
boolean needsScores();
boolean needs_score();
}

/** A factory to construct stateful {@link SearchScript} factories for a specific index. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public SortedBinaryDocValues bytesValues(LeafReaderContext context) throws IOExc

@Override
public boolean needsScores() {
return script.needsScores();
return script.needs_score();
}
}

Expand Down Expand Up @@ -246,7 +246,7 @@ public boolean isFloatingPoint() {

@Override
public boolean needsScores() {
return script.needsScores();
return script.needs_score();
}

@Override
Expand Down Expand Up @@ -387,7 +387,7 @@ public SortedBinaryDocValues bytesValues(LeafReaderContext context) throws IOExc

@Override
public boolean needsScores() {
return script.needsScores();
return script.needs_score();
}
}

Expand All @@ -406,7 +406,7 @@ public WithScript(ValuesSource delegate, SearchScript.LeafFactory script) {

@Override
public boolean needsScores() {
return script.needsScores();
return script.needs_score();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public double runAsDouble() {
}

@Override
public boolean needsScores() {
public boolean needs_score() {
return false;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public SearchScript newInstance(LeafReaderContext context) throws IOException {
return new MyScript(lookup.doc().getLeafDocLookup(context));
}
@Override
public boolean needsScores() {
public boolean needs_score() {
return false;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ExpressionSearchScript implements SearchScript.LeafFactory {
}

@Override
public boolean needsScores() {
public boolean needs_score() {
return needsScores;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ private SearchScript.LeafFactory compile(String expression) {
}

public void testNeedsScores() {
assertFalse(compile("1.2").needsScores());
assertFalse(compile("doc['d'].value").needsScores());
assertTrue(compile("1/_score").needsScores());
assertTrue(compile("doc['d'].value * _score").needsScores());
assertFalse(compile("1.2").needs_score());
assertFalse(compile("doc['d'].value").needs_score());
assertTrue(compile("1/_score").needs_score());
assertTrue(compile("doc['d'].value * _score").needs_score());
}

public void testCompileError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public SearchScript newInstance(final LeafReaderContext context) {
return new ScriptImpl(painlessScript, p, lookup, context);
}
@Override
public boolean needsScores() {
public boolean needs_score() {
return painlessScript.needs_score();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public void testNeedsScores() {

SearchScript.Factory factory = service.compile(null, "1.2", SearchScript.CONTEXT, Collections.emptyMap());
SearchScript.LeafFactory ss = factory.newFactory(Collections.emptyMap(), lookup);
assertFalse(ss.needsScores());
assertFalse(ss.needs_score());

factory = service.compile(null, "doc['d'].value", SearchScript.CONTEXT, Collections.emptyMap());
ss = factory.newFactory(Collections.emptyMap(), lookup);
assertFalse(ss.needsScores());
assertFalse(ss.needs_score());

factory = service.compile(null, "1/_score", SearchScript.CONTEXT, Collections.emptyMap());
ss = factory.newFactory(Collections.emptyMap(), lookup);
assertTrue(ss.needsScores());
assertTrue(ss.needs_score());

factory = service.compile(null, "doc['d'].value * _score", SearchScript.CONTEXT, Collections.emptyMap());
ss = factory.newFactory(Collections.emptyMap(), lookup);
assertTrue(ss.needsScores());
assertTrue(ss.needs_score());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public double runAsDouble() {
}

@Override
public boolean needsScores() {
public boolean needs_score() {
return false;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void setDocument(int doc) {
}

@Override
public boolean needsScores() {
public boolean needs_score() {
return true;
}
}
Expand Down