mirror of https://github.com/apache/lucene.git
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:
parent
021e328676
commit
9f805c7d1d
|
@ -168,7 +168,9 @@ public class QueryParser {
|
|||
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,
|
||||
// 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.
|
||||
TokenStream source = analyzer.tokenStream(field, new StringReader(queryText));
|
||||
TokenStream source = analyzer.tokenStream(field,
|
||||
new StringReader(queryText));
|
||||
Term[] terms = new Term[2];
|
||||
org.apache.lucene.analysis.Token t;
|
||||
|
||||
|
@ -233,7 +239,7 @@ public class QueryParser {
|
|||
|
||||
public static void main(String[] args) throws Exception {
|
||||
QueryParser qp = new QueryParser("field",
|
||||
new org.apache.lucene.analysis.SimpleAnalyzer());
|
||||
new org.apache.lucene.analysis.SimpleAnalyzer());
|
||||
Query q = qp.parse(args[0]);
|
||||
System.out.println(q.toString("field"));
|
||||
}
|
||||
|
@ -277,8 +283,8 @@ PARSER_END(QueryParser)
|
|||
| <PREFIXTERM: <_TERM_START_CHAR> (<_TERM_CHAR>)* "*" >
|
||||
| <WILDTERM: <_TERM_START_CHAR>
|
||||
(<_TERM_CHAR> | ( [ "*", "?" ] ))* >
|
||||
| <RANGEIN: "[" (~["]"])+ "]">
|
||||
| <RANGEEX: "{" (~["}"])+ "}">
|
||||
| <RANGEIN: "[" ( ~[ "]" ] )+ "]">
|
||||
| <RANGEEX: "{" ( ~[ "}" ] )+ "}">
|
||||
}
|
||||
|
||||
<DEFAULT> SKIP : {
|
||||
|
@ -382,12 +388,14 @@ Query Term(String field) : {
|
|||
q = getFieldQuery(field, analyzer, term.image);
|
||||
}
|
||||
| ( term=<RANGEIN> { rangein=true; } | term=<RANGEEX> )
|
||||
[ <CARAT> boost=<NUMBER> ]
|
||||
{
|
||||
q = getRangeQuery(field, analyzer,
|
||||
term.image.substring(1, term.image.length()-1),
|
||||
rangein);
|
||||
}
|
||||
| term=<QUOTED>
|
||||
[ <CARAT> boost=<NUMBER> ]
|
||||
{
|
||||
q = getFieldQuery(field, analyzer,
|
||||
term.image.substring(1, term.image.length()-1));
|
||||
|
@ -407,6 +415,8 @@ Query Term(String field) : {
|
|||
((PhraseQuery) q).setBoost(f);
|
||||
else if (q instanceof MultiTermQuery)
|
||||
((MultiTermQuery) q).setBoost(f);
|
||||
else if (q instanceof RangeQuery)
|
||||
((RangeQuery) q).setBoost(f);
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
|
|
@ -158,6 +158,7 @@ public class TestQueryParser extends TestCase {
|
|||
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("(foo OR bar) AND (baz OR boo)", null,
|
||||
"+(foo bar) +(baz boo)");
|
||||
|
@ -180,5 +181,16 @@ public class TestQueryParser extends TestCase {
|
|||
"+term -\"phrase1 phrase2\" term");
|
||||
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})");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue