Merge branch 'master' into types

This commit is contained in:
Jack Conradson 2016-05-16 14:22:59 -07:00
commit 3f23186712
18 changed files with 97 additions and 182 deletions

View File

@ -37,9 +37,4 @@ public abstract class AbstractDoubleSearchScript extends AbstractSearchScript {
public long runAsLong() {
return (long) runAsDouble();
}
@Override
public float runAsFloat() {
return (float) runAsDouble();
}
}

View File

@ -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();
}
}

View File

@ -37,9 +37,4 @@ public abstract class AbstractLongSearchScript extends AbstractSearchScript {
public double runAsDouble() {
return runAsLong();
}
@Override
public float runAsFloat() {
return runAsLong();
}
}

View File

@ -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();

View File

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

View File

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

View File

@ -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"));
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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(); }

View File

@ -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();

View File

@ -646,50 +646,106 @@ public final class Definition {
}
private void addElements() {
addMethod("Object", "toString", null, false, stringType, new Type[] {}, null, null);
addMethod("Object", "equals", null, false, booleanType, new Type[] {objectType}, null, null);
addMethod("Object", "hashCode", null, false, intType, new Type[] {}, null, null);
addMethod("Object", "toString", null, false, stringType, new Type[] {}, null, null);
addMethod("def", "toString", null, false, stringType, new Type[] {}, null, null);
addMethod("def", "equals", null, false, booleanType, new Type[] {objectType}, null, null);
addMethod("def", "hashCode", null, false, intType, new Type[] {}, null, null);
addMethod("def", "toString", null, false, stringType, new Type[] {}, null, null);
addConstructor("Boolean", "new", new Type[] {booleanType}, null);
addMethod("Boolean", "valueOf", null, true, booleanobjType, new Type[] {booleanType}, null, null);
addMethod("Boolean", "booleanValue", null, false, booleanType, new Type[] {}, null, null);
addMethod("Boolean", "compare", null, true, intType, new Type[] {booleanType,booleanType}, null, null);
addMethod("Boolean", "compareTo", null, false, intType, new Type[] {booleanobjType}, null, null);
addMethod("Boolean", "parseBoolean", null, true, booleanType, new Type[] {stringType}, null, null);
addMethod("Boolean", "valueOf", null, true, booleanobjType, new Type[] {booleanType}, null, null);
addField("Boolean", "FALSE", null, true, booleanobjType, null);
addField("Boolean", "TRUE", null, true, booleanobjType, null);
addConstructor("Byte", "new", new Type[] {byteType}, null);
addMethod("Byte", "compare", null, true, intType, new Type[] {byteType,byteType}, null, null);
addMethod("Byte", "compareTo", null, false, intType, new Type[] {byteobjType}, null, null);
addMethod("Byte", "parseByte", null, true, byteType, new Type[] {stringType}, null, null);
addMethod("Byte", "valueOf", null, true, byteobjType, new Type[] {byteType}, null, null);
addField("Byte", "MIN_VALUE", null, true, byteType, null);
addField("Byte", "MAX_VALUE", null, true, byteType, null);
addConstructor("Short", "new", new Type[] {shortType}, null);
addMethod("Short", "compare", null, true, intType, new Type[] {shortType,shortType}, null, null);
addMethod("Short", "compareTo", null, false, intType, new Type[] {shortobjType}, null, null);
addMethod("Short", "parseShort", null, true, shortType, new Type[] {stringType}, null, null);
addMethod("Short", "valueOf", null, true, shortobjType, new Type[] {shortType}, null, null);
addField("Short", "MIN_VALUE", null, true, shortType, null);
addField("Short", "MAX_VALUE", null, true, shortType, null);
addConstructor("Character", "new", new Type[] {charType}, null);
addMethod("Character", "valueOf", null, true, charobjType, new Type[] {charType}, null, null);
addMethod("Character", "charCount", null, true, intType, new Type[] {intType}, null, null);
addMethod("Character", "charValue", null, false, charType, new Type[] {}, null, null);
addMethod("Character", "compare", null, true, intType, new Type[] {charType,charType}, null, null);
addMethod("Character", "compareTo", null, false, intType, new Type[] {charobjType}, null, null);
addMethod("Character", "digit", null, true, intType, new Type[] {intType,intType}, null, null);
addMethod("Character", "forDigit", null, true, charType, new Type[] {intType,intType}, null, null);
addMethod("Character", "getName", null, true, stringType, new Type[] {intType}, null, null);
addMethod("Character", "getNumericValue", null, true, intType, new Type[] {intType}, null, null);
addMethod("Character", "isAlphabetic", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isDefined", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isDigit", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isIdeographic", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isLetter", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isLetterOrDigit", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isLowerCase", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isMirrored", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isSpaceChar", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isTitleCase", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isUpperCase", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "isWhitespace", null, true, booleanType, new Type[] {intType}, null, null);
addMethod("Character", "valueOf", null, true, charobjType, new Type[] {charType}, null, null);
addField("Character", "MIN_VALUE", null, true, charType, null);
addField("Character", "MAX_VALUE", null, true, charType, null);
addConstructor("Integer", "new", new Type[] {intType}, null);
addMethod("Integer", "compare", null, true, intType, new Type[] {intType,intType}, null, null);
addMethod("Integer", "compareTo", null, false, intType, new Type[] {intobjType}, null, null);
addMethod("Integer", "min", null, true, intType, new Type[] {intType,intType}, null, null);
addMethod("Integer", "max", null, true, intType, new Type[] {intType,intType}, null, null);
addMethod("Integer", "parseInt", null, true, intType, new Type[] {stringType}, null, null);
addMethod("Integer", "signum", null, true, intType, new Type[] {intType}, null, null);
addMethod("Integer", "toHexString", null, true, stringType, new Type[] {intType}, null, null);
addMethod("Integer", "valueOf", null, true, intobjType, new Type[] {intType}, null, null);
addField("Integer", "MIN_VALUE", null, true, intType, null);
addField("Integer", "MAX_VALUE", null, true, intType, null);
addConstructor("Long", "new", new Type[] {longType}, null);
addMethod("Long", "compare", null, true, intType, new Type[] {longType,longType}, null, null);
addMethod("Long", "compareTo", null, false, intType, new Type[] {longobjType}, null, null);
addMethod("Long", "min", null, true, longType, new Type[] {longType,longType}, null, null);
addMethod("Long", "max", null, true, longType, new Type[] {longType,longType}, null, null);
addMethod("Long", "parseLong", null, true, longType, new Type[] {stringType}, null, null);
addMethod("Long", "signum", null, true, intType, new Type[] {longType}, null, null);
addMethod("Long", "toHexString", null, true, stringType, new Type[] {longType}, null, null);
addMethod("Long", "valueOf", null, true, longobjType, new Type[] {longType}, null, null);
addField("Long", "MIN_VALUE", null, true, longType, null);
addField("Long", "MAX_VALUE", null, true, longType, null);
addConstructor("Float", "new", new Type[] {floatType}, null);
addMethod("Float", "compare", null, true, intType, new Type[] {floatType,floatType}, null, null);
addMethod("Float", "compareTo", null, false, intType, new Type[] {floatobjType}, null, null);
addMethod("Float", "min", null, true, floatType, new Type[] {floatType,floatType}, null, null);
addMethod("Float", "max", null, true, floatType, new Type[] {floatType,floatType}, null, null);
addMethod("Float", "parseFloat", null, true, floatType, new Type[] {stringType}, null, null);
addMethod("Float", "toHexString", null, true, stringType, new Type[] {floatType}, null, null);
addMethod("Float", "valueOf", null, true, floatobjType, new Type[] {floatType}, null, null);
addField("Float", "MIN_VALUE", null, true, floatType, null);
addField("Float", "MAX_VALUE", null, true, floatType, null);
addConstructor("Double", "new", new Type[] {doubleType}, null);
addMethod("Double", "compare", null, true, intType, new Type[] {doubleType,doubleType}, null, null);
addMethod("Double", "compareTo", null, false, intType, new Type[] {doubleobjType}, null, null);
addMethod("Double", "min", null, true, doubleType, new Type[] {doubleType,doubleType}, null, null);
addMethod("Double", "max", null, true, doubleType, new Type[] {doubleType,doubleType}, null, null);
addMethod("Double", "parseDouble", null, true, doubleType, new Type[] {stringType}, null, null);
addMethod("Double", "toHexString", null, true, stringType, new Type[] {doubleType}, null, null);
addMethod("Double", "valueOf", null, true, doubleobjType, new Type[] {doubleType}, null, null);
addField("Double", "MIN_VALUE", null, true, doubleType, null);
addField("Double", "MAX_VALUE", null, true, doubleType, null);
@ -830,9 +886,6 @@ public final class Definition {
addMethod("Utility", "StringToCharacter", null, true, charobjType, new Type[] {stringType}, null, null);
addMethod("Math", "abs", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "fabs", "abs", true, floatType, new Type[] {floatType}, null, null);
addMethod("Math", "labs", "abs", true, longType, new Type[] {longType}, null, null);
addMethod("Math", "iabs", "abs", true, intType, new Type[] {intType}, null, null);
addMethod("Math", "acos", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "asin", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "atan", null, true, doubleType, new Type[] {doubleType}, null, null);
@ -848,14 +901,8 @@ public final class Definition {
addMethod("Math", "log", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "log10", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "log1p", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "max", "max", true, doubleType, new Type[] {doubleType, doubleType}, null, null);
addMethod("Math", "fmax", "max", true, floatType, new Type[] {floatType, floatType}, null, null);
addMethod("Math", "lmax", "max", true, longType, new Type[] {longType, longType}, null, null);
addMethod("Math", "imax", "max", true, intType, new Type[] {intType, intType}, null, null);
addMethod("Math", "min", "min", true, doubleType, new Type[] {doubleType, doubleType}, null, null);
addMethod("Math", "fmin", "min", true, floatType, new Type[] {floatType, floatType}, null, null);
addMethod("Math", "lmin", "min", true, longType, new Type[] {longType, longType}, null, null);
addMethod("Math", "imin", "min", true, intType, new Type[] {intType, intType}, null, null);
addMethod("Math", "max", null, true, doubleType, new Type[] {doubleType, doubleType}, null, null);
addMethod("Math", "min", null, true, doubleType, new Type[] {doubleType, doubleType}, null, null);
addMethod("Math", "pow", null, true, doubleType, new Type[] {doubleType, doubleType}, null, null);
addMethod("Math", "random", null, true, doubleType, new Type[] {}, null, null);
addMethod("Math", "rint", null, true, doubleType, new Type[] {doubleType}, null, null);
@ -867,6 +914,8 @@ public final class Definition {
addMethod("Math", "tanh", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "toDegrees", null, true, doubleType, new Type[] {doubleType}, null, null);
addMethod("Math", "toRadians", null, true, doubleType, new Type[] {doubleType}, null, null);
addField("Math", "E", null, true, doubleType, null);
addField("Math", "PI", null, true, doubleType, null);
addMethod("Def", "DefToboolean", null, true, booleanType, new Type[] {defType}, null, null);
addMethod("Def", "DefTobyte", null, true, byteType, new Type[] {defType}, null, null);

View File

@ -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.

View File

@ -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();

View File

@ -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();