Apache Lucene open-source search software
SOLR-3702: A 'concat' function query to support concatenation of Strings
Shalin Shekhar Mangar78d8d0c53552
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ConcatenateFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ConcatenateFunction.javanew file mode 100644index 00000000000..08fa8e9ef41--- /dev/null+++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ConcatenateFunction.java@@ -0,0 +1,75 @@+package org.apache.lucene.queries.function.valuesource;++/*+ * 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.+ */+++import org.apache.lucene.index.AtomicReaderContext;+import org.apache.lucene.queries.function.FunctionValues;+import org.apache.lucene.queries.function.ValueSource;++import java.io.IOException;+import java.util.List;+import java.util.Map;++/**+ * A {@link ValueSource} implementation which concatenates existing string+ * values from the provided ValueSources into one string.+ */+public class ConcatenateFunction extends MultiFunction {++ public ConcatenateFunction(List<ValueSource> sources) {+ super(sources);+ }++ @Override+ protected String name() {+ return "concat";+ }++ @Override+ public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {+ return new Values(valsArr(sources, context, readerContext)) {++ @Override+ public String strVal(int doc) {+ StringBuilder stringBuilder = new StringBuilder();+ for (FunctionValues functionValues : valsArr) {+ if (functionValues.exists(doc)) {+ stringBuilder.append(functionValues.strVal(doc));+ }+ }+ return stringBuilder.toString();+ }++ @Override+ public Object objectVal(int doc) {+ return strVal(doc);+ }++ @Override+ public boolean exists(int doc) {+ for (FunctionValues vals : valsArr) {+ if (vals.exists(doc)) {+ return true;+ }+ }+ return false;+ }+ };+ }+}diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 6687631b590..83a5cc79abd 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -127,6 +127,9 @@ New Features* SOLR-1871: The 'map' function query accepts a ValueSource as target anddefault value. (Chris Harris, shalin)+* SOLR-3702: A 'concat' function query to support concatenation of Strings.+ (Ted Strauss, Andrey Kudryavtsev via shalin)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.javaindex 4c6cb941a3b..25c52489249 100644--- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java@@ -779,6 +779,13 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {}});+ addParser("concat", new ValueSourceParser() {+ @Override+ public ValueSource parse(FunctionQParser fp) throws SyntaxError {+ return new ConcatenateFunction(fp.parseValueSourceList());+ }+ });+}private static TInfo parseTerm(FunctionQParser fp) throws SyntaxError {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 1ed6dbcf4f3..2dbdde13da1 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@@ -754,4 +754,24 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {}}+ public void testConcatFunction() {+ clearIndex();++ assertU(adoc("id", "1", "field1_t", "buzz", "field2_t", "word"));+ assertU(adoc("id", "2", "field1_t", "1", "field2_t", "2","field4_t", "4"));+ assertU(commit());++ assertQ(req("q","id:1",+ "fl","field:concat(field1_t,field2_t)"),+ " //str[@name='field']='buzzword'");++ assertQ(req("q","id:2",+ "fl","field:concat(field1_t,field2_t, field3_t, field4_t)"),+ " //str[@name='field']='124'");++ assertQ(req("q","id:1",+ "fl","field:def(concat(field3_t, field4_t), 'defValue')"),+ " //str[@name='field']='defValue'");+ }+}