SOLR-11758: Fixed FloatDocValues.boolVal to correctly return true for all values != 0.0F

This commit is contained in:
Chris Hostetter 2018-01-06 12:54:05 -07:00
parent 0d55811774
commit d03cb44de7
4 changed files with 97 additions and 3 deletions

View File

@ -124,6 +124,9 @@ Bug Fixes
* LUCENE-8077: Fixed bug in how CheckIndex verifies doc-value iterators.
(Xiaoshan Sun via Adrien Grand)
* SOLR-11758: Fixed FloatDocValues.boolVal to correctly return true for all values != 0.0F
(Munendra S N via hossman)
Other
* LUCENE-8111: IndexOrDocValuesQuery Javadoc references outdated method name.

View File

@ -57,6 +57,11 @@ public abstract class FloatDocValues extends FunctionValues {
return (long)floatVal(doc);
}
@Override
public boolean boolVal(int doc) throws IOException {
return floatVal(doc) != 0.0f;
}
@Override
public double doubleVal(int doc) throws IOException {
return (double)floatVal(doc);

View File

@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.lucene.queries.function.docvalues;
import java.io.IOException;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.util.LuceneTestCase;
/**
* <p>
* Sanity check that {@link FunctionValues#boolVal} behaves as expected for trivial subclasses of the various
* (Numeric) DocValue implementations.
* </p>
* <p>
* Any "non-zero" value should result in "true"
* </p>
*/
public class TestBoolValOfNumericDVs extends LuceneTestCase {
public void test() throws IOException {
check(true);
check(false);
}
public void check(final boolean expected) throws IOException {
// create "constant" based instances of each superclass that should returned the expected value based on
// the constant used
final FunctionValues[] values = new FunctionValues[] {
new FloatDocValues(null) {
@Override
public float floatVal(int doc) throws IOException {
return expected ? Float.MIN_VALUE : 0.0F;
}
},
new DoubleDocValues(null) {
@Override
public double doubleVal(int doc) throws IOException {
return expected ? Double.MIN_VALUE : 0.0D;
}
},
new IntDocValues(null) {
@Override
public int intVal(int doc) throws IOException {
return expected ? 1 : 0;
}
},
new LongDocValues(null) {
@Override
public long longVal(int doc) throws IOException {
return expected ? 1L : 0L;
}
},
};
for (FunctionValues fv : values) {
// docId is irrelevant since all of our FunctionValues return a constant value.
assertEquals(fv.getClass().getSuperclass().toString(), expected, fv.boolVal(123));
}
}
}

View File

@ -800,7 +800,7 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
public void testBooleanFunctions() throws Exception {
clearIndex();
assertU(adoc("id", "1", "text", "hello", "foo_s","A", "foo_ti", "0", "foo_tl","0"));
assertU(adoc("id", "1", "text", "hello", "foo_s","A", "foo_ti", "0", "foo_tl","0", "foo_tf", "0.00001"));
assertU(adoc("id", "2" , "foo_ti","10", "foo_tl","11"));
assertU(commit());
@ -819,6 +819,10 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
// test if()
assertJQ(req("q", "id:1", "fl", "a1:if(true,'A','B')", "fl","b1:if(false,'A',testfunc('B'))")
, "/response/docs/[0]=={'a1':'A', 'b1':'B'}");
// queries with positive scores < 1 should still evaluate to 'true' in boolean context
assertJQ(req("q", "id:1", "nested", "*:*^=0.00001",
"fl", "a1:if(query($nested),'A','B')", "fl","b1:if(not(query($nested)),'A','B')")
, "/response/docs/[0]=={'a1':'A', 'b1':'B'}");
// test boolean operators
assertJQ(req("q", "id:1", "fl", "t1:and(testfunc(true),true)", "fl","f1:and(true,false)", "fl","f2:and(false,true)", "fl","f3:and(false,false)")
@ -830,6 +834,12 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
assertJQ(req("q", "id:1", "fl", "t:not(testfunc(false)),f:not(true)")
, "/response/docs/[0]=={'t':true, 'f':false}");
// test fields evaluated as booleans in wrapping functions
assertJQ(req("q", "id:1", "fl", "a:not(foo_ti), b:if(foo_tf,'TT','FF'), c:and(true,foo_tf)")
, "/response/docs/[0]=={'a':true, 'b':'TT', 'c':true}");
assertJQ(req("q", "id:2", "fl", "a:not(foo_ti), b:if(foo_tf,'TT','FF'), c:and(true,foo_tf)")
, "/response/docs/[0]=={'a':false, 'b':'FF', 'c':false}");
// def(), the default function that returns the first value that exists
assertJQ(req("q", "id:1", "fl", "x:def(id,testfunc(123)), y:def(foo_f,234.0)")
@ -838,8 +848,8 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
, "/response/docs/[0]=={'x':'A', 'y':'W'}");
// test constant conversion to boolean
assertJQ(req("q", "id:1", "fl", "a:not(0), b:not(1), c:not(0.0), d:not(1.1), e:not('A')")
, "/response/docs/[0]=={'a':true, 'b':false, 'c':true, 'd':false, 'e':false}");
assertJQ(req("q", "id:1", "fl", "a:not(0), b:not(1), c:not(0.0), d:not(1.1), e:not('A'), f:not(0.001)")
, "/response/docs/[0]=={'a':true, 'b':false, 'c':true, 'd':false, 'e':false, 'f':false}");
}