You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by md...@apache.org on 2021/05/11 15:04:48 UTC
[solr] branch main updated: SOLR-15389: Tests shouldn't rely on
dismax clause order (#117)
This is an automated email from the ASF dual-hosted git repository.
mdrob pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new f5ad738 SOLR-15389: Tests shouldn't rely on dismax clause order (#117)
f5ad738 is described below
commit f5ad738a6d4c9f3e23b56e95d44120a0d345b4a5
Author: Mike Drob <md...@apache.org>
AuthorDate: Tue May 11 10:04:39 2021 -0500
SOLR-15389: Tests shouldn't rely on dismax clause order (#117)
Introduces QueryMatcher compatible with Hamcrest assertions.
---
.../solr/search/TestExtendedDismaxParser.java | 201 +++++++++++-------
.../apache/solr/search/TestSolrQueryParser.java | 213 ++++++++++++++-----
.../src/java/org/apache/solr/SolrTestCaseJ4.java | 4 +-
.../java/org/apache/solr/util/QueryMatchers.java | 232 +++++++++++++++++++++
4 files changed, 523 insertions(+), 127 deletions(-)
diff --git a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
index fdab246..f9ae300 100644
--- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
+++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
@@ -33,19 +33,30 @@ import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.Utils;
import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.util.BaseTestHarness;
import org.apache.solr.util.SolrPluginUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
-@LuceneTestCase.AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-15389")
+import javax.xml.xpath.XPathConstants;
+
+import static org.apache.solr.util.QueryMatchers.booleanQuery;
+import static org.apache.solr.util.QueryMatchers.boosted;
+import static org.apache.solr.util.QueryMatchers.disjunctionOf;
+import static org.apache.solr.util.QueryMatchers.phraseQuery;
+import static org.apache.solr.util.QueryMatchers.stringQuery;
+import static org.apache.solr.util.QueryMatchers.termQuery;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.anyOf;
+import static org.hamcrest.Matchers.containsString;
+
public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
@BeforeClass
@@ -830,7 +841,7 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
}
// test phrase fields including pf2 pf3 and phrase slop
- public void testPfPs() {
+ public void testPfPs() throws Exception {
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"));
@@ -955,17 +966,24 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
"//str[@name='parsedquery'][contains(.,'(phrase_sw:\"zzzz xxxx\"~2)^22.0')]"
);
- assertQ("phrase field queries spanning multiple fields should be within their own dismax queries",
- req("q", "aaaa bbbb cccc",
+ String parsedquery = getParsedQuery(req("q", "aaaa bbbb cccc",
"qf", "phrase_sw phrase1_sw",
"pf2", "phrase_sw phrase1_sw",
"pf3", "phrase_sw phrase1_sw",
"defType", "edismax",
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'(phrase_sw:\"aaaa bbbb\" | phrase1_sw:\"aaaa bbbb\")')]",
- "//str[@name='parsedquery'][contains(.,'(phrase_sw:\"bbbb cccc\" | phrase1_sw:\"bbbb cccc\")')]",
- "//str[@name='parsedquery'][contains(.,'(phrase_sw:\"aaaa bbbb cccc\" | phrase1_sw:\"aaaa bbbb cccc\")')]"
- );
+ "debugQuery", "true"));
+ assertThat("phrase field queries spanning multiple fields should be within their own dismax queries",
+ parsedquery, anyOf(
+ containsString("(phrase_sw:\"aaaa bbbb\" | phrase1_sw:\"aaaa bbbb\")"),
+ containsString("(phrase1_sw:\"aaaa bbbb\" | phrase_sw:\"aaaa bbbb\")")));
+ assertThat("phrase field queries spanning multiple fields should be within their own dismax queries",
+ parsedquery, anyOf(
+ containsString("(phrase_sw:\"bbbb cccc\" | phrase1_sw:\"bbbb cccc\")"),
+ containsString("(phrase1_sw:\"bbbb cccc\" | phrase_sw:\"bbbb cccc\")")));
+ assertThat("phrase field queries spanning multiple fields should be within their own dismax queries",
+ parsedquery, anyOf(
+ containsString("(phrase_sw:\"aaaa bbbb cccc\" | phrase1_sw:\"aaaa bbbb cccc\")"),
+ containsString("(phrase1_sw:\"aaaa bbbb cccc\" | phrase_sw:\"aaaa bbbb cccc\")")));
}
@Test
@@ -1724,92 +1742,99 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
, "/response/docs/[0]/id=='72'"
);
- assertQ(req("qf", "name title",
+ String parsedquery;
+ parsedquery = getParsedQuery(req("qf", "name title",
"q", "barking curds of stigma",
"defType", "edismax",
"sow", "false",
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:barking | title:barking))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:curds | title:curds))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:of | title:of))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:stigma | title:stigma))')]"
- );
- assertQ(req("qf", "name title",
+ "debugQuery", "true"));
+ assertThat(parsedquery, anyOf(containsString("((name:barking | title:barking))"), containsString("((title:barking | name:barking))")));
+ assertThat(parsedquery, anyOf(containsString("((name:curds | title:curds))"), containsString("((title:curds | name:curds))")));
+ assertThat(parsedquery, anyOf(containsString("((name:of | title:of))"), containsString("((title:of | name:of))")));
+ assertThat(parsedquery, anyOf(containsString("((name:stigma | title:stigma))"), containsString("((title:stigma | name:stigma))")));
+ parsedquery = getParsedQuery(req("qf", "name title",
"q", "barking curds of stigma",
"defType", "edismax",
"sow", "true",
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:barking | title:barking))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:curds | title:curds))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:of | title:of))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:stigma | title:stigma))')]"
- );
- assertQ(req("qf", "name title",
+ "debugQuery", "true"));
+ assertThat(parsedquery, anyOf(containsString("((name:barking | title:barking))"), containsString("((title:barking | name:barking))")));
+ assertThat(parsedquery, anyOf(containsString("((name:curds | title:curds))"), containsString("((title:curds | name:curds))")));
+ assertThat(parsedquery, anyOf(containsString("((name:of | title:of))"), containsString("((title:of | name:of))")));
+ assertThat(parsedquery, anyOf(containsString("((name:stigma | title:stigma))"), containsString("((title:stigma | name:stigma))")));
+ parsedquery = getParsedQuery(req("qf", "name title",
"q", "barking curds of stigma",
"defType", "edismax",
- "debugQuery", "true"), // Default sow=false
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:barking | title:barking))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:curds | title:curds))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:of | title:of))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((name:stigma | title:stigma))')]"
- );
+ "debugQuery", "true")); // Default sow=false
+ assertThat(parsedquery, anyOf(containsString("((name:barking | title:barking))"), containsString("((title:barking | name:barking))")));
+ assertThat(parsedquery, anyOf(containsString("((name:curds | title:curds))"), containsString("((title:curds | name:curds))")));
+ assertThat(parsedquery, anyOf(containsString("((name:of | title:of))"), containsString("((title:of | name:of))")));
+ assertThat(parsedquery, anyOf(containsString("((name:stigma | title:stigma))"), containsString("((title:stigma | name:stigma))")));
+ }
+
+ private static String getParsedQuery(SolrQueryRequest request) throws Exception {
+ String resp = h.query(request);
+ return (String) BaseTestHarness.evaluateXPath(resp, "//str[@name='parsedquery']/text()", XPathConstants.STRING);
}
public void testSplitOnWhitespace_Different_Field_Analysis() throws Exception {
// When the *structure* of produced queries is different in each field,
// sow=true produces boolean-of-dismax query structure,
// and sow=false produces dismax-of-boolean query structure.
- assertQ(req("qf", "text_sw title",
+ String parsedquery = getParsedQuery(req("qf", "text_sw title",
"q", "olive the other",
"defType", "edismax",
"sow", "true",
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((text_sw:oliv | title:olive))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((title:the))')]",
- "//str[@name='parsedquery'][contains(.,'DisjunctionMaxQuery((text_sw:other | title:other))')]"
- );
- assertQ(req("qf", "text_sw title",
+ "debugQuery", "true"));
+ assertThat(parsedquery, anyOf(containsString("((text_sw:oliv | title:olive))"), containsString("((title:olive | text_sw:oliv))")));
+ assertThat(parsedquery, containsString("DisjunctionMaxQuery((title:the))"));
+ assertThat(parsedquery, anyOf(containsString("((text_sw:other | title:other))"), containsString("((title:other | text_sw:other))")));
+
+ parsedquery = getParsedQuery(req("qf", "text_sw title",
"q", "olive the other",
"defType", "edismax",
"sow", "false",
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'+DisjunctionMaxQuery(((text_sw:oliv text_sw:other) | (title:olive title:the title:other)))')]"
- );
+ "debugQuery", "true"));
+ assertThat(parsedquery, anyOf(
+ containsString("(((text_sw:oliv text_sw:other) | (title:olive title:the title:other)))"),
+ containsString("(((title:olive title:the title:other) | (text_sw:oliv text_sw:other)))")
+ ));
// When field's analysis produce different query structures, mm processing is always done on the boolean query.
// sow=true produces (boolean-of-dismax)~<mm> query structure,
// and sow=false produces dismax-of-(boolean)~<mm> query structure.
- assertQ(req("qf", "text_sw title",
+ parsedquery = getParsedQuery(req("qf", "text_sw title",
"q", "olive the other",
"defType", "edismax",
"sow", "true",
"mm", "100%",
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'+(DisjunctionMaxQuery((text_sw:oliv | title:olive)) DisjunctionMaxQuery((title:the)) DisjunctionMaxQuery((text_sw:other | title:other)))~3')]"
- );
- assertQ(req("qf", "text_sw title",
+ "debugQuery", "true"));
+ assertThat(parsedquery, anyOf(containsString("((text_sw:oliv | title:olive))"), containsString("((title:olive | text_sw:oliv))")));
+ assertThat(parsedquery, containsString("DisjunctionMaxQuery((title:the))"));
+ assertThat(parsedquery, anyOf(containsString("((text_sw:other | title:other))"), containsString("((title:other | text_sw:other))")));
+ assertThat(parsedquery, containsString("))~3"));
+
+ parsedquery = getParsedQuery(req("qf", "text_sw title",
"q", "olive the other",
"defType", "edismax",
"sow", "false",
"mm", "100%",
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'+DisjunctionMaxQuery((((text_sw:oliv text_sw:other)~2) | ((title:olive title:the title:other)~3)))')]"
- );
-
+ "debugQuery", "true"));
+ assertThat(parsedquery, anyOf(
+ containsString("(((text_sw:oliv text_sw:other)~2) | ((title:olive title:the title:other)~3)))"),
+ containsString("(((title:olive title:the title:other)~3) | ((text_sw:oliv text_sw:other)~2)))")
+ ));
// When the *structure* of produced queries is the same in each field,
// sow=false/true produce the same boolean-of-dismax query structure
for (String sow : Arrays.asList("true", "false")) {
- assertQ(req("qf", "text_sw title",
+ parsedquery = getParsedQuery(req("qf", "text_sw title",
"q", "olive blah other",
"defType", "edismax",
"sow", sow,
- "debugQuery", "true"),
- "//str[@name='parsedquery'][contains(.,'"
- + "+(DisjunctionMaxQuery((text_sw:oliv | title:olive))"
- + " DisjunctionMaxQuery((text_sw:blah | title:blah))"
- + " DisjunctionMaxQuery((text_sw:other | title:other)))')]"
- );
+ "debugQuery", "true"));
+ assertThat(parsedquery, anyOf(containsString("((text_sw:oliv | title:olive))"), containsString("((title:olive | text_sw:oliv))")));
+ assertThat(parsedquery, anyOf(containsString("((text_sw:blah | title:blah))"), containsString("((title:blah | text_sw:blah))")));
+ assertThat(parsedquery, anyOf(containsString("((text_sw:other | title:other))"), containsString("((title:other | text_sw:other))")));
}
}
@@ -2094,30 +2119,38 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
try (SolrQueryRequest req = req(params)) {
QParser qParser = QParser.getParser("grackle", "edismax", req);
Query q = qParser.getQuery();
- assertEquals("+(((text:\"crow blackbird\" text:grackl))"
- + " | (((+text_sw:crow +text_sw:blackbird) text_sw:grackl)))",
- q.toString());
+ assertThat(q, booleanQuery(disjunctionOf(
+ stringQuery("(text:\"crow blackbird\" text:grackl)"),
+ stringQuery("((+text_sw:crow +text_sw:blackbird) text_sw:grackl)")
+ ), BooleanClause.Occur.MUST));
qParser = QParser.getParser("grackle wi fi", "edismax", req);
q = qParser.getQuery();
- assertEquals("+(((text:\"crow blackbird\" text:grackl) text:wifi)"
- + " | (((+text_sw:crow +text_sw:blackbird) text_sw:grackl) text_sw:wifi))",
- q.toString());
+ assertThat(q, booleanQuery(disjunctionOf(
+ stringQuery("(text:\"crow blackbird\" text:grackl) text:wifi"),
+ stringQuery("((+text_sw:crow +text_sw:blackbird) text_sw:grackl) text_sw:wifi")
+ ), BooleanClause.Occur.MUST));
}
}
try (SolrQueryRequest req = req(sowTrueParams)) {
QParser qParser = QParser.getParser("grackle", "edismax", req);
Query q = qParser.getQuery();
- assertEquals("+((text:\"crow blackbird\" text:grackl)"
- + " | (((+text_sw:crow +text_sw:blackbird) text_sw:grackl)))",
- q.toString());
+ assertThat(q, booleanQuery(disjunctionOf(
+ stringQuery("text:\"crow blackbird\" text:grackl"),
+ stringQuery("((+text_sw:crow +text_sw:blackbird) text_sw:grackl)")
+ ), BooleanClause.Occur.MUST));
qParser = QParser.getParser("grackle wi fi", "edismax", req);
q = qParser.getQuery();
- assertEquals("+(((text:\"crow blackbird\" text:grackl)"
- + " | (((+text_sw:crow +text_sw:blackbird) text_sw:grackl))) (text:wi | text_sw:wi) (text:fi | text_sw:fi))",
- q.toString());
+ assertThat(q, booleanQuery(booleanQuery(
+ disjunctionOf(termQuery("text", "wi"), termQuery("text_sw", "wi")),
+ disjunctionOf(termQuery("text", "fi"), termQuery("text_sw", "fi")),
+ disjunctionOf(
+ stringQuery("((+text_sw:crow +text_sw:blackbird) text_sw:grackl)"),
+ booleanQuery(phraseQuery("text", "crow blackbird"), termQuery("text", "grackl"))
+ )
+ ), BooleanClause.Occur.MUST));
}
}
@@ -2125,22 +2158,34 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
try (SolrQueryRequest req = req("sow", "false", "qf", "subject title")) {
QParser qParser = QParser.getParser("one two", "edismax", req);
Query q = qParser.getQuery();
- assertEquals("+((title:one | subject:on) (title:two | subject:two))", q.toString());
+ assertThat(q, booleanQuery(booleanQuery(
+ disjunctionOf(termQuery("title", "one"), termQuery("subject", "on")),
+ disjunctionOf(termQuery("title", "two"), termQuery("subject", "two"))
+ ), BooleanClause.Occur.MUST));
}
try (SolrQueryRequest req = req("sow", "false", "qf", "subject title^5")) {
QParser qParser = QParser.getParser("one two", "edismax", req);
Query q = qParser.getQuery();
- assertEquals("+(((title:one)^5.0 | subject:on) ((title:two)^5.0 | subject:two))", q.toString());
+ assertThat(q, booleanQuery(booleanQuery(
+ disjunctionOf(boosted("title", "one", 5), termQuery("subject", "on")),
+ disjunctionOf(boosted("title", "two", 5), termQuery("subject", "two"))
+ ), BooleanClause.Occur.MUST));
}
try (SolrQueryRequest req = req("sow", "false", "qf", "subject^3 title")) {
QParser qParser = QParser.getParser("one two", "edismax", req);
Query q = qParser.getQuery();
- assertEquals("+((title:one | (subject:on)^3.0) (title:two | (subject:two)^3.0))", q.toString());
+ assertThat(q, booleanQuery(booleanQuery(
+ disjunctionOf(termQuery("title", "one"), boosted("subject", "on", 3)),
+ disjunctionOf(termQuery("title", "two"), boosted("subject", "two", 3))
+ ), BooleanClause.Occur.MUST));
}
try (SolrQueryRequest req = req("sow", "false", "qf", "subject^10 title^20")) {
QParser qParser = QParser.getParser("one two", "edismax", req);
Query q = qParser.getQuery();
- assertEquals("+(((title:one)^20.0 | (subject:on)^10.0) ((title:two)^20.0 | (subject:two)^10.0))", q.toString());
+ assertThat(q, booleanQuery(booleanQuery(
+ disjunctionOf(boosted("title", "one", 20), boosted("subject", "on", 10)),
+ disjunctionOf(boosted("title", "two", 20), boosted("subject", "two", 10))
+ ), BooleanClause.Occur.MUST));
}
}
@@ -2348,8 +2393,14 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
// test valid field names
String response = h.query(req(params));
- assertTrue(response.contains("+DisjunctionMaxQuery((title:olive | " +
- "(subject:oliv)^3.0)) +DisjunctionMaxQuery((title:other | (subject:other)^3.0))"));
+ assertThat(response, allOf(
+ containsString("+(+DisjunctionMaxQuery(("),
+ containsString("title:olive"),
+ containsString("(subject:oliv)^3.0"),
+ containsString(" +DisjunctionMaxQuery(("),
+ containsString("title:other"),
+ containsString("(subject:other)^3.0")
+ ));
// test invalid field name
params.set("qf", "subject^3 nosuchfield");
diff --git a/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java b/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java
index 142c473..2a90327 100644
--- a/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java
+++ b/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java
@@ -36,7 +36,6 @@ import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
@@ -56,9 +55,13 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
+import static org.apache.solr.util.QueryMatchers.booleanQuery;
+import static org.apache.solr.util.QueryMatchers.boosted;
+import static org.apache.solr.util.QueryMatchers.disjunctionOf;
+import static org.apache.solr.util.QueryMatchers.phraseQuery;
+import static org.apache.solr.util.QueryMatchers.termQuery;
import static org.hamcrest.core.StringContains.containsString;
-@LuceneTestCase.AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-15389")
public class TestSolrQueryParser extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
@@ -1210,49 +1213,78 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
);
}
-
public void testSynonymQueryStyle() throws Exception {
-
- Query q = QParser.getParser("tabby", req(params("df", "t_pick_best_foo"))).getQuery();
- assertEquals("(t_pick_best_foo:tabbi | t_pick_best_foo:cat | t_pick_best_foo:felin | t_pick_best_foo:anim)", q.toString());
-
- q = QParser.getParser("tabby", req(params("df", "t_as_distinct_foo"))).getQuery();
- assertEquals("t_as_distinct_foo:tabbi t_as_distinct_foo:cat t_as_distinct_foo:felin t_as_distinct_foo:anim", q.toString());
+ String field = "t_pick_best_foo";
+ Query q = QParser.getParser("tabby", req(params("df", field))).getQuery();
+ assertThat(q, disjunctionOf(
+ termQuery(field, "cat"),
+ termQuery(field, "tabbi"),
+ termQuery(field, "anim"),
+ termQuery(field, "felin")
+ ));
+
+ field = "t_as_distinct_foo";
+ q = QParser.getParser("tabby", req(params("df", field))).getQuery();
+ assertThat(q, booleanQuery(
+ termQuery(field,"cat"),
+ termQuery(field,"tabbi"),
+ termQuery(field,"anim"),
+ termQuery(field,"felin")
+ ));
/*confirm autoGeneratePhraseQueries always builds OR queries*/
q = QParser.getParser("jeans", req(params("df", "t_as_distinct_foo", "sow", "false"))).getQuery();
assertEquals("(t_as_distinct_foo:\"denim pant\" t_as_distinct_foo:jean)", q.toString());
- q = QParser.getParser("jeans", req(params("df", "t_pick_best_foo", "sow", "false"))).getQuery();
- assertEquals("(t_pick_best_foo:\"denim pant\" | t_pick_best_foo:jean)", q.toString());
+ field = "t_pick_best_foo";
+ q = QParser.getParser("jeans", req(params("df", field, "sow", "false"))).getQuery();
+ assertThat(q, booleanQuery(disjunctionOf(
+ termQuery(field, "jean"),
+ phraseQuery(field, "denim pant")
+ )));
}
public void testSynonymsBoost_singleTermQuerySingleTermSynonyms_shouldParseBoostedQuery() throws Exception {
//tiger, tigre|0.9
- Query q = QParser.getParser("tiger", req(params("df", "t_pick_best_boosted_foo"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:tigre)^0.9 | t_pick_best_boosted_foo:tiger)", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("tiger", req(params("df", field))).getQuery();
+ assertThat(q, disjunctionOf(termQuery(field, "tiger"), boosted(field, "tigre", 0.9f)));
+ field = "t_as_distinct_boosted_foo";
q = QParser.getParser("tiger", req(params("df", "t_as_distinct_boosted_foo"))).getQuery();
assertEquals("(t_as_distinct_boosted_foo:tigre)^0.9 t_as_distinct_boosted_foo:tiger", q.toString());
+ field = "t_as_same_term_boosted_foo";
q = QParser.getParser("tiger", req(params("df", "t_as_same_term_boosted_foo"))).getQuery();
assertEquals("Synonym(t_as_same_term_boosted_foo:tiger t_as_same_term_boosted_foo:tigre^0.9)", q.toString());
//lynx => lince|0.8, lynx_canadensis|0.9
- q = QParser.getParser("lynx", req(params("df", "t_pick_best_boosted_foo"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:lince)^0.8 | (t_pick_best_boosted_foo:lynx_canadensis)^0.9)", q.toString());
-
- q = QParser.getParser("lynx", req(params("df", "t_as_distinct_boosted_foo"))).getQuery();
- assertEquals("(t_as_distinct_boosted_foo:lince)^0.8 (t_as_distinct_boosted_foo:lynx_canadensis)^0.9", q.toString());
-
- q = QParser.getParser("lynx", req(params("df", "t_as_same_term_boosted_foo"))).getQuery();
+ field = "t_pick_best_boosted_foo";
+ q = QParser.getParser("lynx", req(params("df", field))).getQuery();
+ assertThat(q, disjunctionOf(boosted(field, "lince", 0.8f), boosted(field, "lynx_canadensis", 0.9f)));
+
+ field = "t_as_distinct_boosted_foo";
+ q = QParser.getParser("lynx", req(params("df", field))).getQuery();
+ assertThat(q, booleanQuery(
+ boosted(field, "lince", 0.8f),
+ boosted(field, "lynx_canadensis", 0.9f)
+ ));
+
+ field = "t_as_same_term_boosted_foo";
+ q = QParser.getParser("lynx", req(params("df", field))).getQuery();
assertEquals("Synonym(t_as_same_term_boosted_foo:lince^0.8 t_as_same_term_boosted_foo:lynx_canadensis^0.9)", q.toString());
}
public void testSynonymsBoost_singleTermQueryMultiTermSynonyms_shouldParseBoostedQuery() throws Exception {
//leopard, big cat|0.8, bagheera|0.9, panthera pardus|0.85
+ String field = "t_pick_best_boosted_foo";
Query q = QParser.getParser("leopard", req(params("df", "t_pick_best_boosted_foo"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:\"big cat\")^0.8 | (t_pick_best_boosted_foo:bagheera)^0.9 | (t_pick_best_boosted_foo:\"panthera pardus\")^0.85 | t_pick_best_boosted_foo:leopard)", q.toString());
+ assertThat(q, booleanQuery(disjunctionOf(
+ termQuery(field, "leopard"),
+ boosted(phraseQuery(field, "big cat"), 0.8f),
+ boosted(phraseQuery(field, "panthera pardus"), 0.85f),
+ boosted(termQuery(field, "bagheera"), 0.9f)
+ )));
q = QParser.getParser("leopard", req(params("df", "t_as_distinct_boosted_foo"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:\"big cat\")^0.8 (t_as_distinct_boosted_foo:bagheera)^0.9 (t_as_distinct_boosted_foo:\"panthera pardus\")^0.85 t_as_distinct_boosted_foo:leopard)", q.toString());
@@ -1262,7 +1294,11 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
//lion => panthera leo|0.9, simba leo|0.8, kimba|0.75
q = QParser.getParser("lion", req(params("df", "t_pick_best_boosted_foo"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:\"panthera leo\")^0.9 | (t_pick_best_boosted_foo:\"simba leo\")^0.8 | (t_pick_best_boosted_foo:kimba)^0.75)", q.toString());
+ assertThat(q, booleanQuery(disjunctionOf(
+ boosted(termQuery(field, "kimba"), 0.75f),
+ boosted(phraseQuery(field, "simba leo"), 0.8f),
+ boosted(phraseQuery(field, "panthera leo"), 0.9f)
+ )));
q = QParser.getParser("lion", req(params("df", "t_as_distinct_boosted_foo"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:\"panthera leo\")^0.9 (t_as_distinct_boosted_foo:\"simba leo\")^0.8 (t_as_distinct_boosted_foo:kimba)^0.75)", q.toString());
@@ -1274,9 +1310,18 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testSynonymsBoost_multiTermQuerySingleTermSynonyms_shouldParseBoostedQuery() throws Exception {
//tiger, tigre|0.9
//lynx => lince|0.8, lynx_canadensis|0.9
- Query q = QParser.getParser("tiger lynx", req(params("df", "t_pick_best_boosted_foo"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:tigre)^0.9 | t_pick_best_boosted_foo:tiger)" +
- " ((t_pick_best_boosted_foo:lince)^0.8 | (t_pick_best_boosted_foo:lynx_canadensis)^0.9)", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("tiger lynx", req(params("df", field))).getQuery();
+ assertThat(q, booleanQuery(
+ disjunctionOf(
+ boosted(field, "lince", 0.8f),
+ boosted(field, "lynx_canadensis", 0.9f)
+ ),
+ disjunctionOf(
+ termQuery(field, "tiger"),
+ boosted(field, "tigre", 0.9f)
+ )
+ ));
q = QParser.getParser("tiger lynx", req(params("df", "t_as_distinct_boosted_foo"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:tigre)^0.9 t_as_distinct_boosted_foo:tiger)" +
@@ -1290,9 +1335,21 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testSynonymsBoost_multiTermQueryMultiTermSynonyms_shouldParseBoostedQuery() throws Exception {
//leopard, big cat|0.8, bagheera|0.9, panthera pardus|0.85
//lion => panthera leo|0.9, simba leo|0.8, kimba|0.75
- Query q = QParser.getParser("leopard lion", req(params("df", "t_pick_best_boosted_foo"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:\"big cat\")^0.8 | (t_pick_best_boosted_foo:bagheera)^0.9 | (t_pick_best_boosted_foo:\"panthera pardus\")^0.85 | t_pick_best_boosted_foo:leopard)" +
- " ((t_pick_best_boosted_foo:\"panthera leo\")^0.9 | (t_pick_best_boosted_foo:\"simba leo\")^0.8 | (t_pick_best_boosted_foo:kimba)^0.75)", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("leopard lion", req(params("df", field))).getQuery();
+ assertThat(q, booleanQuery(
+ disjunctionOf(
+ termQuery(field, "leopard"),
+ boosted(phraseQuery(field, "big cat"), 0.8f),
+ boosted(phraseQuery(field, "panthera pardus"), 0.85f),
+ boosted(termQuery(field, "bagheera"), 0.9f)
+ ),
+ disjunctionOf(
+ boosted(termQuery(field, "kimba"), 0.75f),
+ boosted(phraseQuery(field, "simba leo"), 0.8f),
+ boosted(phraseQuery(field, "panthera leo"), 0.9f)
+ )
+ ));
q = QParser.getParser("leopard lion", req(params("df", "t_as_distinct_boosted_foo"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:\"big cat\")^0.8 (t_as_distinct_boosted_foo:bagheera)^0.9 (t_as_distinct_boosted_foo:\"panthera pardus\")^0.85 t_as_distinct_boosted_foo:leopard)" +
@@ -1306,8 +1363,15 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testSynonymsBoost_singleConceptQuerySingleTermSynonym_shouldParseBoostedQuery() throws Exception {
//panthera pardus, leopard|0.6
- Query q = QParser.getParser("panthera pardus story",req(params("df", "t_pick_best_boosted_foo","sow", "false"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:leopard)^0.6 | t_pick_best_boosted_foo:\"panthera pardus\") t_pick_best_boosted_foo:story", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("panthera pardus story",req(params("df", field,"sow", "false"))).getQuery();
+ assertThat(q, booleanQuery(
+ termQuery(field, "story"),
+ disjunctionOf(
+ boosted(field, "leopard", 0.6f),
+ phraseQuery(field, "panthera pardus")
+ )
+ ));
q = QParser.getParser("panthera pardus story", req(params("df", "t_as_distinct_boosted_foo","sow", "false"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:leopard)^0.6 t_as_distinct_boosted_foo:\"panthera pardus\") t_as_distinct_boosted_foo:story", q.toString());
@@ -1328,8 +1392,12 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testSynonymsBoost_singleConceptQueryMultiTermSynonymWithMultipleBoost_shouldParseMultiplicativeBoostedQuery() throws Exception {
//panthera blytheae, oldest|0.5 ancient|0.9 panthera
- Query q = QParser.getParser("panthera blytheae",req(params("df", "t_pick_best_boosted_foo","sow", "false"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:\"oldest ancient panthera\")^0.45 | t_pick_best_boosted_foo:\"panthera blytheae\")", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("panthera blytheae",req(params("df", field, "sow", "false"))).getQuery();
+ assertThat(q, booleanQuery(disjunctionOf(
+ boosted(phraseQuery(field, "oldest ancient panthera"), 0.45f),
+ phraseQuery(field, "panthera blytheae")
+ )));
q = QParser.getParser("panthera blytheae", req(params("df", "t_as_distinct_boosted_foo","sow", "false"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:\"oldest ancient panthera\")^0.45 t_as_distinct_boosted_foo:\"panthera blytheae\")", q.toString());
@@ -1340,9 +1408,15 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testSynonymsBoost_singleConceptQueryMultiTermSynonyms_shouldParseBoostedQuery() throws Exception {
//snow leopard, panthera uncia|0.9, big cat|0.8, white_leopard|0.6
- Query q = QParser.getParser("snow leopard",req(params("df", "t_pick_best_boosted_foo","sow", "false"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:\"panthera uncia\")^0.9 | (t_pick_best_boosted_foo:\"big cat\")^0.8 | (t_pick_best_boosted_foo:white_leopard)^0.6 | t_pick_best_boosted_foo:\"snow leopard\")", q.toString());
-
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("snow leopard",req(params("df", field, "sow", "false"))).getQuery();
+ assertThat(q, booleanQuery(disjunctionOf( // TODO why does this generate a single clause Boolean?
+ phraseQuery(field, "snow leopard"),
+ boosted(phraseQuery(field, "panthera uncia"), 0.9f),
+ boosted(phraseQuery(field, "big cat"), 0.8f),
+ boosted(field, "white_leopard", 0.6f)
+ )));
+
q = QParser.getParser("snow leopard", req(params("df", "t_as_distinct_boosted_foo","sow", "false"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:\"panthera uncia\")^0.9 (t_as_distinct_boosted_foo:\"big cat\")^0.8 (t_as_distinct_boosted_foo:white_leopard)^0.6 t_as_distinct_boosted_foo:\"snow leopard\")", q.toString());
@@ -1351,21 +1425,34 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
//panthera onca => jaguar|0.95, big cat|0.85, black panther|0.65
q = QParser.getParser("panthera onca", req(params("df", "t_pick_best_boosted_foo","sow", "false"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:jaguar)^0.95 | (t_pick_best_boosted_foo:\"big cat\")^0.85 | (t_pick_best_boosted_foo:\"black panther\")^0.65)", q.toString());
+ assertThat(q, booleanQuery(disjunctionOf(
+ boosted(field, "jaguar", 0.95f),
+ boosted(phraseQuery(field, "big cat"), 0.85f),
+ boosted(phraseQuery(field, "black panther"), 0.65f)
+ )));
q = QParser.getParser("panthera onca", req(params("df", "t_as_distinct_boosted_foo","sow", "false"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:jaguar)^0.95 (t_as_distinct_boosted_foo:\"big cat\")^0.85 (t_as_distinct_boosted_foo:\"black panther\")^0.65)", q.toString());
q = QParser.getParser("panthera onca", req(params("df", "t_as_same_term_boosted_foo","sow", "false"))).getQuery();
assertEquals("((t_as_same_term_boosted_foo:jaguar)^0.95 (t_as_same_term_boosted_foo:\"big cat\")^0.85 (t_as_same_term_boosted_foo:\"black panther\")^0.65)", q.toString());
-
}
public void testSynonymsBoost_multiConceptQuerySingleTermSynonym_shouldParseBoostedQuery() throws Exception {
//panthera pardus, leopard|0.6
//tiger, tigre|0.9
- Query q = QParser.getParser("panthera pardus tiger",req(params("df", "t_pick_best_boosted_foo","sow", "false"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:leopard)^0.6 | t_pick_best_boosted_foo:\"panthera pardus\") ((t_pick_best_boosted_foo:tigre)^0.9 | t_pick_best_boosted_foo:tiger)", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("panthera pardus tiger",req(params("df", field,"sow", "false"))).getQuery();
+ assertThat(q, booleanQuery(
+ disjunctionOf(
+ boosted(field, "leopard", 0.6f),
+ phraseQuery(field, "panthera pardus")
+ ),
+ disjunctionOf(
+ termQuery(field, "tiger"),
+ boosted(field, "tigre", 0.9f)
+ )
+ ));
q = QParser.getParser("panthera pardus tiger", req(params("df", "t_as_distinct_boosted_foo","sow", "false"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:leopard)^0.6 t_as_distinct_boosted_foo:\"panthera pardus\") ((t_as_distinct_boosted_foo:tigre)^0.9 t_as_distinct_boosted_foo:tiger)", q.toString());
@@ -1377,9 +1464,21 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testSynonymsBoost_multiConceptsQueryMultiTermSynonyms_shouldParseBoostedQuery() throws Exception {
//snow leopard, panthera uncia|0.9, big cat|0.8, white_leopard|0.6
//panthera onca => jaguar|0.95, big cat|0.85, black panther|0.65
- Query q = QParser.getParser("snow leopard panthera onca",req(params("df", "t_pick_best_boosted_foo","sow", "false"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:\"panthera uncia\")^0.9 | (t_pick_best_boosted_foo:\"big cat\")^0.8 | (t_pick_best_boosted_foo:white_leopard)^0.6 | t_pick_best_boosted_foo:\"snow leopard\")" +
- " ((t_pick_best_boosted_foo:jaguar)^0.95 | (t_pick_best_boosted_foo:\"big cat\")^0.85 | (t_pick_best_boosted_foo:\"black panther\")^0.65)", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("snow leopard panthera onca",req(params("df", field,"sow", "false"))).getQuery();
+ assertThat(q, booleanQuery(
+ disjunctionOf(
+ boosted(phraseQuery(field, "panthera uncia"), 0.9f),
+ boosted(phraseQuery(field, "big cat"), 0.8f),
+ boosted(field, "white_leopard", 0.6f),
+ phraseQuery(field, "snow leopard")
+ ),
+ disjunctionOf(
+ boosted(field, "jaguar", 0.95f),
+ boosted(phraseQuery(field, "big cat"), 0.85f),
+ boosted(phraseQuery(field, "black panther"), 0.65f)
+ )
+ ));
q = QParser.getParser("snow leopard panthera onca", req(params("df", "t_as_distinct_boosted_foo","sow", "false"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:\"panthera uncia\")^0.9 (t_as_distinct_boosted_foo:\"big cat\")^0.8 (t_as_distinct_boosted_foo:white_leopard)^0.6 t_as_distinct_boosted_foo:\"snow leopard\")" +
@@ -1392,11 +1491,21 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
}
public void testSynonymsBoost_edismaxBoost_shouldParseBoostedPhraseQuery() throws Exception {
- Query q = QParser.getParser("snow leopard lion","edismax",true, req(params("sow", "false","qf", "t_pick_best_boosted_foo^10"))).getQuery();
- assertEquals("+(" +
- "((((t_pick_best_boosted_foo:\"panthera uncia\")^0.9 | (t_pick_best_boosted_foo:\"big cat\")^0.8 | (t_pick_best_boosted_foo:white_leopard)^0.6 | t_pick_best_boosted_foo:\"snow leopard\"))^10.0)" +
- " ((((t_pick_best_boosted_foo:\"panthera leo\")^0.9 | (t_pick_best_boosted_foo:\"simba leo\")^0.8 | (t_pick_best_boosted_foo:kimba)^0.75))^10.0)" +
- ")", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("snow leopard lion","edismax",true, req(params("sow", "false","qf", field + "^10"))).getQuery();
+ assertThat(q, booleanQuery(booleanQuery(
+ disjunctionOf(boosted(disjunctionOf(
+ phraseQuery(field, "snow leopard"),
+ boosted(phraseQuery(field, "big cat"), 0.8f),
+ boosted(phraseQuery(field, "panthera uncia"), 0.9f),
+ boosted(termQuery(field, "white_leopard"), 0.6f)
+ ), 10)),
+ disjunctionOf(boosted(disjunctionOf(
+ boosted(termQuery(field, "kimba"), 0.75f),
+ boosted(phraseQuery(field, "simba leo"), 0.8f),
+ boosted(phraseQuery(field, "panthera leo"), 0.9f)
+ ), 10))),
+ BooleanClause.Occur.MUST));
q = QParser.getParser("snow leopard lion","edismax",true, req(params("sow", "false","qf", "t_as_distinct_boosted_foo^10"))).getQuery();
assertEquals("+(" +
@@ -1438,8 +1547,14 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testSynonymsBoost_BoostMissing_shouldAssignDefaultBoost() throws Exception {
//leopard, big cat|0.8, bagheera|0.9, panthera pardus|0.85
- Query q = QParser.getParser("leopard", req(params("df", "t_pick_best_boosted_foo"))).getQuery();
- assertEquals("((t_pick_best_boosted_foo:\"big cat\")^0.8 | (t_pick_best_boosted_foo:bagheera)^0.9 | (t_pick_best_boosted_foo:\"panthera pardus\")^0.85 | t_pick_best_boosted_foo:leopard)", q.toString());
+ String field = "t_pick_best_boosted_foo";
+ Query q = QParser.getParser("leopard", req(params("df", field))).getQuery();
+ assertThat(q, booleanQuery(disjunctionOf(
+ termQuery(field, "leopard"),
+ boosted(phraseQuery(field, "big cat"), 0.8f),
+ boosted(phraseQuery(field, "panthera pardus"), 0.85f),
+ boosted(termQuery(field, "bagheera"), 0.9f)
+ )));
q = QParser.getParser("leopard", req(params("df", "t_as_distinct_boosted_foo"))).getQuery();
assertEquals("((t_as_distinct_boosted_foo:\"big cat\")^0.8 (t_as_distinct_boosted_foo:bagheera)^0.9 (t_as_distinct_boosted_foo:\"panthera pardus\")^0.85 t_as_distinct_boosted_foo:leopard)", q.toString());
diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java
index f8033f4..ed0e612 100644
--- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java
+++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java
@@ -1031,10 +1031,8 @@ public abstract class SolrTestCaseJ4 extends SolrTestCase {
+ "\n\txml response was: " + response
+ "\n\trequest was:" + req.getParamString();
- log.error(msg);
- throw new RuntimeException(msg);
+ fail(msg);
}
-
} catch (XPathExpressionException e1) {
throw new RuntimeException("XPath is invalid", e1);
} catch (Exception e2) {
diff --git a/solr/test-framework/src/java/org/apache/solr/util/QueryMatchers.java b/solr/test-framework/src/java/org/apache/solr/util/QueryMatchers.java
new file mode 100644
index 0000000..0506bcf
--- /dev/null
+++ b/solr/test-framework/src/java/org/apache/solr/util/QueryMatchers.java
@@ -0,0 +1,232 @@
+/*
+ * 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.util;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.BoostQuery;
+import org.apache.lucene.search.DisjunctionMaxQuery;
+import org.apache.lucene.search.PhraseQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.hamcrest.internal.ReflectiveTypeFinder;
+
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.is;
+
+// TODO: This can be contributed to Lucene
+public class QueryMatchers {
+ private abstract static class SubTypeDiagnosingMatcher<Q extends Query> extends TypeSafeDiagnosingMatcher<Query> {
+ private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder("matchesExactType", 2, 0);
+
+ public SubTypeDiagnosingMatcher() {
+ super(TYPE_FINDER);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected final boolean matchesSafely(Query item, Description mismatchDescription) {
+ // We know that we're getting a Q instead of a Query here because we use our own TypeFinder
+ return matchesExactType((Q) item, mismatchDescription);
+ }
+
+ protected abstract boolean matchesExactType(Q item, Description mismatchDescription);
+ }
+
+ private QueryMatchers() {}
+
+ /**
+ * Matches is a Query object's toString result is equal to this query string.
+ * A useful shortcut when constructing complex queries with deterministic string repr
+ *<p>
+ * Note: Do not use this in place of DisjunctionMaxQuery, but safe to use for the disjunct clauses
+ * @param query the query string to match against
+ */
+ public static Matcher<Query> stringQuery(String query) {
+ return new TypeSafeDiagnosingMatcher<Query>() {
+ @Override
+ protected boolean matchesSafely(Query item, Description mismatchDescription) {
+ return is(query).matches(item.toString());
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(query);
+ }
+ };
+ }
+
+ public static Matcher<Query> termQuery(String field, String text) {
+ // TODO Use a better matcher for more descriptive results?
+ return is(new TermQuery(new Term(field, text)));
+ }
+
+ public static Matcher<Query> boosted(String field, String text, float boost) {
+ return boosted(termQuery(field, text), boost);
+ }
+
+ public static Matcher<Query> boosted(Matcher<? extends Query> query, float boost) {
+ return new SubTypeDiagnosingMatcher<BoostQuery>() {
+ @Override
+ protected boolean matchesExactType(BoostQuery item, Description mismatchDescription) {
+ boolean match = true;
+ mismatchDescription.appendText("was a BoostQuery ");
+ if (!query.matches(item.getQuery())) {
+ match = false;
+ mismatchDescription.appendText("with" + item.getQuery());
+ }
+ if (boost != item.getBoost()) {
+ match = false;
+ mismatchDescription.appendText("with boost " + item.getBoost());
+ }
+ return match;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("a BoostQuery ").appendDescriptionOf(query).appendText("^" + boost);
+ }
+ };
+ }
+
+ /**
+ * Create a phrase query matcher with a whitespace delimited sequence of terms
+ * @param field the field the phrase query should match against
+ * @param terms a whitespace delimited set of terms that should
+ */
+ public static Matcher<Query> phraseQuery(String field, String terms) {
+ // TODO Use a better matcher for more descriptive results?
+ return is(new PhraseQuery(field, terms.split(" ")));
+ }
+
+ private static Matcher<BooleanClause> shouldClause(Matcher<? extends Query> query) {
+ return booleanClause(query, BooleanClause.Occur.SHOULD);
+ }
+
+ private static Matcher<BooleanClause> mustClause(Matcher<? extends Query> query) {
+ return booleanClause(query, BooleanClause.Occur.MUST);
+ }
+
+ public static Matcher<BooleanClause> booleanClause(Matcher<? extends Query> query, BooleanClause.Occur occur) {
+ return new TypeSafeDiagnosingMatcher<BooleanClause>() {
+ @Override
+ protected boolean matchesSafely(BooleanClause item, Description mismatchDescription) {
+ boolean match = true;
+ mismatchDescription.appendText("was a BooleanClause ");
+ if (occur != item.getOccur()) {
+ match = false;
+ mismatchDescription.appendText("that " + item.getOccur().name() + " occur ");
+ }
+ if (!query.matches(item.getQuery())) {
+ match = false;
+ mismatchDescription.appendText("with " + item.getQuery());
+ query.describeMismatch(item.getQuery(), mismatchDescription);
+ }
+ return match;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(occur.toString()).appendText("(").appendDescriptionOf(query).appendText(")");
+ }
+ };
+ }
+
+ // TODO Figure out Varargs
+ public static Matcher<Query> booleanQuery(Matcher<Query> q1, BooleanClause.Occur occur) {
+ return _booleanQuery(contains(booleanClause(q1, occur)));
+ }
+
+ public static Matcher<Query> booleanQuery(Matcher<Query> q1, Matcher<Query> q2, BooleanClause.Occur occur) {
+ return _booleanQuery(containsInAnyOrder(booleanClause(q1, occur), booleanClause(q2, occur)));
+ }
+
+ public static Matcher<Query> booleanQuery(Matcher<Query> query) {
+ return _booleanQuery(contains(shouldClause(query)));
+ }
+
+ public static Matcher<Query> booleanQuery(Matcher<Query> c1, Matcher<Query> c2) {
+ return _booleanQuery(containsInAnyOrder(shouldClause(c1), shouldClause(c2)));
+ }
+
+ public static Matcher<Query> booleanQuery(Matcher<Query> c1, Matcher<Query> c2, Matcher<Query> c3) {
+ return _booleanQuery(containsInAnyOrder(shouldClause(c1), shouldClause(c2), shouldClause(c3)));
+ }
+
+ public static Matcher<Query> booleanQuery(Matcher<Query> c1, Matcher<Query> c2, Matcher<Query> c3, Matcher<Query> c4) {
+ return _booleanQuery(containsInAnyOrder(shouldClause(c1), shouldClause(c2), shouldClause(c3), shouldClause(c4)));
+ }
+
+ private static Matcher<Query> _booleanQuery(Matcher<Iterable<? extends BooleanClause>> matcher) {
+ return new SubTypeDiagnosingMatcher<BooleanQuery>() {
+ @Override
+ protected boolean matchesExactType(BooleanQuery item, Description mismatchDescription) {
+ if (matcher.matches(item.clauses())) return true;
+
+ mismatchDescription.appendText("was a BooleanQuery with ");
+ matcher.describeMismatch(item.clauses(), mismatchDescription);
+ return false;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("a BooleanQuery with ")
+ .appendDescriptionOf(matcher);
+ }
+ };
+ }
+
+ public static Matcher<Query> disjunctionOf(Matcher<Query> q1) {
+ return disjunctionQuery(contains(q1));
+ }
+
+ public static Matcher<Query> disjunctionOf(Matcher<Query> q1, Matcher<Query> q2) {
+ return disjunctionQuery(containsInAnyOrder(q1, q2));
+ }
+
+ public static Matcher<Query> disjunctionOf(Matcher<Query> q1, Matcher<Query> q2, Matcher<Query> q3) {
+ return disjunctionQuery(containsInAnyOrder(q1, q2, q3));
+ }
+
+ public static Matcher<Query> disjunctionOf(Matcher<Query> q1, Matcher<Query> q2, Matcher<Query> q3, Matcher<Query> q4) {
+ return disjunctionQuery(containsInAnyOrder(q1, q2, q3, q4));
+ }
+
+ private static Matcher<Query> disjunctionQuery(Matcher<Iterable<? extends Query>> disjuncts) {
+ return new SubTypeDiagnosingMatcher<DisjunctionMaxQuery>() {
+ @Override
+ protected boolean matchesExactType(DisjunctionMaxQuery item, Description mismatchDescription) {
+ if (disjuncts.matches(item.getDisjuncts())) return true;
+
+ mismatchDescription.appendText("was a DisjunctionMaxQuery with ");
+ disjuncts.describeMismatch(item.getDisjuncts(), mismatchDescription);
+ return false;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("a DisjunctionMaxQuery with ")
+ .appendDescriptionOf(disjuncts);
+ }
+ };
+ }
+}