Skip to content

Commit 4cda2cc

Browse files
committed
Scripts should expose whether they use the _score or not
Changes related to elastic#12695
1 parent 6c4ba4a commit 4cda2cc

8 files changed

+78
-30
lines changed

src/main/java/org/elasticsearch/examples/nativescript/script/CosineSimilarityScoreScript.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ public class CosineSimilarityScoreScript extends AbstractSearchScript {
4747

4848
final static public String SCRIPT_NAME = "cosine_sim_script_score";
4949

50-
@Override
51-
public void setScorer(Scorer scorer) {
52-
// ignore
53-
}
54-
5550
/**
5651
* Factory that is registered in
5752
* {@link org.elasticsearch.examples.nativescript.plugin.NativeScriptExamplesPlugin#onModule(org.elasticsearch.script.ScriptModule)}
@@ -70,6 +65,16 @@ public static class Factory implements NativeScriptFactory {
7065
public ExecutableScript newScript(@Nullable Map<String, Object> params) throws ScriptException {
7166
return new CosineSimilarityScoreScript(params);
7267
}
68+
69+
/**
70+
* Indicates if document scores may be needed by the produced scripts.
71+
*
72+
* @return {@code true} if scores are needed.
73+
*/
74+
@Override
75+
public boolean needsScores() {
76+
return false;
77+
}
7378
}
7479

7580
/**

src/main/java/org/elasticsearch/examples/nativescript/script/IsPrimeSearchScript.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public ExecutableScript newScript(@Nullable Map<String, Object> params) {
6060
int certainty = params == null ? 10 : XContentMapValues.nodeIntegerValue(params.get("certainty"), 10);
6161
return new IsPrimeSearchScript(fieldName, certainty);
6262
}
63+
64+
/**
65+
* Indicates if document scores may be needed by the produced scripts.
66+
*
67+
* @return {@code true} if scores are needed.
68+
*/
69+
@Override
70+
public boolean needsScores() {
71+
return false;
72+
}
73+
6374
}
6475

6576
private final String fieldName;

src/main/java/org/elasticsearch/examples/nativescript/script/LanguageModelScoreScript.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ public class LanguageModelScoreScript extends AbstractSearchScript {
4949

5050
final static public String SCRIPT_NAME = "language_model_script_score";
5151

52-
@Override
53-
public void setScorer(Scorer scorer) {
54-
// ignore
55-
}
56-
5752
/**
5853
* Factory that is registered in
5954
* {@link org.elasticsearch.examples.nativescript.plugin.NativeScriptExamplesPlugin#onModule(org.elasticsearch.script.ScriptModule)}
@@ -72,6 +67,13 @@ public static class Factory implements NativeScriptFactory {
7267
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
7368
return new LanguageModelScoreScript(params);
7469
}
70+
71+
72+
@Override
73+
public boolean needsScores() {
74+
return false;
75+
}
76+
7577
}
7678

7779
/**

src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ public ExecutableScript newScript(@Nullable Map<String, Object> params) {
106106
}
107107
return new LookupScript(node.client(), logger, cache, lookupIndex, lookupType, field);
108108
}
109+
110+
/**
111+
* Indicates if document scores may be needed by the produced scripts.
112+
*
113+
* @return {@code true} if scores are needed.
114+
*/
115+
@Override
116+
public boolean needsScores() {
117+
return false;
118+
}
119+
109120
}
110121

111122
private final String lookupIndex;

src/main/java/org/elasticsearch/examples/nativescript/script/PhraseScoreScript.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public class PhraseScoreScript extends AbstractSearchScript {
4444

4545
final static public String SCRIPT_NAME = "phrase_script_score";
4646

47-
@Override
48-
public void setScorer(Scorer scorer) {
49-
// ignore
50-
}
51-
5247
/**
5348
* Factory that is registered in
5449
* {@link org.elasticsearch.examples.nativescript.plugin.NativeScriptExamplesPlugin#onModule(org.elasticsearch.script.ScriptModule)}
@@ -67,6 +62,16 @@ public static class Factory implements NativeScriptFactory {
6762
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
6863
return new PhraseScoreScript(params);
6964
}
65+
66+
/**
67+
* Indicates if document scores may be needed by the produced scripts.
68+
*
69+
* @return {@code true} if scores are needed.
70+
*/
71+
@Override
72+
public boolean needsScores() {
73+
return false;
74+
}
7075
}
7176

7277
/**

src/main/java/org/elasticsearch/examples/nativescript/script/PopularityScoreScriptFactory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public ExecutableScript newScript(@Nullable Map<String, Object> params) {
4242
return new PopularityScoreScript(fieldName);
4343
}
4444

45+
/**
46+
* Indicates if document scores may be needed by the produced scripts.
47+
*
48+
* @return {@code true} if scores are needed.
49+
*/
50+
@Override
51+
public boolean needsScores() {
52+
return true;
53+
}
4554

4655
/**
4756
* This script takes a numeric value from the field specified in the parameter field. And calculates boost

src/main/java/org/elasticsearch/examples/nativescript/script/RandomSortScriptFactory.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public ExecutableScript newScript(@Nullable Map<String, Object> params) {
5555
}
5656
}
5757

58+
/**
59+
* Indicates if document scores may be needed by the produced scripts.
60+
*
61+
* @return {@code true} if scores are needed.
62+
*/
63+
@Override
64+
public boolean needsScores() {
65+
return false;
66+
}
67+
5868
private static class RandomSortScript extends AbstractLongSearchScript {
5969
private final Random random;
6070

@@ -66,11 +76,6 @@ private RandomSortScript() {
6676
public long runAsLong() {
6777
return random.nextLong();
6878
}
69-
70-
@Override
71-
public void setScorer(Scorer scorer) {
72-
// we are not using it - ignore
73-
}
7479
}
7580

7681
private static class PseudoRandomSortScript extends AbstractLongSearchScript {
@@ -100,10 +105,5 @@ public long runAsLong() {
100105
return -1;
101106
}
102107
}
103-
104-
@Override
105-
public void setScorer(Scorer scorer) {
106-
// we are not using it - ignore
107-
}
108108
}
109109
}

src/main/java/org/elasticsearch/examples/nativescript/script/TFIDFScoreScript.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ public class TFIDFScoreScript extends AbstractSearchScript {
4343

4444
final static public String SCRIPT_NAME = "tfidf_script_score";
4545

46-
@Override
47-
public void setScorer(Scorer scorer) {
48-
// ignore
49-
}
50-
5146
/**
5247
* Factory that is registered in
5348
* {@link org.elasticsearch.examples.nativescript.plugin.NativeScriptExamplesPlugin#onModule(org.elasticsearch.script.ScriptModule)}
@@ -66,6 +61,16 @@ public static class Factory implements NativeScriptFactory {
6661
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
6762
return new TFIDFScoreScript(params);
6863
}
64+
65+
/**
66+
* Indicates if document scores may be needed by the produced scripts.
67+
*
68+
* @return {@code true} if scores are needed.
69+
*/
70+
@Override
71+
public boolean needsScores() {
72+
return false;
73+
}
6974
}
7075

7176
/**

0 commit comments

Comments
 (0)