Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Added support for RangeQuery. The syntax is expressed as follows: inclusive range = field:[lowerTerm-upperTerm] exclusive range = field:{lowerTerm-upperTerm}

Scott Ganyo891209bfd38c
diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.jj b/src/java/org/apache/lucene/queryParser/QueryParser.jj
index 6eab8f0b9ad..cd62032ad51 100644
--- a/src/java/org/apache/lucene/queryParser/QueryParser.jj
+++ b/src/java/org/apache/lucene/queryParser/QueryParser.jj
@@ -59,13 +59,13 @@ options {
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 @@ public class QueryParser {
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 @@ public class QueryParser {
}
}
+ 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 @@ PARSER_END(QueryParser)
| <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 @@ Query Term(String field) : {
boolean prefix = false;
boolean wildcard = false;
boolean fuzzy = false;
+ boolean rangein = false;
Query q;
}
{
@@ -340,6 +372,11 @@ Query Term(String field) : {
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)); }