You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2014/04/01 07:04:29 UTC
svn commit: r1583533 - in
/lucene/dev/branches/lucene5205/lucene/queryparser/src:
java/org/apache/lucene/queryparser/spans/
test/org/apache/lucene/queryparser/classic/
test/org/apache/lucene/queryparser/complexPhrase/
test/org/apache/lucene/queryparser...
Author: rmuir
Date: Tue Apr 1 05:04:29 2014
New Revision: 1583533
URL: http://svn.apache.org/r1583533
Log:
LUCENE-5205: Tim's test cleanup patch
Removed:
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestMultiAnalyzer.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanQPBasedOnQPTestBase.java
Modified:
lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanOnlyParser.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParser.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParserBase.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiAnalyzer.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestComplexPhraseSpanQuery.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanOnlyQueryParser.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanQueryParserLexer.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java
lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestCase.java
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanOnlyParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanOnlyParser.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanOnlyParser.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanOnlyParser.java Tue Apr 1 05:04:29 2014
@@ -38,11 +38,10 @@ import org.apache.lucene.util.Version;
* @see SpanQueryParser
*/
-public class SpanOnlyParser extends AbstractSpanQueryParser{
+public class SpanOnlyParser extends AbstractSpanQueryParser {
/**
* Initializes the SpanOnlyParser.
- * @param matchVersion
* @param f default field
* @param a analyzer to use
*/
@@ -52,7 +51,6 @@ public class SpanOnlyParser extends Abst
/**
* Initializes SpanOnlyParser.
- * @param matchVersion
* @param f default field
* @param a analyzer to use for full terms
* @param multitermAnalyzer analyzer to use for multiterm analysis
@@ -62,7 +60,7 @@ public class SpanOnlyParser extends Abst
}
@Override
- public Query parse(String s) throws ParseException{
+ public Query parse(String s) throws ParseException {
Query q = _parsePureSpan(getField(), s);
assert(q == null || q instanceof SpanQuery);
return q;
@@ -94,7 +92,7 @@ public class SpanOnlyParser extends Abst
}
- protected Query _parsePureSpan(String field, String queryString) throws ParseException{
+ protected Query _parsePureSpan(String field, String queryString) throws ParseException {
SpanQueryLexer lexer = new SpanQueryLexer();
List<SQPToken> tokens = lexer.getTokens(queryString);
SQPClause overallClause = new SQPOrClause(0, tokens.size());
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParser.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParser.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParser.java Tue Apr 1 05:04:29 2014
@@ -392,7 +392,12 @@ public class SpanQueryParser extends Abs
if (tmpField.equals("*") &&
tmpTerm instanceof SQPTerm &&
((SQPTerm)tmpTerm).getString().equals("*")) {
- return new MatchAllDocsQuery();
+ Query q = new MatchAllDocsQuery();
+ float boost = ((SQPBoostableToken)tmpTerm).getBoost();
+ if (boost != SpanQueryParserBase.UNSPECIFIED_BOOST){
+ q.setBoost(((SQPBoostableToken)tmpTerm).getBoost());
+ }
+ return q;
}
return null;
}
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParserBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParserBase.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParserBase.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/java/org/apache/lucene/queryparser/spans/SpanQueryParserBase.java Tue Apr 1 05:04:29 2014
@@ -99,7 +99,6 @@ abstract class SpanQueryParserBase exten
//if a full term is analyzed and the analyzer returns nothing,
//should a ParseException be thrown or should I just ignore the full token.
private boolean throwExceptionForEmptyTerm = false;
- private boolean lowercaseRegex = false;
////////
//Unsupported operations
@@ -173,7 +172,7 @@ abstract class SpanQueryParserBase exten
@Override
protected Query getRegexpQuery(String field, String termStr) throws ParseException {
- if (getLowercaseRegex()) {
+ if (getLowercaseExpandedTerms()) {
termStr = termStr.toLowerCase(getLocale());
}
Term t = new Term(field, termStr);
@@ -229,8 +228,6 @@ abstract class SpanQueryParserBase exten
/**
* Creates a new fuzzy term.
- * If minimumSimilarity is >= 1.0f, this rounds to avoid
- * exception for numEdits != whole number.
*
* @return fuzzy query
*/
@@ -412,28 +409,27 @@ abstract class SpanQueryParserBase exten
}
//if there was an exception during analysis, swallow it and
//try for lowercase
- if ((start == null && getAnalyzeRangeTerms()) ||
+ if ((start == null && getAnalyzeRangeTerms()) &&
getNormMultiTerms() == NORM_MULTI_TERMS.LOWERCASE) {
start = part1.toLowerCase(getLocale());
- } else {
+ } else if (start == null){
start = part1;
}
}
-
if (part2 == null) {
end = null;
} else {
if (getAnalyzeRangeTerms()) {
try {
- end = analyzeMultitermTermParseEx(field, part1).utf8ToString();
+ end = analyzeMultitermTermParseEx(field, part2).utf8ToString();
} catch (ParseException e) {
//swallow..doh!
}
}
- if ((end == null && getAnalyzeRangeTerms()) ||
+ if ((end == null && getAnalyzeRangeTerms()) &&
getNormMultiTerms() == NORM_MULTI_TERMS.LOWERCASE) {
end = part2.toLowerCase(getLocale());
- } else {
+ } else if (end == null) {
end = part2;
}
}
@@ -491,11 +487,9 @@ abstract class SpanQueryParserBase exten
}
}
- // if the user enters 2.4 for example, round it so that there won't be
- // an
- // illegalparameter exception
- if (minSimilarity >= 1.0f) {
- minSimilarity = (float) Math.round(minSimilarity);
+ // if the user enters 2.4 for example, throw parse exception
+ if (minSimilarity >= 1.0f && minSimilarity != (int) minSimilarity) {
+ throw new ParseException("Fractional edit distances are not allowed!");
}
int prefixLen = getFuzzyPrefixLength();
@@ -699,6 +693,7 @@ abstract class SpanQueryParserBase exten
if (nonEmpties.size() == 1) {
return nonEmpties.get(0);
}
+
SpanQuery[] ret = nonEmpties
.toArray(new SpanQuery[nonEmpties.size()]);
if (quoted || getAutoGeneratePhraseQueries() == true) {
@@ -755,10 +750,7 @@ abstract class SpanQueryParserBase exten
termAtt.fillBytesRef();
//if start is the same, treat it as a synonym...ignore end because
//of potential for shingles
- if (lastStart > -1 && offAtt.startOffset() == lastStart)
- //&& offAttr.endOffset() == lastEnd)
- {
-
+ if (lastStart > -1 && offAtt.startOffset() == lastStart) {
handleSyn(queries, (SpanTermQuery)newTermQuery(new Term(fieldName, BytesRef.deepCopyOf(bytes))));
} else {
queries.add((SpanTermQuery)newTermQuery(new Term(fieldName, BytesRef.deepCopyOf(bytes))));
@@ -1053,16 +1045,10 @@ abstract class SpanQueryParserBase exten
}
/**
- * Copied nearly exactly from FuzzyQuery's floatToEdits.
- * <p>
- * There are two differences:
- * <p>
- * <ol>
- * <li>FuzzyQuery's floatToEdits requires that the return value
- * be <= LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE<li>
- * <li>This adds a small amount so that nearly exact
- * hits don't get floored: 0.80 for termLen 5 should = 1</li>
- * <ol>
+ * Copied nearly exactly from FuzzyQuery's floatToEdits because
+ * FuzzyQuery's floatToEdits requires that the return value
+ * be <= LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE
+ *
* @return edits
*/
public static int unboundedFloatToEdits(float minimumSimilarity, int termLen) {
@@ -1071,15 +1057,8 @@ abstract class SpanQueryParserBase exten
} else if (minimumSimilarity == 0.0f) {
return 0; // 0 means exact, not infinite # of edits!
} else {
- return (int)(0.00001f+(1f-minimumSimilarity) * termLen);
+ return (int)((1f-minimumSimilarity) * termLen);
}
}
- public boolean getLowercaseRegex() {
- return lowercaseRegex;
- }
-
- public void setLowercaseRegex(boolean lowercaseRegex) {
- this.lowercaseRegex = lowercaseRegex;
- }
}
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiAnalyzer.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiAnalyzer.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiAnalyzer.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiAnalyzer.java Tue Apr 1 05:04:29 2014
@@ -113,7 +113,7 @@ public class TestMultiAnalyzer extends B
}
public void testPosIncrementAnalyzer() throws ParseException {
- QueryParser qp = new QueryParser(Version.LUCENE_40, "", new PosIncrementAnalyzer());
+ QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "", new PosIncrementAnalyzer());
assertEquals("quick brown", qp.parse("the quick brown").toString());
assertEquals("quick brown fox", qp.parse("the quick brown fox").toString());
}
@@ -122,7 +122,10 @@ public class TestMultiAnalyzer extends B
* Expands "multi" to "multi" and "multi2", both at the same position,
* and expands "triplemulti" to "triplemulti", "multi3", and "multi2".
*/
- private class MultiAnalyzer extends Analyzer {
+ protected class MultiAnalyzer extends Analyzer {
+
+ public MultiAnalyzer() {
+ }
@Override
public TokenStreamComponents createComponents(String fieldName) {
@@ -193,7 +196,11 @@ public class TestMultiAnalyzer extends B
* Analyzes "the quick brown" as: quick(incr=2) brown(incr=1).
* Does not work correctly for input other than "the quick brown ...".
*/
- private class PosIncrementAnalyzer extends Analyzer {
+ protected class PosIncrementAnalyzer extends Analyzer {
+
+ public PosIncrementAnalyzer() {
+ // TODO Auto-generated constructor stub
+ }
@Override
public TokenStreamComponents createComponents(String fieldName) {
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java Tue Apr 1 05:04:29 2014
@@ -37,7 +37,7 @@ import org.apache.lucene.util.LuceneTest
public class TestComplexPhraseQuery extends LuceneTestCase {
Directory rd;
- Analyzer analyzer;
+ protected Analyzer analyzer;
DocData docsContent[] = { new DocData("john smith", "1"),
new DocData("johathon smith", "2"),
@@ -47,36 +47,45 @@ public class TestComplexPhraseQuery exte
private IndexSearcher searcher;
private IndexReader reader;
- String defaultFieldName = "name";
+ protected String defaultFieldName = "name";
public void testComplexPhrases() throws Exception {
checkMatches("\"john smith\"", "1"); // Simple multi-term still works
checkMatches("\"j* smyth~\"", "1,2"); // wildcards and fuzzies are OK in
// phrases
- checkMatches("\"(jo* -john) smith\"", "2"); // boolean logic works
checkMatches("\"jo* smith\"~2", "1,2,3"); // position logic works.
checkMatches("\"jo* [sma TO smZ]\" ", "1,2"); // range queries supported
checkMatches("\"john\"", "1,3"); // Simple single-term still works
- checkMatches("\"(john OR johathon) smith\"", "1,2"); // boolean logic with
- // brackets works.
+
+ checkMatches("\"john nosuchword*\"", ""); // phrases with clauses producing
+ // empty sets
+
+ checkBadQuery("\"jo* id:1 smith\""); // mixing fields in a phrase is bad
+ }
+
+ public void testParserSpecificSyntax() throws Exception {
+ checkMatches("\"(jo* -john) smith\"", "2"); // boolean logic works
checkMatches("\"(jo* -john) smyth~\"", "2"); // boolean logic with
// brackets works.
// checkMatches("\"john -percival\"", "1"); // not logic doesn't work
// currently :(.
+ checkMatches("\"(john OR johathon) smith\"", "1,2"); // boolean logic with
+ // brackets works.
+ checkBadQuery("\"jo* \"smith\" \""); // phrases inside phrases is bad
- checkMatches("\"john nosuchword*\"", ""); // phrases with clauses producing
- // empty sets
- checkBadQuery("\"jo* id:1 smith\""); // mixing fields in a phrase is bad
- checkBadQuery("\"jo* \"smith\" \""); // phrases inside phrases is bad
}
- private void checkBadQuery(String qString) {
+ public Query getQuery(String qString) throws Exception {
QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
+ return qp.parse(qString);
+
+ }
+ protected void checkBadQuery(String qString) {
Throwable expected = null;
try {
- qp.parse(qString);
+ getQuery(qString);
} catch (Throwable e) {
expected = e;
}
@@ -84,12 +93,9 @@ public class TestComplexPhraseQuery exte
}
- private void checkMatches(String qString, String expectedVals)
+ protected void checkMatches(String qString, String expectedVals)
throws Exception {
- QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
- qp.setFuzzyPrefixLength(1); // usually a good idea
-
- Query q = qp.parse(qString);
+ Query q = getQuery(qString);
HashSet<String> expecteds = new HashSet<String>();
String[] vals = expectedVals.split(",");
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestComplexPhraseSpanQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestComplexPhraseSpanQuery.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestComplexPhraseSpanQuery.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestComplexPhraseSpanQuery.java Tue Apr 1 05:04:29 2014
@@ -17,151 +17,45 @@ package org.apache.lucene.queryparser.sp
* limitations under the License.
*/
-import java.util.HashSet;
-
-import org.apache.lucene.analysis.Analyzer;
-import org.apache.lucene.analysis.MockAnalyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.index.DirectoryReader;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.StoredDocument;
-import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.queryparser.complexPhrase.TestComplexPhraseQuery;
import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoreDoc;
-import org.apache.lucene.search.TopDocs;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.util.LuceneTestCase;
/**
- * Copied and pasted from TestComplexPhraseSpanQuery r1569314.
+ * Copied and pasted from TestComplexPhraseQuery r1569314.
* Had to make small changes in syntax.
*/
-public class TestComplexPhraseSpanQuery extends LuceneTestCase {
- Directory rd;
- Analyzer analyzer;
-
- DocData docsContent[] = { new DocData("john smith", "1"),
- new DocData("johathon smith", "2"),
- new DocData("john percival smith", "3"),
- new DocData("jackson waits tom", "4") };
-
- private IndexSearcher searcher;
- private IndexReader reader;
-
- String defaultFieldName = "name";
+public class TestComplexPhraseSpanQuery extends TestComplexPhraseQuery {
- public void testComplexPhrases() throws Exception {
- checkMatches("\"john smith\"", "1"); // Simple multi-term still works
- checkMatches("\"j* smyth~\"", "1,2"); // wildcards and fuzzies are OK in
- // phrases
- //WAS:
-// checkMatches("\"(jo* -john) smith\"", "2"); // boolean logic works
- checkMatches("\"[jo* john]!~ smith\"", "2"); // boolean logic works
- checkMatches("\"jo* smith\"~2", "1,2,3"); // position logic works.
- checkMatches("\"jo* [sma TO smZ]\" ", "1,2"); // range queries supported
- checkMatches("\"john\"", "1,3"); // Simple single-term still works
+ @Override
+ public Query getQuery(String qString) throws Exception {
+ SpanQueryParser p = new SpanQueryParser(TEST_VERSION_CURRENT,
+ defaultFieldName, analyzer);
+ return p.parse(qString);
+ }
+
+ @Override
+ public void testParserSpecificSyntax() throws Exception {
+ //can't have boolean operators within a SpanNear
+ //must rewrite as SpanNot !~ or ( OR ) clauses without the "OR"
+ checkMatches("\"[jo* john]!~ smith\"", "2");
+ checkMatches("\"(john johathon) smith\"", "1,2");
+ checkMatches("\"[jo* john]!~ smyth~\"", "2");
+ checkMatches("\"john percival\"!~2,2", "1");
+
+ //check multiterms with no hits
+ checkMatches("\"john nosuchword*\"", "");
+ checkMatches("\"john nosuchw?rd\"!~2,3", "1,3");
+ checkMatches("\"nosuchw?rd john\"!~2,3", "");
+ checkMatches("\"nosuchw?rd john\"", "");
//WAS:
-// checkMatches("\"(john OR johathon) smith\"", "1,2"); // boolean logic with
- checkMatches("\"(john johathon) smith\"", "1,2"); // boolean logic with
- // brackets works.
- //WAS:
-// checkMatches("\"(jo* -john) smyth~\"", "2"); // boolean logic with
- checkMatches("\"[jo* john]!~ smyth~\"", "2"); // boolean logic with
- // brackets works.
- //WAS:
- // checkMatches("\"john -percival\"", "1"); // not logic doesn't work
- checkMatches("\"john percival\"!~2,2", "1"); // not logic doesn't work
-
- checkMatches("\"john nosuchword*\"", ""); // phrases with clauses producing
- checkMatches("\"john nosuchw?rd\"!~2,3", "1,3"); // phrases with clauses producing
- checkMatches("\"nosuchw?rd john\"!~2,3", ""); // phrases with clauses producing
- checkMatches("\"nosuchw?rd john\"", ""); // phrases with clauses producing
-
- // empty sets
- checkBadQuery("\"jo* id:1 smith\""); // mixing fields in a phrase is bad
- //WAS:
- //checkBadQuery("\"jo* \"smith\" \""); // phrases inside phrases is bad
+ //checkBadQuery("\"jo* \"smith\" \"");
//IS: ignore test. SpanQueryParser will parse this as
//1) "jo* "
//2) smith
//3) " "
checkBadQuery("\"(jo* -john) smith\""); // can't have boolean operators in phrase
- }
- private void checkBadQuery(String qString) {
- SpanQueryParser qp = new SpanQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
- Throwable expected = null;
- try {
- qp.parse(qString);
- } catch (Throwable e) {
- expected = e;
- }
- assertNotNull("Expected parse error in " + qString, expected);
}
- private void checkMatches(String qString, String expectedVals)
- throws Exception {
- SpanQueryParser qp = new SpanQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
- qp.setFuzzyPrefixLength(1); // usually a good idea
-
- Query q = qp.parse(qString);
- System.out.println("QUERY: " + q.toString());
- HashSet<String> expecteds = new HashSet<String>();
- String[] vals = expectedVals.split(",");
- for (int i = 0; i < vals.length; i++) {
- if (vals[i].length() > 0)
- expecteds.add(vals[i]);
- }
-
- TopDocs td = searcher.search(q, 10);
- ScoreDoc[] sd = td.scoreDocs;
- for (int i = 0; i < sd.length; i++) {
- StoredDocument doc = searcher.doc(sd[i].doc);
- String id = doc.get("id");
- assertTrue(qString + "matched doc#" + id + " not expected", expecteds
- .contains(id));
- expecteds.remove(id);
- }
- assertEquals(qString + " missing some matches ", 0, expecteds.size());
- }
-
- @Override
- public void setUp() throws Exception {
- super.setUp();
-
- analyzer = new MockAnalyzer(random());
- rd = newDirectory();
- IndexWriter w = new IndexWriter(rd, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
- for (int i = 0; i < docsContent.length; i++) {
- Document doc = new Document();
- doc.add(newTextField("name", docsContent[i].name, Field.Store.YES));
- doc.add(newTextField("id", docsContent[i].id, Field.Store.YES));
- w.addDocument(doc);
- }
- w.close();
- reader = DirectoryReader.open(rd);
- searcher = newSearcher(reader);
- }
-
- @Override
- public void tearDown() throws Exception {
- reader.close();
- rd.close();
- super.tearDown();
- }
-
- static class DocData {
- String name;
-
- String id;
-
- public DocData(String name, String id) {
- super();
- this.name = name;
- this.id = id;
- }
- }
}
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanOnlyQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanOnlyQueryParser.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanOnlyQueryParser.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanOnlyQueryParser.java Tue Apr 1 05:04:29 2014
@@ -129,7 +129,7 @@ public class TestSpanOnlyQueryParser ext
"\u666E \u6797 \u65AF \u987F \u5927 \u5B66",
"reg/exp",
"/regex/",
- "fuzzy~0.6",
+ "fuzzy~2",
"wil*card",
"wil?card",
"prefi*",
@@ -341,27 +341,13 @@ public class TestSpanOnlyQueryParser ext
countSpansDocs(p, "crown~3,1", 0, 0);
countSpansDocs(p, "brwn~1,1", 3, 2);
- p.setFuzzyMinSim(0.6f);
- countSpansDocs(p, "brwon~0.80", 3, 2);
-
- p.setFuzzyMinSim(0.85f);
- countSpansDocs(p, "brwon~0.80", 0, 0);
-
- p.setFuzzyMinSim(0.80f);
+ p.setFuzzyMinSim(0.79f);
countSpansDocs(p, "brwon~2", 3, 2);
- p.setFuzzyMinSim(0.60f);
- //this requires edit = 3
- testOffsetForSingleSpanMatch(p, "abcdefgh~0.60", 3, 0, 1);
-
- p.setFuzzyMinSim(0.65f);
- //this requires edit = 3, 63%
- countSpansDocs(p, "abcdefgh~0.60", 0, 0);
-
//fuzzy val of 0 should yield straight SpanTermQuery
- Query q = p.parse("brown~0.0");
- assertTrue("fuzzy val = 0.0", q instanceof SpanTermQuery);
+ Query q = p.parse("brown~0");
+ assertTrue("fuzzy val = 0", q instanceof SpanTermQuery);
q = p.parse("brown~0");
assertTrue("fuzzy val = 0", q instanceof SpanTermQuery);
}
@@ -495,15 +481,16 @@ public class TestSpanOnlyQueryParser ext
assertEquals(true, exc);
}
- public void testQuotedSingleTerm() throws Exception{
+ public void testQuotedSingleTerm() throws Exception {
SpanOnlyParser p = new SpanOnlyParser(TEST_VERSION_CURRENT, FIELD, noStopAnalyzer);
- String[] quoteds = new String[]{
+ String[] quoteds = new String[] {
"/regex/",
- "fuzzy~0.6",
+ "fuzzy~2",
"wil*card",
"wil?card",
- "prefi*"};
+ "prefi*"
+ };
for (String q : quoteds) {
countSpansDocs(p, "\""+q+"\"", 1, 1);
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanQueryParserLexer.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanQueryParserLexer.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanQueryParserLexer.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/spans/TestSpanQueryParserLexer.java Tue Apr 1 05:04:29 2014
@@ -755,7 +755,7 @@ public class TestSpanQueryParserLexer ex
new SQPTerm(s, false)
);
}
-
+
private void executeSingleTokenTest(String q, int targetOffset, SQPToken truth)
throws ParseException {
List<SQPToken> tokens = lexer.getTokens(q);
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java Tue Apr 1 05:04:29 2014
@@ -109,7 +109,7 @@ public abstract class QueryParserTestBas
expected.add(new Term("field", "ä¸"));
expected.add(new Term("field", "å½"));
- assertEquals(expected, getQuery("\"ä¸å½\"^0.5", analyzer));
+ assertQueryEquals(expected, getQuery("\"ä¸å½\"^0.5", analyzer));
}
public void testCJKSloppyPhrase() throws Exception {
@@ -121,7 +121,7 @@ public abstract class QueryParserTestBas
expected.add(new Term("field", "ä¸"));
expected.add(new Term("field", "å½"));
- assertEquals(expected, getQuery("\"ä¸å½\"~3", analyzer));
+ assertQueryEquals(expected, getQuery("\"ä¸å½\"~3", analyzer));
}
public void testAutoGeneratePhraseQueriesOn() throws Exception {
@@ -133,7 +133,7 @@ public abstract class QueryParserTestBas
expected.add(new Term("field", "å½"));
CommonQueryParserConfiguration qp = getParserConfig(analyzer);
setAutoGeneratePhraseQueries(qp, true);
- assertEquals(expected, getQuery("ä¸å½",qp));
+ assertQueryEquals(expected, getQuery("ä¸å½",qp));
}
public void testSimple() throws Exception {
@@ -146,40 +146,22 @@ public abstract class QueryParserTestBas
assertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
assertQueryEquals("a AND NOT b", null, "+a -b");
assertQueryEquals("a AND -b", null, "+a -b");
- assertQueryEquals("a AND !b", null, "+a -b");
- assertQueryEquals("a && b", null, "+a +b");
assertQueryEquals("a OR b", null, "a b");
- assertQueryEquals("a || b", null, "a b");
- assertQueryEquals("a OR !b", null, "a -b");
assertQueryEquals("a OR -b", null, "a -b");
assertQueryEquals("+term -term term", null, "+term -term term");
assertQueryEquals("foo:term AND field:anotherTerm", null,
"+foo:term +anotherterm");
- assertQueryEquals("term AND \"phrase phrase\"", null,
- "+term +\"phrase phrase\"");
- assertQueryEquals("\"hello there\"", null, "\"hello there\"");
- assertTrue(getQuery("a AND b") instanceof BooleanQuery);
- assertTrue(getQuery("hello") instanceof TermQuery);
- assertTrue(getQuery("\"hello there\"") instanceof PhraseQuery);
+ assertInstanceOf(getQuery("a AND b"), BooleanQuery.class);
+ assertInstanceOf(getQuery("hello"), TermQuery.class);
+ assertInstanceOf(getQuery("\"hello there\""), PhraseQuery.class);
assertQueryEquals("germ term^2.0", null, "germ term^2.0");
assertQueryEquals("(term)^2.0", null, "term^2.0");
assertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");
assertQueryEquals("term^2.0", null, "term^2.0");
assertQueryEquals("term^2", null, "term^2.0");
- assertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");
- assertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");
-
- assertQueryEquals("(foo OR bar) AND (baz OR boo)", null,
- "+(foo bar) +(baz boo)");
- assertQueryEquals("((a OR b) AND NOT c) OR d", null,
- "(+(a b) -c) d");
- assertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)", null,
- "+(apple \"steve jobs\") -(foo bar baz)");
- assertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"", null,
- "+(title:dog title:cat) -author:\"bob dole\"");
}
// FIXME: enhance MockAnalyzer to be able to support testing the empty string.
@@ -208,11 +190,17 @@ public abstract class QueryParserTestBas
}
public void testSlop() throws Exception {
- assertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");
- assertQueryEquals("\"term germ\"~2 flork", null, "\"term germ\"~2 flork");
+ PhraseQuery q = new PhraseQuery();
+ q.add(new Term("field", "term"));
+ q.add(new Term("field", "germ"));
+ q.setSlop(2);
+ assertQueryEquals(q, getQuery("\"term germ\"~2"));
+
+ q.setBoost(2.0f);
+ assertQueryEquals(q, getQuery("\"term germ\"~2^2"));
+
assertQueryEquals("\"term\"~2", null, "term");
assertQueryEquals("\" \"~2 germ", null, "germ");
- assertQueryEquals("\"term germ\"~2^2", null, "\"term germ\"~2^2.0");
}
public void testNumber() throws Exception {
@@ -234,24 +222,20 @@ public abstract class QueryParserTestBas
assertQueryEquals("term~1", null, "term~1");
assertQueryEquals("term~0.7", null, "term~1");
assertQueryEquals("term~^3", null, "term~2^3.0");
- assertQueryEquals("term^3~", null, "term~2^3.0");
+
assertQueryEquals("term*germ", null, "term*germ");
assertQueryEquals("term*germ^3", null, "term*germ^3.0");
- assertTrue(getQuery("term*") instanceof PrefixQuery);
- assertTrue(getQuery("term*^2") instanceof PrefixQuery);
- assertTrue(getQuery("term~") instanceof FuzzyQuery);
- assertTrue(getQuery("term~0.7") instanceof FuzzyQuery);
- FuzzyQuery fq = (FuzzyQuery)getQuery("term~0.7");
- assertEquals(1, fq.getMaxEdits());
- assertEquals(FuzzyQuery.defaultPrefixLength, fq.getPrefixLength());
- fq = (FuzzyQuery)getQuery("term~");
- assertEquals(2, fq.getMaxEdits());
- assertEquals(FuzzyQuery.defaultPrefixLength, fq.getPrefixLength());
+ assertInstanceOf(getQuery("term*"), PrefixQuery.class);
+ assertInstanceOf(getQuery("term*^2"), PrefixQuery.class);
+ assertInstanceOf(getQuery("term~"), FuzzyQuery.class);
+ assertInstanceOf(getQuery("term~0.7"),FuzzyQuery.class);
+ assertFuzzyQueryEquals("field", "term", 1, FuzzyQuery.defaultPrefixLength, getQuery("term~0.7"));
+ assertFuzzyQueryEquals("field", "term", 2, FuzzyQuery.defaultPrefixLength, getQuery("term~"));
assertParseException("term~1.1"); // value > 1, throws exception
- assertTrue(getQuery("term*germ") instanceof WildcardQuery);
+ assertInstanceOf(getQuery("term*germ"), WildcardQuery.class);
// Tests to see that wild card terms are (or are not) properly
// lower-cased with propery parser configuration
@@ -311,9 +295,9 @@ public abstract class QueryParserTestBas
public void testLeadingWildcardType() throws Exception {
CommonQueryParserConfiguration cqpC = getParserConfig(null);
cqpC.setAllowLeadingWildcard(true);
- assertEquals(WildcardQuery.class, getQuery("t*erm*",cqpC).getClass());
- assertEquals(WildcardQuery.class, getQuery("?term*",cqpC).getClass());
- assertEquals(WildcardQuery.class, getQuery("*term*",cqpC).getClass());
+ assertInstanceOf(getQuery("t*erm*",cqpC), WildcardQuery.class);
+ assertInstanceOf(getQuery("?term*",cqpC), WildcardQuery.class);
+ assertInstanceOf(getQuery("*term*",cqpC), WildcardQuery.class);
}
public void testQPA() throws Exception {
@@ -329,10 +313,7 @@ public abstract class QueryParserTestBas
assertQueryEquals("term -(stop) term", qpAnalyzer, "term term");
assertQueryEquals("drop AND stop AND roll", qpAnalyzer, "+drop +roll");
- assertQueryEquals("term phrase term", qpAnalyzer,
- "term (phrase1 phrase2) term");
- assertQueryEquals("term AND NOT phrase term", qpAnalyzer,
- "+term -(phrase1 phrase2) term");
+
assertQueryEquals("stop^3", qpAnalyzer, "");
assertQueryEquals("stop", qpAnalyzer, "");
assertQueryEquals("(stop)^3", qpAnalyzer, "");
@@ -341,15 +322,9 @@ public abstract class QueryParserTestBas
assertQueryEquals("((stop)^3)", qpAnalyzer, "");
assertQueryEquals("(stop)", qpAnalyzer, "");
assertQueryEquals("((stop))", qpAnalyzer, "");
- assertTrue(getQuery("term term term", qpAnalyzer) instanceof BooleanQuery);
- assertTrue(getQuery("term +stop", qpAnalyzer) instanceof TermQuery);
+ assertInstanceOf(getQuery("term term term", qpAnalyzer), BooleanQuery.class);
+ assertInstanceOf(getQuery("term +stop", qpAnalyzer), TermQuery.class);
- CommonQueryParserConfiguration cqpc = getParserConfig(qpAnalyzer);
- setDefaultOperatorAND(cqpc);
- assertQueryEquals(cqpc, "field", "term phrase term",
- "+term +(+phrase1 +phrase2) +term");
- assertQueryEquals(cqpc, "field", "phrase",
- "+phrase1 +phrase2");
}
public void testRange() throws Exception {
@@ -357,12 +332,6 @@ public abstract class QueryParserTestBas
assertQueryEquals("[ a TO z}", null, "[a TO z}");
assertQueryEquals("{ a TO z]", null, "{a TO z]");
- assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((TermRangeQuery)getQuery("[ a TO z]")).getRewriteMethod());
-
- CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
-
- qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
- assertEquals(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE,((TermRangeQuery)getQuery("[ a TO z]", qp)).getRewriteMethod());
// test open ranges
assertQueryEquals("[ a TO * ]", null, "[a TO *]");
@@ -379,10 +348,6 @@ public abstract class QueryParserTestBas
assertQueryEquals("{ a TO z}", null, "{a TO z}");
assertQueryEquals("{ a TO z }", null, "{a TO z}");
assertQueryEquals("{ a TO z }^2.0", null, "{a TO z}^2.0");
- assertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
- assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
- assertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
- assertQueryEquals("gack ( bar blar { a TO z}) ", null, "gack (bar blar {a TO z})");
assertQueryEquals("[* TO Z]",null,"[* TO z]");
assertQueryEquals("[A TO *]",null,"[a TO *]");
@@ -484,14 +449,10 @@ public abstract class QueryParserTestBas
assertQueryEquals("[ a\\: TO a\\~ ]", null, "[a: TO a~]");
assertQueryEquals("[ a\\\\ TO a\\* ]", null, "[a\\ TO a*]");
- assertQueryEquals("[\"c\\:\\\\temp\\\\\\~foo0.txt\" TO \"c\\:\\\\temp\\\\\\~foo9.txt\"]", a,
- "[c:\\temp\\~foo0.txt TO c:\\temp\\~foo9.txt]");
assertQueryEquals("a\\\\\\+b", a, "a\\+b");
assertQueryEquals("a \\\"b c\\\" d", a, "a \"b c\" d");
- assertQueryEquals("\"a \\\"b c\\\" d\"", a, "\"a \"b c\" d\"");
- assertQueryEquals("\"a \\+b c d\"", a, "\"a +b c d\"");
assertQueryEquals("c\\:\\\\temp\\\\\\~foo.txt", a, "c:\\temp\\~foo.txt");
@@ -501,7 +462,6 @@ public abstract class QueryParserTestBas
assertQueryEquals("a\\u0062c", a, "abc");
assertQueryEquals("XY\\u005a", a, "XYZ");
assertQueryEquals("XY\\u005A", a, "XYZ");
- assertQueryEquals("\"a \\\\\\u0028\\u0062\\\" c\"", a, "\"a \\(b\" c\"");
assertParseException("XY\\u005G"); // test non-hex character in escaped unicode sequence
assertParseException("XY\\u005"); // test incomplete escaped unicode sequence
@@ -611,7 +571,7 @@ public abstract class QueryParserTestBas
q = getQuery("the^3", qp2);
// "the" is a stop word so the result is an empty query:
assertNotNull(q);
- assertEquals("", q.toString());
+ assertEmpty(q);
assertEquals(1.0f, q.getBoost(), 0.01f);
}
@@ -645,55 +605,47 @@ public abstract class QueryParserTestBas
public void testEscapedWildcard() throws Exception {
CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
WildcardQuery q = new WildcardQuery(new Term("field", "foo\\?ba?r"));
- assertEquals(q, getQuery("foo\\?ba?r", qp));
+ assertQueryEquals(q, getQuery("foo\\?ba?r", qp));
}
public void testRegexps() throws Exception {
CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
RegexpQuery q = new RegexpQuery(new Term("field", "[a-z][123]"));
- assertEquals(q, getQuery("/[a-z][123]/",qp));
+ assertQueryEquals(q, getQuery("/[a-z][123]/",qp));
qp.setLowercaseExpandedTerms(true);
- assertEquals(q, getQuery("/[A-Z][123]/",qp));
+ assertQueryEquals(q, getQuery("/[A-Z][123]/",qp));
q.setBoost(0.5f);
- assertEquals(q, getQuery("/[A-Z][123]/^0.5",qp));
+ assertQueryEquals(q, getQuery("/[A-Z][123]/^0.5",qp));
qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
q.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
- assertTrue(getQuery("/[A-Z][123]/^0.5",qp) instanceof RegexpQuery);
- assertEquals(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE, ((RegexpQuery)getQuery("/[A-Z][123]/^0.5",qp)).getRewriteMethod());
- assertEquals(q, getQuery("/[A-Z][123]/^0.5",qp));
+ assertInstanceOf(getQuery("/[A-Z][123]/^0.5",qp), RegexpQuery.class);
+ assertQueryEquals(q, getQuery("/[A-Z][123]/^0.5",qp));
qp.setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
- Query escaped = new RegexpQuery(new Term("field", "[a-z]\\/[123]"));
- assertEquals(escaped, getQuery("/[a-z]\\/[123]/",qp));
Query escaped2 = new RegexpQuery(new Term("field", "[a-z]\\*[123]"));
- assertEquals(escaped2, getQuery("/[a-z]\\*[123]/",qp));
+ assertQueryEquals(escaped2, getQuery("/[a-z]\\*[123]/",qp));
- BooleanQuery complex = new BooleanQuery();
- complex.add(new RegexpQuery(new Term("field", "[a-z]\\/[123]")), Occur.MUST);
- complex.add(new TermQuery(new Term("path", "/etc/init.d/")), Occur.MUST);
- complex.add(new TermQuery(new Term("field", "/etc/init[.]d/lucene/")), Occur.SHOULD);
- assertEquals(complex, getQuery("/[a-z]\\/[123]/ AND path:\"/etc/init.d/\" OR \"/etc\\/init\\[.\\]d/lucene/\" ",qp));
Query re = new RegexpQuery(new Term("field", "http.*"));
- assertEquals(re, getQuery("field:/http.*/",qp));
- assertEquals(re, getQuery("/http.*/",qp));
+ assertQueryEquals(re, getQuery("field:/http.*/",qp));
+ assertQueryEquals(re, getQuery("/http.*/",qp));
re = new RegexpQuery(new Term("field", "http~0.5"));
- assertEquals(re, getQuery("field:/http~0.5/",qp));
- assertEquals(re, getQuery("/http~0.5/",qp));
+ assertQueryEquals(re, getQuery("field:/http~0.5/",qp));
+ assertQueryEquals(re, getQuery("/http~0.5/",qp));
re = new RegexpQuery(new Term("field", "boo"));
- assertEquals(re, getQuery("field:/boo/",qp));
- assertEquals(re, getQuery("/boo/",qp));
+ assertQueryEquals(re, getQuery("field:/boo/",qp));
+ assertQueryEquals(re, getQuery("/boo/",qp));
- assertEquals(new TermQuery(new Term("field", "/boo/")), getQuery("\"/boo/\"",qp));
- assertEquals(new TermQuery(new Term("field", "/boo/")), getQuery("\\/boo\\/",qp));
+ assertQueryEquals(new TermQuery(new Term("field", "/boo/")), getQuery("\"/boo/\"",qp));
+ assertQueryEquals(new TermQuery(new Term("field", "/boo/")), getQuery("\\/boo\\/",qp));
BooleanQuery two = new BooleanQuery();
two.add(new RegexpQuery(new Term("field", "foo")), Occur.SHOULD);
two.add(new RegexpQuery(new Term("field", "bar")), Occur.SHOULD);
- assertEquals(two, getQuery("field:/foo/ field:/bar/",qp));
- assertEquals(two, getQuery("/foo/ /bar/",qp));
+ assertQueryEquals(two, getQuery("field:/foo/ field:/bar/",qp));
+ assertQueryEquals(two, getQuery("/foo/ /bar/",qp));
}
public void testStopwords() throws Exception {
@@ -701,14 +653,14 @@ public abstract class QueryParserTestBas
CommonQueryParserConfiguration qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, stopSet));
Query result = getQuery("field:the OR field:foo",qp);
assertNotNull("result is null and it shouldn't be", result);
- assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery);
- assertTrue(((BooleanQuery) result).clauses().size() + " does not equal: " + 0, ((BooleanQuery) result).clauses().size() == 0);
+ assertInstanceOf(result, BooleanQuery.class);
+ assertEmpty(result);
result = getQuery("field:woo OR field:the",qp);
assertNotNull("result is null and it shouldn't be", result);
- assertTrue("result is not a TermQuery", result instanceof TermQuery);
+ assertInstanceOf(result, TermQuery.class);
result = getQuery("(fieldX:xxxxx OR fieldy:xxxxxxxx)^2 AND (fieldx:the OR fieldy:foo)",qp);
assertNotNull("result is null and it shouldn't be", result);
- assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery);
+ assertInstanceOf(result, BooleanQuery.class);
if (VERBOSE) System.out.println("Result: " + result);
assertTrue(((BooleanQuery) result).clauses().size() + " does not equal: " + 2, ((BooleanQuery) result).clauses().size() == 2);
}
@@ -720,12 +672,9 @@ public abstract class QueryParserTestBas
// 0 2 5 7 8
int expectedPositions[] = {1,3,4,6,9};
PhraseQuery pq = (PhraseQuery) getQuery(qtxt,qp);
- //System.out.println("Query text: "+qtxt);
- //System.out.println("Result: "+pq);
Term t[] = pq.getTerms();
int pos[] = pq.getPositions();
for (int i = 0; i < t.length; i++) {
- //System.out.println(i+". "+t[i]+" pos: "+pos[i]);
assertEquals("term "+i+" = "+t[i]+" has wrong term-position!",expectedPositions[i],pos[i]);
}
}
@@ -735,8 +684,8 @@ public abstract class QueryParserTestBas
assertEquals(new MatchAllDocsQuery(), getQuery("*:*",qp));
assertEquals(new MatchAllDocsQuery(), getQuery("(*:*)",qp));
BooleanQuery bq = (BooleanQuery)getQuery("+*:* -*:*",qp);
- assertTrue(bq.getClauses()[0].getQuery() instanceof MatchAllDocsQuery);
- assertTrue(bq.getClauses()[1].getQuery() instanceof MatchAllDocsQuery);
+ assertInstanceOf(bq.getClauses()[0].getQuery(), MatchAllDocsQuery.class);
+ assertInstanceOf(bq.getClauses()[1].getQuery(), MatchAllDocsQuery.class);
}
// LUCENE-2002: make sure defaults for StandardAnalyzer's
@@ -766,12 +715,12 @@ public abstract class QueryParserTestBas
setAnalyzeRangeTerms(qp, true);
Query expected = TermRangeQuery.newStringRange(getDefaultField(), "collatedabc", "collateddef", true, true);
Query actual = getQuery("[abc TO def]", qp);
- assertEquals(expected, actual);
+ assertQueryEquals(expected, actual);
}
public void testDistanceAsEditsParsing() throws Exception {
- FuzzyQuery q = (FuzzyQuery) getQuery("foobar~2",new MockAnalyzer(random()));
- assertEquals(2, q.getMaxEdits());
+ FuzzyQuery expected = new FuzzyQuery(new Term("field", "foobar"), 2);
+ assertQueryEquals(expected, getQuery("foobar~2",new MockAnalyzer(random())));
}
public void testPhraseQueryToString() throws Exception {
@@ -806,7 +755,7 @@ public abstract class QueryParserTestBas
for (int j = 0; j < prefixQueries[i].length; j++) {
String queryString = prefixQueries[i][j];
Query q = getQuery(queryString,qp);
- assertEquals(PrefixQuery.class, q.getClass());
+ assertInstanceOf(q, PrefixQuery.class);
}
}
@@ -815,7 +764,7 @@ public abstract class QueryParserTestBas
for (int j = 0; j < wildcardQueries[i].length; j++) {
String qtxt = wildcardQueries[i][j];
Query q = getQuery(qtxt,qp);
- assertEquals(WildcardQuery.class, q.getClass());
+ assertInstanceOf(q, WildcardQuery.class);
}
}
setDefaultField(oldDefaultField);
@@ -845,7 +794,7 @@ public abstract class QueryParserTestBas
// test parsing with non-default boost
MatchAllDocsQuery query = new MatchAllDocsQuery();
query.setBoost(2.3f);
- assertEquals(query, getQuery(query.toString(),qp));
+ assertQueryEquals(query, getQuery(query.toString(),qp));
setDefaultField(oldDefaultField);
}
@@ -859,4 +808,86 @@ public abstract class QueryParserTestBas
q.add(new TermQuery(new Term("field2", "z")), Occur.MUST);
assertEquals(q, getQuery(query, new MockAnalyzer(random())));
}
+
+ //string query equality tests that have to be rewritten
+ //if parser is generating a SpanQuery
+ public void testParserSpecificQuery() throws Exception {
+
+ //testSimple
+ assertQueryEquals("a AND !b", null, "+a -b");
+ assertQueryEquals("a && b", null, "+a +b");
+ assertQueryEquals("a || b", null, "a b");
+ assertQueryEquals("a OR !b", null, "a -b");
+
+ assertQueryEquals("term AND \"phrase phrase\"", null,
+ "+term +\"phrase phrase\"");
+ assertQueryEquals("\"hello there\"", null, "\"hello there\"");
+ assertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");
+ assertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");
+
+ assertQueryEquals("(foo OR bar) AND (baz OR boo)", null,
+ "+(foo bar) +(baz boo)");
+ assertQueryEquals("((a OR b) AND NOT c) OR d", null,
+ "(+(a b) -c) d");
+ assertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)", null,
+ "+(apple \"steve jobs\") -(foo bar baz)");
+ assertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"", null,
+ "+(title:dog title:cat) -author:\"bob dole\"");
+
+
+ //testRegexps
+ CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
+ Query escaped = new RegexpQuery(new Term("field", "[a-z]\\/[123]"));
+ assertQueryEquals(escaped, getQuery("/[a-z]\\/[123]/",qp));
+
+ BooleanQuery complex = new BooleanQuery();
+ complex.add(new RegexpQuery(new Term("field", "[a-z]\\/[123]")), Occur.MUST);
+ complex.add(new TermQuery(new Term("path", "/etc/init.d/")), Occur.MUST);
+ complex.add(new TermQuery(new Term("field", "/etc/init[.]d/lucene/")), Occur.SHOULD);
+ assertQueryEquals(complex, getQuery("/[a-z]\\/[123]/ AND path:\"/etc/init.d/\" OR \"/etc\\/init\\[.\\]d/lucene/\" ",qp));
+ qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
+ qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
+ assertEquals(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE, ((RegexpQuery)getQuery("/[A-Z][123]/^0.5",qp)).getRewriteMethod());
+
+ //testWildcard
+ assertQueryEquals("term^3~", null, "term~2^3.0");
+
+
+ //testRange
+ assertEquals(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, ((TermRangeQuery)getQuery("[ a TO z]")).getRewriteMethod());
+ qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
+ qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
+ assertEquals(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE,((TermRangeQuery)getQuery("[ a TO z]", qp)).getRewriteMethod());
+ assertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
+ assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
+ assertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
+ assertQueryEquals("gack ( bar blar { a TO z}) ", null, "gack (bar blar {a TO z})");
+
+ //testSlop
+ assertQueryEquals("\"term germ\"~2 flork", null, "\"term germ\"~2 flork");
+
+ //testEscaped
+ Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
+ assertQueryEquals("[\"c\\:\\\\temp\\\\\\~foo0.txt\" TO \"c\\:\\\\temp\\\\\\~foo9.txt\"]", a,
+ "[c:\\temp\\~foo0.txt TO c:\\temp\\~foo9.txt]");
+ assertQueryEquals("\"a \\\"b c\\\" d\"", a, "\"a \"b c\" d\"");
+ assertQueryEquals("\"a \\+b c d\"", a, "\"a +b c d\"");
+ assertQueryEquals("\"a \\\\\\u0028\\u0062\\\" c\"", a, "\"a \\(b\" c\"");
+
+ // LUCENE-1189
+ assertQueryEquals("(\"a\\\\\") or (\"b\")", a ,"a\\ or b");
+
+ //testQPA
+ assertQueryEquals("term phrase term", qpAnalyzer,
+ "term (phrase1 phrase2) term");
+ assertQueryEquals("term AND NOT phrase term", qpAnalyzer,
+ "+term -(phrase1 phrase2) term");
+
+ CommonQueryParserConfiguration cqpc = getParserConfig(qpAnalyzer);
+ setDefaultOperatorAND(cqpc);
+ assertQueryEquals(cqpc, "field", "term phrase term",
+ "+term +(+phrase1 +phrase2) +term");
+ assertQueryEquals(cqpc, "field", "phrase",
+ "+phrase1 +phrase2");
+ }
}
Modified: lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestCase.java?rev=1583533&r1=1583532&r2=1583533&view=diff
==============================================================================
--- lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestCase.java (original)
+++ lucene/dev/branches/lucene5205/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestCase.java Tue Apr 1 05:04:29 2014
@@ -38,6 +38,7 @@ import org.apache.lucene.document.DateTo
import org.apache.lucene.queryparser.classic.QueryParserBase;
import org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration;
import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.AfterClass;
@@ -309,6 +310,11 @@ public abstract class QueryParserTestCas
Query q = getQuery(query, cqpC);
assertEquals(result, q.toString(field));
}
+
+ public void assertQueryEquals(Query expected, Query test) {
+ assertEquals(expected.toString(), test.toString());
+ }
+
public void assertEscapedQueryEquals(String query, Analyzer a, String result) throws Exception {
assertEquals(result, QueryParserBase.escape(query));
@@ -331,6 +337,27 @@ public abstract class QueryParserTestCas
assertEquals(result, q.toString("field"));
}
+ public void assertFuzzyQueryEquals(String field, String term, int maxEdits, int prefixLen, Query query) {
+ assert(query instanceof FuzzyQuery);
+ FuzzyQuery fq = (FuzzyQuery)query;
+ assertEquals(field, fq.getField());
+ assertEquals(term, fq.getTerm().text());
+ assertEquals(maxEdits, fq.getMaxEdits());
+ assertEquals(prefixLen, fq.getPrefixLength());
+ }
+
+ @SuppressWarnings("rawtypes")
+ public void assertInstanceOf(Query q, Class other) {
+ assertTrue(q.getClass().isAssignableFrom(other));
+ }
+
+ public void assertEmpty(Query q) {
+ boolean e = false;
+ if (q instanceof BooleanQuery && ((BooleanQuery)q).getClauses().length == 0) {
+ e = true;
+ }
+ assertTrue("Empty: "+q.toString(), e);
+ }
public Query getQueryDOA(String query, Analyzer a) throws Exception {
if (a == null) {
a = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);