Apache Lucene open-source search software
SOLR-10613: add flag to FunctionQParser to use FieldNameValueSource place holder when encountering a field name
yonik33178fb5ccd4
diff --git a/solr/core/src/java/org/apache/solr/search/FunctionQParser.java b/solr/core/src/java/org/apache/solr/search/FunctionQParser.javaindex 7e6a706403d..d3a311d936a 100644--- a/solr/core/src/java/org/apache/solr/search/FunctionQParser.java+++ b/solr/core/src/java/org/apache/solr/search/FunctionQParser.java@@ -25,6 +25,7 @@ import org.apache.solr.common.params.SolrParams;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.schema.SchemaField;import org.apache.solr.search.facet.AggValueSource;+import org.apache.solr.search.function.FieldNameValueSource;import java.util.ArrayList;import java.util.List;@@ -33,6 +34,7 @@ public class FunctionQParser extends QParser {public static final int FLAG_CONSUME_DELIMITER = 0x01; // consume delimiter after parsing argpublic static final int FLAG_IS_AGG = 0x02;+ public static final int FLAG_USE_FIELDNAME_SOURCE = 0x04; // When a field name is encountered, use the placeholder FieldNameValueSource instead of resolving to a real ValueSourcepublic static final int FLAG_DEFAULT = FLAG_CONSUME_DELIMITER;/** @lucene.internal */@@ -374,8 +376,13 @@ public class FunctionQParser extends QParser {} else if ("false".equals(id)) {valueSource = new BoolConstValueSource(false);} else {- SchemaField f = req.getSchema().getField(id);- valueSource = f.getType().getValueSource(f, this);+ if ((flags & FLAG_USE_FIELDNAME_SOURCE) != 0) {+ // Don't try to create a ValueSource for the field, just use a placeholder.+ valueSource = new FieldNameValueSource(id);+ } else {+ SchemaField f = req.getSchema().getField(id);+ valueSource = f.getType().getValueSource(f, this);+ }}}diff --git a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.javaindex 7d6d162ce1a..51048d2aa6b 100644--- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java@@ -1017,14 +1017,14 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {addParser("agg_min", new ValueSourceParser() {@Overridepublic ValueSource parse(FunctionQParser fp) throws SyntaxError {- return new MinMaxAgg("min", fp.parseValueSource());+ return new MinMaxAgg("min", fp.parseValueSource(FunctionQParser.FLAG_DEFAULT | FunctionQParser.FLAG_USE_FIELDNAME_SOURCE));}});addParser("agg_max", new ValueSourceParser() {@Overridepublic ValueSource parse(FunctionQParser fp) throws SyntaxError {- return new MinMaxAgg("max", fp.parseValueSource());+ return new MinMaxAgg("max", fp.parseValueSource(FunctionQParser.FLAG_DEFAULT | FunctionQParser.FLAG_USE_FIELDNAME_SOURCE));}});diff --git a/solr/core/src/java/org/apache/solr/search/facet/MinMaxAgg.java b/solr/core/src/java/org/apache/solr/search/facet/MinMaxAgg.javaindex a6d6b9744b5..9569599c458 100644--- a/solr/core/src/java/org/apache/solr/search/facet/MinMaxAgg.java+++ b/solr/core/src/java/org/apache/solr/search/facet/MinMaxAgg.java@@ -28,6 +28,7 @@ import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.LongValues;import org.apache.solr.schema.SchemaField;import org.apache.solr.schema.StrFieldSource;+import org.apache.solr.search.function.FieldNameValueSource;public class MinMaxAgg extends SimpleAggValueSource {final int minmax; // a multiplier to reverse the normal order of compare if this is max instead of min (i.e. max will be -1)@@ -41,9 +42,16 @@ public class MinMaxAgg extends SimpleAggValueSource {public SlotAcc createSlotAcc(FacetContext fcontext, int numDocs, int numSlots) throws IOException {ValueSource vs = getArg();+ SchemaField sf = null;++ if (vs instanceof FieldNameValueSource) {+ String field = ((FieldNameValueSource)vs).getFieldName();+ sf = fcontext.qcontext.searcher().getSchema().getField(field);++ vs = sf.getType().getValueSource(sf, null); // temporary implementation to make existing code work+ }+if (vs instanceof StrFieldSource) {- String field = ((StrFieldSource) vs).getField();- SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(field);if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {if (sf.hasDocValues()) {// dvdiff --git a/solr/core/src/java/org/apache/solr/search/function/FieldNameValueSource.java b/solr/core/src/java/org/apache/solr/search/function/FieldNameValueSource.javanew file mode 100644index 00000000000..c122dbb611e--- /dev/null+++ b/solr/core/src/java/org/apache/solr/search/function/FieldNameValueSource.java@@ -0,0 +1,60 @@+/*+ * 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.solr.search.function;+++import java.io.IOException;+import java.util.Map;++import org.apache.lucene.index.LeafReaderContext;+import org.apache.lucene.queries.function.FunctionValues;+import org.apache.lucene.queries.function.ValueSource;++/** Placeholder value source.+ * @lucene.internal */+public class FieldNameValueSource extends ValueSource {+ private String fieldName;++ public FieldNameValueSource(String fieldName) {+ this.fieldName = fieldName;+ }++ public String getFieldName() {+ return fieldName;+ }++ @Override+ public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {+ throw new UnsupportedOperationException("FieldNameValueSource should not be directly used: " + this);+ }++ @Override+ public boolean equals(Object o) {+ return o instanceof FieldNameValueSource && fieldName.equals(((FieldNameValueSource)o).getFieldName());+ }++ @Override+ public int hashCode() {+ return fieldName.hashCode();+ }++ @Override+ public String description() {+ return "FIELDNAME(" + fieldName + ")";+ }+}