diff --git a/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/MaxFloatFunction.java b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/MaxFloatFunction.java new file mode 100644 index 00000000000..3f43494e1a7 --- /dev/null +++ b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/MaxFloatFunction.java @@ -0,0 +1,50 @@ +/** + * 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.valuesource; + +import org.apache.lucene.queries.function.DocValues; +import org.apache.lucene.queries.function.ValueSource; + +/** + * MaxFloatFunction returns the max of it's components. + */ +public class MaxFloatFunction extends MultiFloatFunction { + public MaxFloatFunction(ValueSource[] sources) { + super(sources); + } + + @Override + protected String name() { + return "max"; + } + + @Override + protected float func(int doc, DocValues[] valsArr) { + boolean first = true; + float val = 0.0f; + for (DocValues vals : valsArr) { + if (first) { + first = false; + val = vals.floatVal(doc); + } else { + val = Math.max(vals.floatVal(doc),val); + } + } + return val; + } +} diff --git a/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/MinFloatFunction.java b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/MinFloatFunction.java new file mode 100644 index 00000000000..8453c3830fa --- /dev/null +++ b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/MinFloatFunction.java @@ -0,0 +1,50 @@ +/** + * 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.valuesource; + +import org.apache.lucene.queries.function.DocValues; +import org.apache.lucene.queries.function.ValueSource; + +/** + * MinFloatFunction returns the min of it's components. + */ +public class MinFloatFunction extends MultiFloatFunction { + public MinFloatFunction(ValueSource[] sources) { + super(sources); + } + + @Override + protected String name() { + return "min"; + } + + @Override + protected float func(int doc, DocValues[] valsArr) { + boolean first = true; + float val = 0.0f; + for (DocValues vals : valsArr) { + if (first) { + first = false; + val = vals.floatVal(doc); + } else { + val = Math.min(vals.floatVal(doc),val); + } + } + return val; + } +} diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 07cf70a63bf..551131e4e9e 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -31,7 +31,7 @@ Apache UIMA 2.3.1 Apache ZooKeeper 3.3.3 -Upgrading from Solr 3.4-dev +Upgrading from Solr 3.5-dev ---------------------- * The Lucene index format has changed and as a result, once you upgrade, @@ -398,6 +398,9 @@ Bug Fixes * LUCENE-3457: Upgrade commons-compress to 1.2 (Doron Cohen) +* SOLR-2757: min() and max() functions now support an arbitrary number of + ValueSources (Bill Bell via hossman) + ================== 3.4.0 ================== Upgrading from Solr 3.3 diff --git a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java index a74976401d6..8e62a2d26cc 100755 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -483,19 +483,20 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin { return Math.atan2(a.doubleVal(doc), b.doubleVal(doc)); } }); - addParser(new Double2Parser("max") { + addParser("max", new ValueSourceParser() { @Override - public double func(int doc, DocValues a, DocValues b) { - return Math.max(a.doubleVal(doc), b.doubleVal(doc)); + public ValueSource parse(FunctionQParser fp) throws ParseException { + List sources = fp.parseValueSourceList(); + return new MaxFloatFunction(sources.toArray(new ValueSource[sources.size()])); } }); - addParser(new Double2Parser("min") { + addParser("min", new ValueSourceParser() { @Override - public double func(int doc, DocValues a, DocValues b) { - return Math.min(a.doubleVal(doc), b.doubleVal(doc)); + public ValueSource parse(FunctionQParser fp) throws ParseException { + List sources = fp.parseValueSourceList(); + return new MinFloatFunction(sources.toArray(new ValueSource[sources.size()])); } }); - addParser("sqedist", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws ParseException { 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.java index 774cbf703d6..b5065d4d424 100755 --- a/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java +++ b/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java @@ -571,7 +571,9 @@ public class TestFunctionQuery extends SolrTestCaseJ4 { dofunc("sqrt(9)", Math.sqrt(9)); dofunc("cbrt(8)", Math.cbrt(8)); dofunc("max(0,1)", Math.max(0,1)); + dofunc("max(10,3,8,7,5,4)", Math.max(Math.max(Math.max(Math.max(Math.max(10,3),8),7),5),4)); dofunc("min(0,1)", Math.min(0,1)); + dofunc("min(10,3,8,7,5,4)", Math.min(Math.min(Math.min(Math.min(Math.min(10,3),8),7),5),4)); dofunc("log(100)", Math.log10(100)); dofunc("ln(3)", Math.log(3)); dofunc("exp(1)", Math.exp(1));