Skip to content

Commit caf7792

Browse files
authored
Scripting: Rename SearchScript.needsScores to needs_score (#25235)
This commit renames the needsScores method so as to make it automatically generatable, based on the name of the `_score` variable which is available in search scripts. It also adds documentation to ScriptContext to explain the naming and signature of such methods.
1 parent a4471f5 commit caf7792

File tree

12 files changed

+29
-23
lines changed

12 files changed

+29
-23
lines changed

core/src/main/java/org/elasticsearch/common/lucene/search/function/ScriptScoreFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Explanation explainScore(int docId, Explanation subQueryScore) throws IOE
119119

120120
@Override
121121
public boolean needsScores() {
122-
return script.needsScores();
122+
return script.needs_score();
123123
}
124124

125125
@Override

core/src/main/java/org/elasticsearch/script/ScriptContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
* The <i>StatefulFactoryType</i> is an optional class which allows a stateful factory from the
4747
* stateless factory type required by the {@link ScriptService}. If defined, the <i>StatefulFactoryType</i>
4848
* must have a method named {@code newInstance} which returns an instance of <i>InstanceType</i>.
49+
* <p>
50+
* Both the <i>FactoryType</i> and <i>StatefulFactoryType</i> may have abstract methods to indicate
51+
* whether a variable is used in a script. These method should return a {@code boolean} and their name
52+
* should start with {@code needs}, followed by the variable name, with the first letter uppercased.
53+
* For example, to check if a variable {@code doc} is used, a method {@code boolean needsDoc()} should be added.
54+
* If the variable name starts with an underscore, for example, {@code _score}, the needs method would
55+
* be {@code boolean needs_score()}.
4956
*/
5057
public final class ScriptContext<FactoryType> {
5158

core/src/main/java/org/elasticsearch/script/SearchScript.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,11 @@ public Object run() {
144144
/** A factory to construct {@link SearchScript} instances. */
145145
public interface LeafFactory {
146146
SearchScript newInstance(LeafReaderContext ctx) throws IOException;
147+
147148
/**
148-
* Indicates if document scores may be needed by this {@link SearchScript}.
149-
*
150-
* @return {@code true} if scores are needed.
149+
* Return {@code true} if the script needs {@code _score} calculated, or {@code false} otherwise.
151150
*/
152-
boolean needsScores();
151+
boolean needs_score();
153152
}
154153

155154
/** A factory to construct stateful {@link SearchScript} factories for a specific index. */

core/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public SortedBinaryDocValues bytesValues(LeafReaderContext context) throws IOExc
174174

175175
@Override
176176
public boolean needsScores() {
177-
return script.needsScores();
177+
return script.needs_score();
178178
}
179179
}
180180

@@ -246,7 +246,7 @@ public boolean isFloatingPoint() {
246246

247247
@Override
248248
public boolean needsScores() {
249-
return script.needsScores();
249+
return script.needs_score();
250250
}
251251

252252
@Override
@@ -387,7 +387,7 @@ public SortedBinaryDocValues bytesValues(LeafReaderContext context) throws IOExc
387387

388388
@Override
389389
public boolean needsScores() {
390-
return script.needsScores();
390+
return script.needs_score();
391391
}
392392
}
393393

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

407407
@Override
408408
public boolean needsScores() {
409-
return script.needsScores();
409+
return script.needs_score();
410410
}
411411

412412
@Override

core/src/test/java/org/elasticsearch/common/lucene/search/function/ScriptScoreFunctionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public double runAsDouble() {
4444
}
4545

4646
@Override
47-
public boolean needsScores() {
47+
public boolean needs_score() {
4848
return false;
4949
}
5050
});

core/src/test/java/org/elasticsearch/search/functionscore/ExplainableScriptIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public SearchScript newInstance(LeafReaderContext context) throws IOException {
8383
return new MyScript(lookup.doc().getLeafDocLookup(context));
8484
}
8585
@Override
86-
public boolean needsScores() {
86+
public boolean needs_score() {
8787
return false;
8888
}
8989
};

modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionSearchScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ExpressionSearchScript implements SearchScript.LeafFactory {
5454
}
5555

5656
@Override
57-
public boolean needsScores() {
57+
public boolean needs_score() {
5858
return needsScores;
5959
}
6060

modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ private SearchScript.LeafFactory compile(String expression) {
4747
}
4848

4949
public void testNeedsScores() {
50-
assertFalse(compile("1.2").needsScores());
51-
assertFalse(compile("doc['d'].value").needsScores());
52-
assertTrue(compile("1/_score").needsScores());
53-
assertTrue(compile("doc['d'].value * _score").needsScores());
50+
assertFalse(compile("1.2").needs_score());
51+
assertFalse(compile("doc['d'].value").needs_score());
52+
assertTrue(compile("1/_score").needs_score());
53+
assertTrue(compile("doc['d'].value * _score").needs_score());
5454
}
5555

5656
public void testCompileError() {

modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScriptEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public SearchScript newInstance(final LeafReaderContext context) {
134134
return new ScriptImpl(painlessScript, p, lookup, context);
135135
}
136136
@Override
137-
public boolean needsScores() {
137+
public boolean needs_score() {
138138
return painlessScript.needs_score();
139139
}
140140
};

modules/lang-painless/src/test/java/org/elasticsearch/painless/NeedsScoreTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ public void testNeedsScores() {
4444

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

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

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

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

6262
}

0 commit comments

Comments
 (0)