Remove LeafSearchScript.runAsFloat(): Nothing calls it.

This commit is contained in:
Robert Muir 2016-05-15 22:59:28 -04:00
parent 897fe9108a
commit 8edf213492
17 changed files with 33 additions and 167 deletions
core/src
docs/reference/modules/scripting
modules
lang-expression/src/main/java/org/elasticsearch/script/expression
lang-groovy/src/main/java/org/elasticsearch/script/groovy
lang-painless/src/main/java/org/elasticsearch/painless
plugins
lang-javascript/src/main/java/org/elasticsearch/script/javascript
lang-python/src/main/java/org/elasticsearch/script/python

@ -37,9 +37,4 @@ public abstract class AbstractDoubleSearchScript extends AbstractSearchScript {
public long runAsLong() { public long runAsLong() {
return (long) runAsDouble(); 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() { public double runAsDouble() {
return runAsLong(); 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). * A base class for any script type that is used during the search process (custom score, aggs, and so on).
* <p> * <p>
* If the script returns a specific numeric type, consider overriding the type specific base classes * 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. * for better performance.
* <p> * <p>
* The use is required to implement the {@link #run()} method. * 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); lookup.source().setSource(source);
} }
@Override
public float runAsFloat() {
return ((Number) run()).floatValue();
}
@Override @Override
public long runAsLong() { public long runAsLong() {
return ((Number) run()).longValue(); return ((Number) run()).longValue();

@ -44,8 +44,6 @@ public interface LeafSearchScript extends ScorerAware, ExecutableScript {
setNextVar("_value", value); setNextVar("_value", value);
} }
float runAsFloat();
long runAsLong(); long runAsLong();
double runAsDouble(); double runAsDouble();

@ -29,7 +29,6 @@ import java.util.Map;
* *
* @see AbstractExecutableScript * @see AbstractExecutableScript
* @see AbstractSearchScript * @see AbstractSearchScript
* @see AbstractFloatSearchScript
* @see AbstractLongSearchScript * @see AbstractLongSearchScript
* @see AbstractDoubleSearchScript * @see AbstractDoubleSearchScript
*/ */

@ -20,7 +20,7 @@
package org.elasticsearch.common.lucene.search.function; package org.elasticsearch.common.lucene.search.function;
import org.apache.lucene.index.LeafReaderContext; 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.LeafSearchScript;
import org.elasticsearch.script.Script; import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptException; import org.elasticsearch.script.ScriptException;
@ -29,51 +29,37 @@ import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
public class ScriptScoreFunctionTests extends ESTestCase { public class ScriptScoreFunctionTests extends ESTestCase {
/** /**
* Tests https://github.com/elastic/elasticsearch/issues/2426 * Tests https://github.com/elastic/elasticsearch/issues/2426
*/ */
public void testScriptScoresReturnsNaN() throws IOException { 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); LeafScoreFunction leafScoreFunction = scoreFunction.getLeafScoreFunction(null);
try { ScriptException expected = expectThrows(ScriptException.class, () -> {
leafScoreFunction.score(randomInt(), randomFloat()); leafScoreFunction.score(randomInt(), randomFloat());
fail("should have thrown an exception about the script_score returning NaN"); });
} catch (ScriptException e) { assertTrue(expected.getMessage().contains("returned NaN"));
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;
}
} }
} }

@ -455,11 +455,6 @@ public class AvgIT extends AbstractNumericTestCase {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public float runAsFloat() {
throw new UnsupportedOperationException();
}
@Override @Override
public double runAsDouble() { public double runAsDouble() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -578,11 +573,6 @@ public class AvgIT extends AbstractNumericTestCase {
return ((Number) vars.get("_value")).longValue() + inc; return ((Number) vars.get("_value")).longValue() + inc;
} }
@Override
public float runAsFloat() {
throw new UnsupportedOperationException();
}
@Override @Override
public double runAsDouble() { public double runAsDouble() {
return ((Number) vars.get("_value")).doubleValue() + inc; return ((Number) vars.get("_value")).doubleValue() + inc;

@ -452,11 +452,6 @@ public class SumIT extends AbstractNumericTestCase {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public float runAsFloat() {
throw new UnsupportedOperationException();
}
@Override @Override
public double runAsDouble() { public double runAsDouble() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -583,11 +578,6 @@ public class SumIT extends AbstractNumericTestCase {
return ((Number) vars.get("_value")).longValue() + inc; return ((Number) vars.get("_value")).longValue() + inc;
} }
@Override
public float runAsFloat() {
throw new UnsupportedOperationException();
}
@Override @Override
public double runAsDouble() { public double runAsDouble() {
return ((Number) vars.get("_value")).doubleValue() + inc; return ((Number) vars.get("_value")).doubleValue() + inc;

@ -314,11 +314,6 @@ public class ValueCountIT extends ESIntegTestCase {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public float runAsFloat() {
throw new UnsupportedOperationException();
}
@Override @Override
public double runAsDouble() { public double runAsDouble() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();

@ -75,11 +75,6 @@ public class ScriptValuesTests extends ESTestCase {
public void setSource(Map<String, Object> source) { public void setSource(Map<String, Object> source) {
} }
@Override
public float runAsFloat() {
throw new UnsupportedOperationException();
}
@Override @Override
public long runAsLong() { public long runAsLong() {
throw new UnsupportedOperationException(); 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 to construct the script. The actual script will extend either
`AbstractExecutableScript` or `AbstractSearchScript`. The second one is likely `AbstractExecutableScript` or `AbstractSearchScript`. The second one is likely
the most useful and has several helpful subclasses you can extend like the most useful and has several helpful subclasses you can extend like
`AbstractLongSearchScript`, `AbstractDoubleSearchScript`, and `AbstractLongSearchScript` and `AbstractDoubleSearchScript`.
`AbstractFloatSearchScript`. Finally, your plugin should register the native Finally, your plugin should register the native
script by declaring the `onModule(ScriptModule)` method. script by declaring the `onModule(ScriptModule)` method.
If you squashed the whole thing into one class it'd look like: 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 @Override
public float runAsFloat() { public double runAsDouble() {
float a = (float) source().get("a"); double a = (double) source().get("a");
float b = (float) source().get("b"); double b = (double) source().get("b");
return a * b; return a * b;
} }
} }

@ -80,9 +80,6 @@ class ExpressionSearchScript implements SearchScript {
@Override @Override
public Object run() { return new Double(evaluate()); } public Object run() { return new Double(evaluate()); }
@Override
public float runAsFloat() { return (float)evaluate();}
@Override @Override
public long runAsLong() { return (long)evaluate(); } 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 @Override
public long runAsLong() { public long runAsLong() {
return ((Number) run()).longValue(); return ((Number) run()).longValue();

@ -126,15 +126,6 @@ final class ScriptImpl implements ExecutableScript, LeafSearchScript {
return ((Number)run()).doubleValue(); 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. * Run the script.
* @return The script result as a long. * @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 @Override
public long runAsLong() { public long runAsLong() {
return ((Number) run()).longValue(); return ((Number) run()).longValue();

@ -243,11 +243,6 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
return ret.__tojava__(Object.class); return ret.__tojava__(Object.class);
} }
@Override
public float runAsFloat() {
return ((Number) run()).floatValue();
}
@Override @Override
public long runAsLong() { public long runAsLong() {
return ((Number) run()).longValue(); return ((Number) run()).longValue();