From 9f805c7d1d7395f67e73a485006f48475fd200d2 Mon Sep 17 00:00:00 2001 From: Brian Goetz Date: Mon, 14 Jan 2002 03:02:39 +0000 Subject: [PATCH] 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 --- .../apache/lucene/queryParser/QueryParser.jj | 22 ++++++++++++++----- .../lucene/queryParser/TestQueryParser.java | 12 ++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.jj b/src/java/org/apache/lucene/queryParser/QueryParser.jj index 0c0fc45b06a..46572f982e8 100644 --- a/src/java/org/apache/lucene/queryParser/QueryParser.jj +++ b/src/java/org/apache/lucene/queryParser/QueryParser.jj @@ -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) | (<_TERM_CHAR>)* "*" > | (<_TERM_CHAR> | ( [ "*", "?" ] ))* > -| -| +| +| } SKIP : { @@ -382,12 +388,14 @@ Query Term(String field) : { q = getFieldQuery(field, analyzer, term.image); } | ( term= { rangein=true; } | term= ) + [ boost= ] { q = getRangeQuery(field, analyzer, term.image.substring(1, term.image.length()-1), rangein); } | term= + [ boost= ] { 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; } diff --git a/src/test/org/apache/lucene/queryParser/TestQueryParser.java b/src/test/org/apache/lucene/queryParser/TestQueryParser.java index dede0d00dd2..27aeff2d4ff 100644 --- a/src/test/org/apache/lucene/queryParser/TestQueryParser.java +++ b/src/test/org/apache/lucene/queryParser/TestQueryParser.java @@ -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})"); + } }