* prefix queries are not parsed into wild card queries, and viceversa.
* @throws Exception
*/
public void testParsingAndSearching() throws Exception {
String field = "content";
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, field, new MockAnalyzer(random));
qp.setAllowLeadingWildcard(true);
String docs[] = {
"\\ abcdefg1",
"\\79 hijklmn1",
"\\\\ opqrstu1",
};
// queries that should find all docs
String matchAll[] = {
"*", "*1", "**1", "*?", "*?1", "?*1", "**", "***", "\\\\*"
};
// queries that should find no docs
String matchNone[] = {
"a*h", "a?h", "*a*h", "?a", "a?",
};
// queries that should be parsed to prefix queries
String matchOneDocPrefix[][] = {
{"a*", "ab*", "abc*", }, // these should find only doc 0
{"h*", "hi*", "hij*", "\\\\7*"}, // these should find only doc 1
{"o*", "op*", "opq*", "\\\\\\\\*"}, // these should find only doc 2
};
// queries that should be parsed to wildcard queries
String matchOneDocWild[][] = {
{"*a*", "*ab*", "*abc**", "ab*e*", "*g?", "*f?1", "abc**"}, // these should find only doc 0
{"*h*", "*hi*", "*hij**", "hi*k*", "*n?", "*m?1", "hij**"}, // these should find only doc 1
{"*o*", "*op*", "*opq**", "op*q*", "*u?", "*t?1", "opq**"}, // these should find only doc 2
};
// prepare the index
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random, dir,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setMergePolicy(newLogMergePolicy()));
for (int i = 0; i < docs.length; i++) {
Document doc = new Document();
doc.add(newField(field,docs[i],Store.NO,Index.ANALYZED));
iw.addDocument(doc);
}
iw.close();
IndexSearcher searcher = new IndexSearcher(dir, true);
// test queries that must find all
for (int i = 0; i < matchAll.length; i++) {
String qtxt = matchAll[i];
Query q = qp.parse(qtxt);
if (VERBOSE) System.out.println("matchAll: qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
assertEquals(docs.length,hits.length);
}
// test queries that must find none
for (int i = 0; i < matchNone.length; i++) {
String qtxt = matchNone[i];
Query q = qp.parse(qtxt);
if (VERBOSE) System.out.println("matchNone: qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
assertEquals(0,hits.length);
}
// test queries that must be prefix queries and must find only one doc
for (int i = 0; i < matchOneDocPrefix.length; i++) {
for (int j = 0; j < matchOneDocPrefix[i].length; j++) {
String qtxt = matchOneDocPrefix[i][j];
Query q = qp.parse(qtxt);
if (VERBOSE) System.out.println("match 1 prefix: doc="+docs[i]+" qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
assertEquals(PrefixQuery.class, q.getClass());
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
assertEquals(1,hits.length);
assertEquals(i,hits[0].doc);
}
}
// test queries that must be wildcard queries and must find only one doc
for (int i = 0; i < matchOneDocPrefix.length; i++) {
for (int j = 0; j < matchOneDocWild[i].length; j++) {
String qtxt = matchOneDocWild[i][j];
Query q = qp.parse(qtxt);
if (VERBOSE) System.out.println("match 1 wild: doc="+docs[i]+" qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
assertEquals(WildcardQuery.class, q.getClass());
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
assertEquals(1,hits.length);
assertEquals(i,hits[0].doc);