LUCENE-543: WildcardQuery without wildcarded term rewrites to a TermQuery

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@477424 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2006-11-21 00:09:50 +00:00
parent cd39647656
commit c44be0c446
3 changed files with 32 additions and 1 deletions

View File

@ -30,6 +30,11 @@ Changes in runtime behavior
for range queries. Added useOldRangeQuery property to QueryParser to allow for range queries. Added useOldRangeQuery property to QueryParser to allow
selection of old RangeQuery class if required. selection of old RangeQuery class if required.
6. LUCENE-543: WildcardQuery now performs a TermQuery if the provided term
does not contain a wildcard character (? or *), when previously a
StringIndexOutOfBoundsException was thrown.
(Michael Busch via Erik Hatcher)
New features New features
1. LUCENE-503: New ThaiAnalyzer and ThaiWordFilter in contrib/analyzers 1. LUCENE-503: New ThaiAnalyzer and ThaiWordFilter in contrib/analyzers
@ -58,7 +63,7 @@ New features
7. LUCENE-573: QueryParser now allows backslash escaping in 7. LUCENE-573: QueryParser now allows backslash escaping in
quoted terms and phrases. (Michael Busch via Yonik Seeley) quoted terms and phrases. (Michael Busch via Yonik Seeley)
7. LUCENE-716: QueryParser now allows specification of unicode 8. LUCENE-716: QueryParser now allows specification of unicode
characters in terms via a unicode escape of the form \uXXXX characters in terms via a unicode escape of the form \uXXXX
(Michael Busch via Yonik Seeley) (Michael Busch via Yonik Seeley)

View File

@ -31,8 +31,11 @@ import java.io.IOException;
* @see WildcardTermEnum * @see WildcardTermEnum
*/ */
public class WildcardQuery extends MultiTermQuery { public class WildcardQuery extends MultiTermQuery {
private boolean termContainsWildcard;
public WildcardQuery(Term term) { public WildcardQuery(Term term) {
super(term); super(term);
this.termContainsWildcard = (term.text().indexOf('*') != -1) || (term.text().indexOf('?') != -1);
} }
protected FilteredTermEnum getEnum(IndexReader reader) throws IOException { protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
@ -45,4 +48,12 @@ public class WildcardQuery extends MultiTermQuery {
return false; return false;
} }
public Query rewrite(IndexReader reader) throws IOException {
if (this.termContainsWildcard) {
return super.rewrite(reader);
}
return new TermQuery(getTerm());
}
} }

View File

@ -55,6 +55,21 @@ public class TestWildcard
assertFalse(fq.equals(wq1)); assertFalse(fq.equals(wq1));
} }
/**
* Tests if a WildcardQuery that has no wildcard in the term is rewritten to a single
* TermQuery.
*/
public void testTermWithoutWildcard() throws IOException {
RAMDirectory indexStore = getIndexStore("field", new String[]{"nowildcard", "nowildcardx"});
IndexSearcher searcher = new IndexSearcher(indexStore);
Query wq = new WildcardQuery(new Term("field", "nowildcard"));
assertMatches(searcher, wq, 1);
wq = searcher.rewrite(wq);
assertTrue(wq instanceof TermQuery);
}
/** /**
* Tests Wildcard queries with an asterisk. * Tests Wildcard queries with an asterisk.
*/ */