From 78d8d0c53552473f52c60cf098ce2fc6d6658365 Mon Sep 17 00:00:00 2001 From: Shalin Shekhar Mangar Date: Fri, 13 Dec 2013 06:40:01 +0000 Subject: [PATCH] SOLR-3702: A 'concat' function query to support concatenation of Strings git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1550656 13f79535-47bb-0310-9956-ffa450edef68 --- .../valuesource/ConcatenateFunction.java | 75 +++++++++++++++++++ solr/CHANGES.txt | 3 + .../apache/solr/search/ValueSourceParser.java | 7 ++ .../search/function/TestFunctionQuery.java | 20 +++++ 4 files changed, 105 insertions(+) create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ConcatenateFunction.java 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.java new file mode 100644 index 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 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.txt index 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 and default 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.java index 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.java index 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'"); + } + }