Apache Lucene open-source search software
SOLR-2649: MM ignored in edismax queries with operators
Erick Erickson5e587ab4d15a
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 39b8b313e1e..4a450e54215 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -244,6 +244,9 @@ Bug Fixes* SOLR-6992: Fix "Files" UI to show the managed-schema file as well.(Shawn Heisey, Varun Thacker)+* SOLR-2469: MM ignored in edismax queries with operators.+ (Greg Pendlebury, Jan Høydahl et. al. via Erick Erickson)+Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.javaindex c6787280cec..dfa2a62e226 100644--- a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java+++ b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java@@ -338,10 +338,10 @@ public class ExtendedDismaxQParser extends QParser {if(query == null) {return null;}+// For correct lucene queries, turn off mm processing if there// were explicit operators (except for AND).- boolean doMinMatched = doMinMatched(clauses, config.lowercaseOperators);- if (doMinMatched && query instanceof BooleanQuery) {+ if (query instanceof BooleanQuery) {query = SolrPluginUtils.setMinShouldMatch((BooleanQuery)query, config.minShouldMatch, config.mmAutoRelax);}return query;@@ -393,27 +393,6 @@ public class ExtendedDismaxQParser extends QParser {return sb.toString();}- /**- * Returns false if at least one of the clauses is an explicit operator (except for AND)- */- private boolean doMinMatched(List<Clause> clauses, boolean lowercaseOperators) {- for (Clause clause : clauses) {- if (clause.must == '+') return false;- if (clause.must == '-') return false;- if (clause.isBareWord()) {- String s = clause.val;- if ("OR".equals(s)) {- return false;- } else if ("NOT".equals(s)) {- return false;- } else if (lowercaseOperators && "or".equals(s)) {- return false;- }- }- }- return true;- }-/*** Generates a query string from the raw clauses, uppercasing* 'and' and 'or' as needed.@@ -1001,8 +980,11 @@ public class ExtendedDismaxQParser extends QParser {public ExtendedSolrQueryParser(QParser parser, String defaultField) {super(parser, defaultField);- // don't trust that our parent class won't ever change its default- setDefaultOperator(QueryParser.Operator.OR);+ // Respect the q.op parameter before mm will be applied later+ SolrParams defaultParams = SolrParams.wrapDefaults(parser.getLocalParams(), parser.getParams());+ QueryParser.Operator defaultOp = QueryParsing.getQueryParserDefaultOperator(+ parser.getReq().getSchema(), defaultParams.get(QueryParsing.OP));+ setDefaultOperator(defaultOp);}public void setRemoveStopFilter(boolean remove) {diff --git a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.javaindex 89a015f720b..8919fea5220 100644--- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java+++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java@@ -78,6 +78,10 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {assertU(adoc("id", "59", "HTMLstandardtok", "大亚"));assertU(adoc("id", "60", "HTMLstandardtok", "大亚湾"));assertU(adoc("id", "61", "text_sw", "bazaaa")); // synonyms in an expansion group+ assertU(adoc("id", "62", "text_sw", "oil stocks"));+ assertU(adoc("id", "63", "text_sw", "gold stocks"));+ assertU(adoc("id", "64", "text_sw", "stocks gold stockade"));+ assertU(adoc("id", "65", "text_sw", "snake oil"));assertU(commit());}@@ -683,7 +687,7 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {assertU(adoc("id", "s0", "phrase_sw", "foo bar a b c", "boost_d", "1.0"));assertU(adoc("id", "s1", "phrase_sw", "foo a bar b c", "boost_d", "2.0"));assertU(adoc("id", "s2", "phrase_sw", "foo a b bar c", "boost_d", "3.0"));- assertU(adoc("id", "s3", "phrase_sw", "foo a b c bar", "boost_d", "4.0"));+ assertU(adoc("id", "s3", "phrase_sw", "foo a b c bar", "boost_d", "4.0"));assertU(commit());assertQ("default order assumption wrong",@@ -695,7 +699,7 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {"//doc[1]/str[@name='id'][.='s3']","//doc[2]/str[@name='id'][.='s2']","//doc[3]/str[@name='id'][.='s1']",- "//doc[4]/str[@name='id'][.='s0']");+ "//doc[4]/str[@name='id'][.='s0']");assertQ("pf not working",req("q", "foo bar",@@ -704,42 +708,42 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {"fl", "score,*","defType", "edismax"),"//doc[1]/str[@name='id'][.='s0']");-+assertQ("pf2 not working",- req("q", "foo bar",+ req("q", "foo bar","qf", "phrase_sw","pf2", "phrase_sw^10","fl", "score,*","defType", "edismax"),- "//doc[1]/str[@name='id'][.='s0']");+ "//doc[1]/str[@name='id'][.='s0']");assertQ("pf3 not working",- req("q", "a b bar",+ req("q", "a b bar","qf", "phrase_sw","pf3", "phrase_sw^10","fl", "score,*","defType", "edismax"),- "//doc[1]/str[@name='id'][.='s2']");+ "//doc[1]/str[@name='id'][.='s2']");assertQ("ps not working for pf2",- req("q", "bar foo",+ req("q", "bar foo","qf", "phrase_sw","pf2", "phrase_sw^10","ps", "2","fl", "score,*","defType", "edismax"),- "//doc[1]/str[@name='id'][.='s0']");+ "//doc[1]/str[@name='id'][.='s0']");assertQ("ps not working for pf3",- req("q", "a bar foo",+ req("q", "a bar foo","qf", "phrase_sw","pf3", "phrase_sw^10","ps", "3","fl", "score,*","debugQuery", "true","defType", "edismax"),- "//doc[1]/str[@name='id'][.='s1']");-+ "//doc[1]/str[@name='id'][.='s1']");+assertQ("ps/ps2/ps3 with default slop overrides not working",req("q", "zzzz xxxx cccc vvvv","qf", "phrase_sw",@@ -993,11 +997,11 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {"defType", "edismax"), "*[count(//doc)=2]");assertQ("test cjk (conjunction)",- req("q", "大亚湾 OR bogus",+ req("q", "大亚湾 OR bogus", // +(((((standardtok:大 standardtok:亚 standardtok:湾)~3)) (standardtok:bogus))~2)"qf", "standardtok","mm", "100%","defType", "edismax")- , "*[count(//doc)=1]");+ , "//*[@numFound='0']");}/**@@ -1013,7 +1017,137 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {"defType", "edismax"), "*[count(//doc)=1]");}++ /**+ * Test that the default operator and MM are interacting appropriately when both provided+ */+ public void testDefaultOperatorWithMm() throws Exception {+ // Text we are searching+ // "line up and fly directly at the enemy death cannons, clogging them with wreckage!"+ assertQ("test default operator with mm (AND + 0% => 0 hits)",+ req("q", "(line notfound) OR notfound",+ "qf", "text",+ "q.op", "AND",+ "mm", "0%",+ "defType", "edismax")+ , "*[count(//doc)=0]");+ assertQ("test default operator with mm (OR + 0% => 1 hit)",+ req("q", "line notfound OR notfound",+ "qf", "text",+ "q.op", "OR",+ "mm", "0%",+ "defType", "edismax")+ , "*[count(//doc)=1]");+ assertQ("test default operator with mm (OR + 100% => 0 hits)",+ req("q", "line notfound OR notfound",+ "qf", "text",+ "q.op", "OR",+ "mm", "100%",+ "defType", "edismax")+ , "*[count(//doc)=0]");+ assertQ("test default operator with mm (OR + 35% => 1 hit)",+ req("q", "line notfound notfound2 OR notfound",+ "qf", "text",+ "q.op", "OR",+ "mm", "35%",+ "defType", "edismax")+ , "*[count(//doc)=1]");+ assertQ("test default operator with mm (OR + 75% => 0 hits)",+ req("q", "line notfound notfound2 OR notfound3",+ "qf", "text",+ "q.op", "OR",+ "mm", "75%",+ "defType", "edismax")+ , "*[count(//doc)=0]");+ assertQ("test default operator with mm (AND + 0% => 1 hit)",+ req("q", "(line enemy) OR notfound",+ "qf", "text",+ "q.op", "AND",+ "mm", "0%",+ "defType", "edismax")+ , "*[count(//doc)=1]");+ assertQ("test default operator with mm (AND + 50% => 1 hit)",+ req("q", "(line enemy) OR (line notfound) OR (death cannons) OR (death notfound)",+ "qf", "text",+ "q.op", "AND",+ "mm", "50%",+ "defType", "edismax")+ , "*[count(//doc)=1]");+ assertQ("test default operator with mm (AND + 75% => 0 hits)",+ req("q", "(line enemy) OR (line notfound) OR (death cannons) OR (death notfound)",+ "qf", "text",+ "q.op", "AND",+ "mm", "75%",+ "defType", "edismax")+ , "*[count(//doc)=0]");+ }+ /**+ * Test that minShouldMatch applies to Optional terms only+ */+ public void testMinShouldMatchOptional() throws Exception {+ assertQ("test minShouldMatch (top level optional terms only)",+ req("q", "stocks oil gold", // +(((text_sw:stock) (text_sw:oil) (text_sw:gold))~1)+ "qf", "text_sw",+ "mm", "50%",+ "defType", "edismax")+ , "*[count(//doc)=4]");++ assertQ("test minShouldMatch (top level optional and negative terms mm=50%)",+ req("q", "stocks oil gold -stockade", // +(((text_sw:stock) (text_sw:oil) (text_sw:gold) -(text_sw:stockad))~1)+ "qf", "text_sw",+ "mm", "50%",+ "defType", "edismax")+ , "*[count(//doc)=3]");++ assertQ("test minShouldMatch (top level optional and negative terms mm=100%)",+ req("q", "stocks gold -stockade", // +(((text_sw:stock) (text_sw:oil) (text_sw:gold) -(text_sw:stockad))~2)+ "qf", "text_sw",+ "mm", "100%",+ "defType", "edismax")+ , "*[count(//doc)=1]");++ assertQ("test minShouldMatch (top level required terms only)",+ req("q", "stocks AND oil", // +(+(text_sw:stock) +(text_sw:oil))+ "qf", "text_sw",+ "mm", "50%",+ "defType", "edismax")+ , "*[count(//doc)=1]");++ assertQ("test minShouldMatch (top level optional and required terms)",+ req("q", "oil gold +stocks", // +(((text_sw:oil) (text_sw:gold) +(text_sw:stock))~1)+ "qf", "text_sw",+ "mm", "50%",+ "defType", "edismax")+ , "*[count(//doc)=3]");++ assertQ("test minShouldMatch (top level optional with explicit OR and parens)",+ req("q", "(snake OR stocks) oil",+ "qf", "text_sw",+ "mm", "100%",+ "defType", "edismax")+ , "*[count(//doc)=2]");++ // The results for these two appear odd, but are correct as per BooleanQuery processing.+ // See: http://searchhub.org/2011/12/28/why-not-and-or-and-not/+ // Non-parenthesis OR/AND precedence is not true to abstract boolean logic in solr when q.op = AND+ // and when q.op = OR all three clauses are top-level and optional so mm takes over+ assertQ("test minShouldMatch (top level optional with explicit OR without parens)",+ req("q", "snake OR stocks oil",+ "qf", "text_sw",+ "q.op", "OR",+ "mm", "100%",+ "defType", "edismax")+ , "*[count(//doc)=0]");+ assertQ("test minShouldMatch (top level optional with explicit OR without parens)",+ req("q", "snake OR stocks oil",+ "qf", "text_sw",+ "q.op", "AND",+ "mm", "100%",+ "defType", "edismax")+ , "*[count(//doc)=0]");+ }+public void testEdismaxSimpleExtension() throws SyntaxError {ModifiableSolrParams params = new ModifiableSolrParams();params.set("q", "foo bar");