SOLR-3702: concat(..) function query

This commit is contained in:
Mikhail Khludnev 2017-06-03 19:35:29 +03:00
parent 99093caec5
commit 357f4dfb18
9 changed files with 52 additions and 6 deletions

View File

@ -276,6 +276,8 @@ New Features
* SOLR-10479: Adds support for HttpShardHandlerFactory.loadBalancerRequests(MinimumAbsolute|MaximumFraction)
configuration. (Ramsey Haddad, Daniel Collins, Christine Poerschke)
* SOLR-3702: concat(...) function query (Andrey Kudryavtsev via Mikhail Khludnev)
Bug Fixes
----------------------
* SOLR-10723 JSON Facet API: resize() implemented incorrectly for CountSlotAcc, HllAgg.NumericAcc

View File

@ -36,7 +36,6 @@ import org.apache.solr.analytics.request.ExpressionRequest;
import org.apache.solr.analytics.util.AnalyticsParams;
import org.apache.solr.analytics.util.valuesource.AbsoluteValueDoubleFunction;
import org.apache.solr.analytics.util.valuesource.AddDoubleFunction;
import org.apache.solr.analytics.util.valuesource.ConcatStringFunction;
import org.apache.solr.analytics.util.valuesource.ConstDateSource;
import org.apache.solr.analytics.util.valuesource.ConstDoubleSource;
import org.apache.solr.analytics.util.valuesource.ConstStringSource;
@ -64,6 +63,7 @@ import org.apache.solr.schema.TrieDoubleField;
import org.apache.solr.schema.TrieFloatField;
import org.apache.solr.schema.TrieIntField;
import org.apache.solr.schema.TrieLongField;
import org.apache.solr.search.function.ConcatStringFunction;
import org.apache.solr.util.DateMathParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -22,6 +22,7 @@ import java.util.Set;
import org.apache.solr.common.params.FacetParams.FacetRangeInclude;
import org.apache.solr.common.params.FacetParams.FacetRangeOther;
import org.apache.solr.search.function.ConcatStringFunction;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@ -106,7 +107,7 @@ public interface AnalyticsParams {
//Strings
final static String CONSTANT_STRING = "const_str";
final static String REVERSE = "rev";
final static String CONCATENATE = "concat";
final static String CONCATENATE = ConcatStringFunction.NAME;
public static final Set<String> STRING_OPERATION_SET = Collections.unmodifiableSet(Sets.newLinkedHashSet(Lists.newArrayList(CONSTANT_STRING,REVERSE,CONCATENATE)));
// Field Source Wrappers

View File

@ -64,6 +64,7 @@ import org.apache.solr.search.facet.SumsqAgg;
import org.apache.solr.search.facet.UniqueAgg;
import org.apache.solr.search.facet.VarianceAgg;
import org.apache.solr.search.function.CollapseScoreFunction;
import org.apache.solr.search.function.ConcatStringFunction;
import org.apache.solr.search.function.OrdFieldSource;
import org.apache.solr.search.function.ReverseOrdFieldSource;
import org.apache.solr.search.function.SolrComparisonBoolFunction;
@ -932,6 +933,14 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {
}
});
addParser("concat", new ValueSourceParser() {
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
List<ValueSource> sources = fp.parseValueSourceList();
return new ConcatStringFunction(sources.toArray(new ValueSource[sources.size()]));
}
});
addParser("agg", new ValueSourceParser() {
@Override

View File

@ -14,20 +14,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.analytics.util.valuesource;
package org.apache.solr.search.function;
import java.io.IOException;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.solr.analytics.util.AnalyticsParams;
/**
* <code>ConcatStringFunction</code> concatenates the string values of its
* components in the order given.
*/
public class ConcatStringFunction extends MultiStringFunction {
public final static String NAME = AnalyticsParams.CONCATENATE;
public final static String NAME = "concat";
public ConcatStringFunction(ValueSource[] sources) {
super(sources);

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.analytics.util.valuesource;
package org.apache.solr.search.function;
import java.io.IOException;
import java.util.Arrays;

View File

@ -862,6 +862,21 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {
}
}
public void testFuncConcat() throws Exception {
SolrQueryRequest req = req("myField","bar_f","myOtherField","bar_t");
try {
assertFuncEquals(req,
"concat(bar_f,bar_t)",
"concat($myField,bar_t)",
"concat(bar_f,$myOtherField)",
"concat($myField,$myOtherField)");
} finally {
req.close();
}
}
public void testFuncSingleValueMathFuncs() throws Exception {
SolrQueryRequest req = req("myVal","45", "myField","foo_i");
for (final String func : new String[] {"abs","rad","deg","sqrt","cbrt",

View File

@ -773,6 +773,25 @@ 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,field4_t)"),
" //str[@name='field']='124'");
assertQ(req("q","id:1",
"fl","field:def(concat(field3_t, field4_t), 'defValue')"),
" //str[@name='field']='defValue'");
}
@Test
public void testPseudoFieldFunctions() throws Exception {

View File

@ -86,6 +86,7 @@ The table below summarizes the functions available for function queries.
|===
|Function |Description |Syntax Examples
|abs |Returns the absolute value of the specified value or function. |`abs(x)` `abs(-5)`
|concat(v,f..)|concatenates the given string fields, literals and other functions |`concat(name," ",$param,def(opt,"-"))`
|"constant" |Specifies a floating point constant. |`1.5`
|def |`def` is short for default. Returns the value of field "field", or if the field does not exist, returns the default value specified. and yields the first value where `exists()==true`.) |`def(rating,5):` This `def()` function returns the rating, or if no rating specified in the doc, returns 5 `def(myfield, 1.0):` equivalent to `if(exists(myfield),myfield,1.0)`
|div |Divides one value or function by another. div(x,y) divides x by y. |`div(1,y)` `div(sum(x,100),max(y,1))`