
scottganyo at apache
Sep 25, 2001, 2:54 PM
Post #1 of 2
(390 views)
Permalink
|
|
cvs commit: jakarta-lucene/src/java/org/apache/lucene/queryParser QueryParser.jj
|
|
scottganyo 01/09/25 14:54:18 Modified: src/java/org/apache/lucene/queryParser QueryParser.jj Log: Added support for RangeQuery. The syntax is expressed as follows: inclusive range = field:[lowerTerm-upperTerm] exclusive range = field:{lowerTerm-upperTerm} Revision Changes Path 1.2 +45 -8 jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj Index: QueryParser.jj =================================================================== RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- QueryParser.jj 2001/09/18 16:29:55 1.1 +++ QueryParser.jj 2001/09/25 21:54:18 1.2 @@ -59,13 +59,13 @@ PARSER_BEGIN(QueryParser) -package org.apache.lucene.queryParser; +package com.lucene.queryParser; import java.util.Vector; import java.io.*; -import org.apache.lucene.index.Term; -import org.apache.lucene.analysis.*; -import org.apache.lucene.search.*; +import com.lucene.index.Term; +import com.lucene.analysis.*; +import com.lucene.search.*; /** * This class is generated by JavaCC. The only method that clients should need @@ -170,7 +170,7 @@ TokenStream source = analyzer.tokenStream(field, new StringReader(queryText)); Vector v = new Vector(); - org.apache.lucene.analysis.Token t; + com.lucene.analysis.Token t; while (true) { try { @@ -197,9 +197,38 @@ } } + 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)); + Term[] terms = new Term[2]; + com.lucene.analysis.Token t; + + for (int i = 0; i < 2; i++) + { + try + { + t = source.next(); + } + catch (IOException e) + { + t = null; + } + if (t != null) + { + String text = t.termText(); + if (!text.equalsIgnoreCase("NULL")) + { + terms[i] = new Term(field, text); + } + } + } + return new RangeQuery(terms[0], terms[1], inclusive); + } + public static void main(String[] args) throws Exception { QueryParser qp = new QueryParser("field", - new org.apache.lucene.analysis.SimpleAnalyzer()); + new com.lucene.analysis.SimpleAnalyzer()); Query q = qp.parse(args[0]); System.out.println(q.toString("field")); } @@ -245,10 +274,12 @@ | <QUOTED: "\"" (~["\""])+ "\""> | <NUMBER: (<_NUM_CHAR>)+ "." (<_NUM_CHAR>)+ > | <TERM: <_IDENTIFIER_CHAR> - ( ~[."\"", " ", "\t", "(", ")", ":", "&", "|", "^", "*", "?", "~" ] )* > + ( ~[."\"", " ", "\t", "(", ")", ":", "&", "|", "^", "*", "?", "~", "{", "}", "[", "]" ] )* > | <FUZZY: "~" > | <WILDTERM: <_IDENTIFIER_CHAR> - ( ~[."\"", " ", "\t", "(", ")", ":", "&", "|", "^", "~" ] )* <_IDENTIFIER_CHAR>> + ( ~[."\"", " ", "\t", "(", ")", ":", "&", "|", "^", "~", "{", "}", "[", "]" ] )* <_IDENTIFIER_CHAR>> +| <RANGEIN: "[" (~["]"])+ "]"> +| <RANGEEX: "{" (~["}"])+ "}"> } <DEFAULT> SKIP : { @@ -327,6 +358,7 @@ boolean prefix = false; boolean wildcard = false; boolean fuzzy = false; + boolean rangein = false; Query q; } { @@ -340,6 +372,11 @@ q = new FuzzyQuery(new Term(field, term.image)); else q = getFieldQuery(field, analyzer, term.image); } + | (term=<RANGEIN>{rangein=true;}|term=<RANGEEX>) + { + q = getRangeQuery(field, analyzer, + term.image.substring(1, term.image.length()-1), rangein); + } | term=<QUOTED> { q = getFieldQuery(field, analyzer, term.image.substring(1, term.image.length()-1)); }
|