From 4b0c3cc7b43db581a6e663bd9ca67d2d267e2644 Mon Sep 17 00:00:00 2001 From: Alexey Timofeev Date: Thu, 13 Apr 2017 18:07:56 +0300 Subject: [PATCH] SOLR-1384 minShouldMatch parameter is supported for boolean query --- solr/CHANGES.txt | 2 + .../solr/parser/SolrQueryParserBase.java | 14 ++++ .../org/apache/solr/search/LuceneQParser.java | 3 + .../TestMinShouldMatchForBooleanQuery.java | 69 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 solr/core/src/test/org/apache/solr/search/TestMinShouldMatchForBooleanQuery.java diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 4627b2e094eb..a05d4b106797 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -67,6 +67,8 @@ Upgrading from Solr 6.x New Features ---------------------- +* SOLR-1384: minShouldMatch parameter is supported for boolean query. + * SOLR-9857, SOLR-9858: Collect aggregated metrics from nodes and shard leaders in overseer. (ab) * SOLR-9835: Create another replication mode for SolrCloud diff --git a/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java b/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java index 21e0aa0630af..692aba053fb7 100644 --- a/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java +++ b/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import org.apache.lucene.analysis.Analyzer; @@ -58,6 +59,9 @@ import org.apache.solr.search.QParser; import org.apache.solr.search.SolrConstantScoreQuery; import org.apache.solr.search.SyntaxError; +import org.apache.solr.util.SolrPluginUtils; + +import static java.util.Optional.empty; /** This class is overridden by QueryParser in QueryParser.jj * and acts to separate the majority of the Java code from the .jj grammar file. @@ -103,6 +107,12 @@ public abstract class SolrQueryParserBase extends QueryBuilder { // implementation detail - caching ReversedWildcardFilterFactory based on type private Map leadingWildcards; + public void setDefaultMinShouldMatchSpec(String defaultMinShouldMatchSpec) { + this.defaultMinShouldMatchSpec = Optional.ofNullable(defaultMinShouldMatchSpec); + } + + private Optional defaultMinShouldMatchSpec = empty(); + /** * Identifies the list of all known "magic fields" that trigger * special parsing behavior @@ -657,6 +667,10 @@ protected Query getBooleanQuery(List clauses) throws SyntaxError } } + if (defaultMinShouldMatchSpec.isPresent()) { + SolrPluginUtils.setMinShouldMatch(booleanBuilder, defaultMinShouldMatchSpec.get()); + } + BooleanQuery bq = booleanBuilder.build(); if (bq.clauses().size() == 1) { // Unwrap single SHOULD query BooleanClause clause = bq.clauses().iterator().next(); diff --git a/solr/core/src/java/org/apache/solr/search/LuceneQParser.java b/solr/core/src/java/org/apache/solr/search/LuceneQParser.java index 9668d8f8e4ac..8f746e0fba04 100644 --- a/solr/core/src/java/org/apache/solr/search/LuceneQParser.java +++ b/solr/core/src/java/org/apache/solr/search/LuceneQParser.java @@ -18,6 +18,7 @@ import org.apache.lucene.search.Query; import org.apache.solr.common.params.CommonParams; +import org.apache.solr.common.params.DisMaxParams; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.StrUtils; import org.apache.solr.request.SolrQueryRequest; @@ -50,6 +51,8 @@ public Query parse() throws SyntaxError { lparser.setSplitOnWhitespace(StrUtils.parseBool (getParam(QueryParsing.SPLIT_ON_WHITESPACE), SolrQueryParser.DEFAULT_SPLIT_ON_WHITESPACE)); + lparser.setDefaultMinShouldMatchSpec(getParam(DisMaxParams.MM)); + return lparser.parse(qstr); } diff --git a/solr/core/src/test/org/apache/solr/search/TestMinShouldMatchForBooleanQuery.java b/solr/core/src/test/org/apache/solr/search/TestMinShouldMatchForBooleanQuery.java new file mode 100644 index 000000000000..04ea8d423e55 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/TestMinShouldMatchForBooleanQuery.java @@ -0,0 +1,69 @@ +/* + * 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; + +import org.apache.solr.util.AbstractSolrTestCase; +import org.junit.BeforeClass; + +/** + * + */ +public class TestMinShouldMatchForBooleanQuery extends AbstractSolrTestCase { + + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig.xml", "schema.xml"); + } + + public void testMinShouldMatchForBooleanQuery() { + String f = "name"; // name is whitespace tokenized + + assertU(adoc("id", "1", f, "A B C D")); + assertU(adoc("id", "2", f, "A B C")); + assertU(adoc("id", "3", f, "A B")); + assertU(commit()); + + assertQ("without mm we expect 3 documents to match", + req("name:(A B C D)"), + "//result[@numFound='3']" + ); + + assertQ("with mm=3 we expect 2 documents to match", + req("{!mm=3}name:(A B C D)"), + "//result[@numFound='2']" + ); + + assertQ("with mm=80% in q we expect 2 documents to match", + req("{!mm=80%}name:(A B C D)"), + "//result[@numFound='2']" + ); + + assertQ("with mm=80% in fq we expect 2 documents to match", + req("q", "*:*", + "fq","{!mm=80%}name:(A B C D)"), + "//result[@numFound='2']" + ); + + assertQ("with mm<50% 6<70% we expect 2 documents to match", + req("q", "*:*", + "fq","{!mm='3<50% 6<70%'}name:(A B C D E F)"), + "//result[@numFound='2']" + ); + + } + +}