Apache Solr open-source search software
SOLR-16646: New function query operator isnan to verify if value is NaN (#1331)
Gabriel Magno04ea6dfe56c9
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex be3eaeb9a56..78c87769d21 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -53,6 +53,8 @@ New Features* SOLR-16608: Ability to compress state.json in Zookeeper (Justin Sweeney via noble)+* SOLR-16646: New function query operator isnan to verify if value is NaN (Gabriel Magno via Kevin Risden)+Improvements---------------------diff --git a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.javaindex 43600f9ac9e..c58b33836d4 100644--- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java@@ -953,6 +953,26 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {}});+ addParser(+ "isnan",+ new ValueSourceParser() {+ @Override+ public ValueSource parse(FunctionQParser fp) throws SyntaxError {+ ValueSource vs = fp.parseValueSource();+ return new SimpleBoolFunction(vs) {+ @Override+ protected String name() {+ return "isnan";+ }++ @Override+ protected boolean func(int doc, FunctionValues vals) throws IOException {+ return Float.isNaN(vals.floatVal(doc));+ }+ };+ }+ });+addParser("not",new ValueSourceParser() {diff --git a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.javaindex c8536033550..c159ca94628 100644--- a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java+++ b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java@@ -1069,6 +1069,16 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {}}+ public void testFuncIsnan() throws Exception {+ SolrQueryRequest req = req("num", "12.3456", "zero", "0");+ try {+ assertFuncEquals(req, "isnan(12.3456)", "isnan(12.3456)", "isnan($num)");+ assertFuncEquals(req, "isnan(div(0,0))", "isnan(div($zero,$zero))");+ } finally {+ req.close();+ }+ }+public void testFuncNot() throws Exception {SolrQueryRequest req = req("myField", "field_b", "myTrue", "true");try {diff --git a/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java b/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.javaindex 56d94a5f2ab..0078ff9b01e 100644--- a/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java+++ b/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java@@ -1170,6 +1170,21 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {req("q", "id:1", "fl", "t:not(testfunc(false)),f:not(true)"),"/response/docs/[0]=={'t':true, 'f':false}");+ // test isnan operator+ assertJQ(+ req(+ "q",+ "id:1",+ "fl",+ "f1:isnan(12.3456)",+ "fl",+ "f2:isnan(0)",+ "fl",+ "t1:isnan(div(0,0))",+ "fl",+ "t2:isnan(sqrt(-1))"),+ "/response/docs/[0]=={'f1':false, 'f2':false, 't1':true, 't2':true}");+// test fields evaluated as booleans in wrapping functionsassertJQ(req("q", "id:1", "fl", "a:not(foo_ti), b:if(foo_tf,'TT','FF'), c:and(true,foo_tf)"),diff --git a/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adoc b/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adocindex be91507526f..8a81bddf8de 100644--- a/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adoc+++ b/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adoc@@ -564,6 +564,16 @@ Returns `true` if any member of the field exists.* `if(lt(ms(mydatefield),315569259747),0.8,1)` translates to this pseudocode: `if mydatefield < 315569259747 then 0.8 else 1`+=== isnan Function+Returns `true` if the value is a float NaN (not a number).++*Syntax Example*++* `isnan(myfield)`: returns `true` for any document having a NaN stored in "myfield".+* `isnan(12.3456)`: returns `false`.+* `isnan(0)`: returns `false`.+* `isnan(div(0,0))`: returns `true` (since 0 divided by 0 is not defined and returns a NaN).+== Example Function QueriesTo give you a better understanding of how function queries can be used in Solr, suppose an index stores the dimensions in meters x,y,z of some hypothetical boxes with arbitrary names stored in field `boxname`.