Enable boosting on range queries, phrpase queries (I think this used to work, but got broken when range queries were added); add tests cases for range queries

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@149639 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brian Goetz 2002-01-14 03:02:39 +00:00
parent 021e328676
commit 9f805c7d1d
2 changed files with 28 additions and 6 deletions

View File

@ -168,7 +168,9 @@ public class QueryParser {
clauses.addElement(new BooleanClause(q, required, prohibited)); clauses.addElement(new BooleanClause(q, required, prohibited));
} }
private Query getFieldQuery(String field, Analyzer analyzer, String queryText) { private Query getFieldQuery(String field,
Analyzer analyzer,
String queryText) {
// Use the analyzer to get all the tokens, and then build a TermQuery, // Use the analyzer to get all the tokens, and then build a TermQuery,
// PhraseQuery, or nothing based on the term count // PhraseQuery, or nothing based on the term count
@ -202,10 +204,14 @@ public class QueryParser {
} }
} }
private Query getRangeQuery(String field, Analyzer analyzer, String queryText, boolean inclusive) private Query getRangeQuery(String field,
Analyzer analyzer,
String queryText,
boolean inclusive)
{ {
// Use the analyzer to get all the tokens. There should be 1 or 2. // Use the analyzer to get all the tokens. There should be 1 or 2.
TokenStream source = analyzer.tokenStream(field, new StringReader(queryText)); TokenStream source = analyzer.tokenStream(field,
new StringReader(queryText));
Term[] terms = new Term[2]; Term[] terms = new Term[2];
org.apache.lucene.analysis.Token t; org.apache.lucene.analysis.Token t;
@ -277,8 +283,8 @@ PARSER_END(QueryParser)
| <PREFIXTERM: <_TERM_START_CHAR> (<_TERM_CHAR>)* "*" > | <PREFIXTERM: <_TERM_START_CHAR> (<_TERM_CHAR>)* "*" >
| <WILDTERM: <_TERM_START_CHAR> | <WILDTERM: <_TERM_START_CHAR>
(<_TERM_CHAR> | ( [ "*", "?" ] ))* > (<_TERM_CHAR> | ( [ "*", "?" ] ))* >
| <RANGEIN: "[" (~["]"])+ "]"> | <RANGEIN: "[" ( ~[ "]" ] )+ "]">
| <RANGEEX: "{" (~["}"])+ "}"> | <RANGEEX: "{" ( ~[ "}" ] )+ "}">
} }
<DEFAULT> SKIP : { <DEFAULT> SKIP : {
@ -382,12 +388,14 @@ Query Term(String field) : {
q = getFieldQuery(field, analyzer, term.image); q = getFieldQuery(field, analyzer, term.image);
} }
| ( term=<RANGEIN> { rangein=true; } | term=<RANGEEX> ) | ( term=<RANGEIN> { rangein=true; } | term=<RANGEEX> )
[ <CARAT> boost=<NUMBER> ]
{ {
q = getRangeQuery(field, analyzer, q = getRangeQuery(field, analyzer,
term.image.substring(1, term.image.length()-1), term.image.substring(1, term.image.length()-1),
rangein); rangein);
} }
| term=<QUOTED> | term=<QUOTED>
[ <CARAT> boost=<NUMBER> ]
{ {
q = getFieldQuery(field, analyzer, q = getFieldQuery(field, analyzer,
term.image.substring(1, term.image.length()-1)); term.image.substring(1, term.image.length()-1));
@ -407,6 +415,8 @@ Query Term(String field) : {
((PhraseQuery) q).setBoost(f); ((PhraseQuery) q).setBoost(f);
else if (q instanceof MultiTermQuery) else if (q instanceof MultiTermQuery)
((MultiTermQuery) q).setBoost(f); ((MultiTermQuery) q).setBoost(f);
else if (q instanceof RangeQuery)
((RangeQuery) q).setBoost(f);
} }
return q; return q;
} }

View File

@ -158,6 +158,7 @@ public class TestQueryParser extends TestCase {
assertQueryEquals("germ term^2.0", null, "germ term^2.0"); assertQueryEquals("germ term^2.0", null, "germ term^2.0");
assertQueryEquals("term^2.0", null, "term^2.0"); assertQueryEquals("term^2.0", null, "term^2.0");
assertQueryEquals("term^2", null, "term^2.0"); assertQueryEquals("term^2", null, "term^2.0");
assertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");
assertQueryEquals("(foo OR bar) AND (baz OR boo)", null, assertQueryEquals("(foo OR bar) AND (baz OR boo)", null,
"+(foo bar) +(baz boo)"); "+(foo bar) +(baz boo)");
@ -180,5 +181,16 @@ public class TestQueryParser extends TestCase {
"+term -\"phrase1 phrase2\" term"); "+term -\"phrase1 phrase2\" term");
assertQueryEquals("stop", qpAnalyzer, ""); assertQueryEquals("stop", qpAnalyzer, "");
} }
public void testRange() throws Exception {
assertQueryEquals("[ a z]", null, "[a-z]");
assertQueryEquals("[ a z ]", null, "[a-z]");
assertQueryEquals("{ a z}", null, "{a-z}");
assertQueryEquals("{ a z }", null, "{a-z}");
assertQueryEquals("{ a z }^2.0", null, "{a-z}^2.0");
assertQueryEquals("[ a z] OR bar", null, "[a-z] bar");
assertQueryEquals("[ a z] AND bar", null, "+[a-z] +bar");
assertQueryEquals("( bar blar { a z}) ", null, "(bar blar {a-z})");
}
} }