mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-25 01:19:02 +00:00
Remove LeafSearchScript.runAsFloat(): Nothing calls it.
This commit is contained in:
parent
897fe9108a
commit
8edf213492
@ -37,9 +37,4 @@ public abstract class AbstractDoubleSearchScript extends AbstractSearchScript {
|
||||
public long runAsLong() {
|
||||
return (long) runAsDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return (float) runAsDouble();
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.script;
|
||||
|
||||
/**
|
||||
* A simpler base class instead of {@link AbstractSearchScript} for computations
|
||||
* that return a float number.
|
||||
*/
|
||||
public abstract class AbstractFloatSearchScript extends AbstractSearchScript {
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
return runAsFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract float runAsFloat();
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
return runAsFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
return (long) runAsFloat();
|
||||
}
|
||||
}
|
@ -37,9 +37,4 @@ public abstract class AbstractLongSearchScript extends AbstractSearchScript {
|
||||
public double runAsDouble() {
|
||||
return runAsLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return runAsLong();
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ import java.util.Map;
|
||||
* A base class for any script type that is used during the search process (custom score, aggs, and so on).
|
||||
* <p>
|
||||
* If the script returns a specific numeric type, consider overriding the type specific base classes
|
||||
* such as {@link AbstractDoubleSearchScript}, {@link AbstractFloatSearchScript} and {@link AbstractLongSearchScript}
|
||||
* such as {@link AbstractDoubleSearchScript} and {@link AbstractLongSearchScript}
|
||||
* for better performance.
|
||||
* <p>
|
||||
* The use is required to implement the {@link #run()} method.
|
||||
@ -120,11 +120,6 @@ public abstract class AbstractSearchScript extends AbstractExecutableScript impl
|
||||
lookup.source().setSource(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return ((Number) run()).floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
return ((Number) run()).longValue();
|
||||
|
@ -44,8 +44,6 @@ public interface LeafSearchScript extends ScorerAware, ExecutableScript {
|
||||
setNextVar("_value", value);
|
||||
}
|
||||
|
||||
float runAsFloat();
|
||||
|
||||
long runAsLong();
|
||||
|
||||
double runAsDouble();
|
||||
|
@ -29,7 +29,6 @@ import java.util.Map;
|
||||
*
|
||||
* @see AbstractExecutableScript
|
||||
* @see AbstractSearchScript
|
||||
* @see AbstractFloatSearchScript
|
||||
* @see AbstractLongSearchScript
|
||||
* @see AbstractDoubleSearchScript
|
||||
*/
|
||||
|
@ -20,7 +20,7 @@
|
||||
package org.elasticsearch.common.lucene.search.function;
|
||||
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.elasticsearch.script.AbstractFloatSearchScript;
|
||||
import org.elasticsearch.script.AbstractDoubleSearchScript;
|
||||
import org.elasticsearch.script.LeafSearchScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptException;
|
||||
@ -29,51 +29,37 @@ import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class ScriptScoreFunctionTests extends ESTestCase {
|
||||
/**
|
||||
* Tests https://github.com/elastic/elasticsearch/issues/2426
|
||||
*/
|
||||
public void testScriptScoresReturnsNaN() throws IOException {
|
||||
ScoreFunction scoreFunction = new ScriptScoreFunction(new Script("Float.NaN"), new FloatValueScript(Float.NaN));
|
||||
// script that always returns NaN
|
||||
ScoreFunction scoreFunction = new ScriptScoreFunction(new Script("Double.NaN"), new SearchScript() {
|
||||
@Override
|
||||
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
|
||||
return new AbstractDoubleSearchScript() {
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
return Double.NaN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDocument(int doc) {
|
||||
// do nothing: we are a fake with no lookup
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsScores() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
LeafScoreFunction leafScoreFunction = scoreFunction.getLeafScoreFunction(null);
|
||||
try {
|
||||
ScriptException expected = expectThrows(ScriptException.class, () -> {
|
||||
leafScoreFunction.score(randomInt(), randomFloat());
|
||||
fail("should have thrown an exception about the script_score returning NaN");
|
||||
} catch (ScriptException e) {
|
||||
assertThat("message contains error about script_score returning NaN: " + e.getMessage(),
|
||||
e.getMessage().contains("NaN"), equalTo(true));
|
||||
}
|
||||
}
|
||||
|
||||
static class FloatValueScript implements SearchScript {
|
||||
|
||||
private final float value;
|
||||
|
||||
FloatValueScript(float value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
|
||||
return new AbstractFloatSearchScript() {
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDocument(int doc) {
|
||||
// nothing here
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsScores() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
assertTrue(expected.getMessage().contains("returned NaN"));
|
||||
}
|
||||
}
|
||||
|
@ -455,11 +455,6 @@ public class AvgIT extends AbstractNumericTestCase {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
throw new UnsupportedOperationException();
|
||||
@ -578,11 +573,6 @@ public class AvgIT extends AbstractNumericTestCase {
|
||||
return ((Number) vars.get("_value")).longValue() + inc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
return ((Number) vars.get("_value")).doubleValue() + inc;
|
||||
|
@ -452,11 +452,6 @@ public class SumIT extends AbstractNumericTestCase {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
throw new UnsupportedOperationException();
|
||||
@ -583,11 +578,6 @@ public class SumIT extends AbstractNumericTestCase {
|
||||
return ((Number) vars.get("_value")).longValue() + inc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
return ((Number) vars.get("_value")).doubleValue() + inc;
|
||||
|
@ -314,11 +314,6 @@ public class ValueCountIT extends ESIntegTestCase {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
throw new UnsupportedOperationException();
|
||||
|
@ -75,11 +75,6 @@ public class ScriptValuesTests extends ESTestCase {
|
||||
public void setSource(Map<String, Object> source) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
throw new UnsupportedOperationException();
|
||||
|
@ -12,8 +12,8 @@ To register the actual script you'll need to implement `NativeScriptFactory`
|
||||
to construct the script. The actual script will extend either
|
||||
`AbstractExecutableScript` or `AbstractSearchScript`. The second one is likely
|
||||
the most useful and has several helpful subclasses you can extend like
|
||||
`AbstractLongSearchScript`, `AbstractDoubleSearchScript`, and
|
||||
`AbstractFloatSearchScript`. Finally, your plugin should register the native
|
||||
`AbstractLongSearchScript` and `AbstractDoubleSearchScript`.
|
||||
Finally, your plugin should register the native
|
||||
script by declaring the `onModule(ScriptModule)` method.
|
||||
|
||||
If you squashed the whole thing into one class it'd look like:
|
||||
@ -44,11 +44,11 @@ public class MyNativeScriptPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyNativeScript extends AbstractFloatSearchScript {
|
||||
public static class MyNativeScript extends AbstractDoubleSearchScript {
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
float a = (float) source().get("a");
|
||||
float b = (float) source().get("b");
|
||||
public double runAsDouble() {
|
||||
double a = (double) source().get("a");
|
||||
double b = (double) source().get("b");
|
||||
return a * b;
|
||||
}
|
||||
}
|
||||
|
@ -80,9 +80,6 @@ class ExpressionSearchScript implements SearchScript {
|
||||
@Override
|
||||
public Object run() { return new Double(evaluate()); }
|
||||
|
||||
@Override
|
||||
public float runAsFloat() { return (float)evaluate();}
|
||||
|
||||
@Override
|
||||
public long runAsLong() { return (long)evaluate(); }
|
||||
|
||||
|
@ -316,11 +316,6 @@ public class GroovyScriptEngineService extends AbstractComponent implements Scri
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return ((Number) run()).floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
return ((Number) run()).longValue();
|
||||
|
@ -126,15 +126,6 @@ final class ScriptImpl implements ExecutableScript, LeafSearchScript {
|
||||
return ((Number)run()).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the script.
|
||||
* @return The script result as a float.
|
||||
*/
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return ((Number)run()).floatValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the script.
|
||||
* @return The script result as a long.
|
||||
|
@ -319,11 +319,6 @@ public class JavaScriptScriptEngineService extends AbstractComponent implements
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return ((Number) run()).floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
return ((Number) run()).longValue();
|
||||
|
@ -243,11 +243,6 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
|
||||
return ret.__tojava__(Object.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
return ((Number) run()).floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
return ((Number) run()).longValue();
|
||||
|
Loading…
x
Reference in New Issue
Block a user