📄 queryparser.java
字号:
/* Generated By:JavaCC: Do not edit this line. QueryParser.java */
package org.apache.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.*;
/** * This class is generated by JavaCC. The only method that clients should need * to call is <a href="#parse">parse()</a>. * * The syntax for query strings is as follows: * A Query is a series of clauses. * A clause may be prefixed by: * <ul> * <li> a plus (<code>+</code>) or a minus (<code>-</code>) sign, indicating * that the clause is required or prohibited respectively; or * <li> a term followed by a colon, indicating the field to be searched. * This enables one to construct queries which search multiple fields. * </ul> * * A clause may be either: * <ul> * <li> a term, indicating all the documents that contain this term; or * <li> a nested query, enclosed in parentheses. Note that this may be used * with a <code>+</code>/<code>-</code> prefix to require any of a set of * terms. * </ul> * * Thus, in BNF, the query grammar is: * <pre> * Query ::= ( Clause )* * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" ) * </pre> * * <p> * Examples of appropriately formatted queries can be found in the <a * href="http://jakarta.apache.org/lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java">test cases</a>. * </p> * * @author Brian Goetz */
public class QueryParser implements QueryParserConstants {
/** Parses a query string, returning a {@link org.apache.lucene.search.Query}. * @param query the query string to be parsed. * @param field the default field for query terms. * @param analyzer used to find terms in the query text. * @throws ParseException if the parsing fails */
static public Query parse(String query, String field, Analyzer analyzer)
throws ParseException {
try {
QueryParser parser = new QueryParser(field, analyzer);
return parser.parse(query);
}
catch (TokenMgrError tme) {
throw new ParseException(tme.getMessage());
}
}
Analyzer analyzer;
String field;
int phraseSlop = 0;
/** Constructs a query parser. * @param field the default field for query terms. * @param analyzer used to find terms in the query text. */
public QueryParser(String f, Analyzer a) {
this(new FastCharStream(new StringReader("")));
analyzer = a;
field = f;
}
/** Parses a query string, returning a * <a href="lucene.search.Query.html">Query</a>. * @param query the query string to be parsed. * @throws ParseException if the parsing fails * @throws TokenMgrError if ther parsing fails */
public Query parse(String query) throws ParseException, TokenMgrError {
ReInit(new FastCharStream(new StringReader(query)));
return Query(field);
}
/** Sets the default slop for phrases. If zero, then exact phrase matches are required. Zero by default. */
public void setPhraseSlop(int s) { phraseSlop = s; }
/** Gets the default slop for phrases. */
public int getPhraseSlop() { return phraseSlop; }
private void addClause(Vector clauses, int conj, int mods,
Query q) {
boolean required, prohibited;
// If this term is introduced by AND, make the preceding term required, // unless it's already prohibited if (conj == CONJ_AND) {
BooleanClause c = (BooleanClause) clauses.elementAt(clauses.size()-1);
if (!c.prohibited)
c.required = true;
}
// We might have been passed a null query; the term might have been // filtered away by the analyzer. if (q == null)
return;
// We set REQUIRED if we're introduced by AND or +; PROHIBITED if // introduced by NOT or -; make sure not to set both. prohibited = (mods == MOD_NOT);
required = (mods == MOD_REQ);
if (conj == CONJ_AND && !prohibited)
required = true;
clauses.addElement(new BooleanClause(q, required, prohibited));
}
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
TokenStream source = analyzer.tokenStream(field,
new StringReader(queryText));
Vector v = new Vector();
org.apache.lucene.analysis.Token t;
while (true) {
try {
t = source.next();
}
catch (IOException e) {
t = null;
}
if (t == null)
break;
v.addElement(t.termText());
}
if (v.size() == 0)
return null;
else if (v.size() == 1)
return new TermQuery(new Term(field, (String) v.elementAt(0)));
else {
PhraseQuery q = new PhraseQuery();
q.setSlop(phraseSlop);
for (int i=0; i<v.size(); i++) {
q.add(new Term(field, (String) v.elementAt(i)));
}
return q;
}
}
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];
org.apache.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());
Query q = qp.parse(args[0]);
System.out.println(q.toString("field"));
}
private static final int CONJ_NONE = 0;
private static final int CONJ_AND = 1;
private static final int CONJ_OR = 2;
private static final int MOD_NONE = 0;
private static final int MOD_NOT = 10;
private static final int MOD_REQ = 11;
// * Query ::= ( Clause )*// * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" ) final public int Conjunction() throws ParseException {
int ret = CONJ_NONE;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case AND:
case OR:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case AND:
jj_consume_token(AND);
ret = CONJ_AND;
break;
case OR:
jj_consume_token(OR);
ret = CONJ_OR;
break;
default:
jj_la1[0] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[1] = jj_gen;
;
}
{if (true) return ret;}
throw new Error("Missing return statement in function");
}
final public int Modifiers() throws ParseException {
int ret = MOD_NONE;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case NOT:
case PLUS:
case MINUS:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
jj_consume_token(PLUS);
ret = MOD_REQ;
break;
case MINUS:
jj_consume_token(MINUS);
ret = MOD_NOT;
break;
case NOT:
jj_consume_token(NOT);
ret = MOD_NOT;
break;
default:
jj_la1[2] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[3] = jj_gen;
;
}
{if (true) return ret;}
throw new Error("Missing return statement in function");
}
final public Query Query(String field) throws ParseException {
Vector clauses = new Vector();
Query q, firstQuery=null;
int conj, mods;
mods = Modifiers();
q = Clause(field);
addClause(clauses, CONJ_NONE, mods, q);
if (mods == MOD_NONE)
firstQuery=q;
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case AND:
case OR:
case NOT:
case PLUS:
case MINUS:
case LPAREN:
case QUOTED:
case TERM:
case PREFIXTERM:
case WILDTERM:
case RANGEIN:
case RANGEEX:
case NUMBER:
;
break;
default:
jj_la1[4] = jj_gen;
break label_1;
}
conj = Conjunction();
mods = Modifiers();
q = Clause(field);
addClause(clauses, conj, mods, q);
}
if (clauses.size() == 1 && firstQuery != null)
{if (true) return firstQuery;}
else {
BooleanQuery query = new BooleanQuery();
for (int i = 0; i < clauses.size(); i++)
query.add((BooleanClause)clauses.elementAt(i));
{if (true) return query;}
}
throw new Error("Missing return statement in function");
}
final public Query Clause(String field) throws ParseException {
Query q;
Token fieldToken=null;
if (jj_2_1(2)) {
fieldToken = jj_consume_token(TERM);
jj_consume_token(COLON);
field = fieldToken.image;
} else {
;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case QUOTED:
case TERM:
case PREFIXTERM:
case WILDTERM:
case RANGEIN:
case RANGEEX:
case NUMBER:
q = Term(field);
break;
case LPAREN:
jj_consume_token(LPAREN);
q = Query(field);
jj_consume_token(RPAREN);
break;
default:
jj_la1[5] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
{if (true) return q;}
throw new Error("Missing return statement in function");
}
final public Query Term(String field) throws ParseException {
Token term, boost=null, slop=null;
boolean prefix = false;
boolean wildcard = false;
boolean fuzzy = false;
boolean rangein = false;
Query q;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case TERM:
case PREFIXTERM:
case WILDTERM:
case NUMBER:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case TERM:
term = jj_consume_token(TERM);
break;
case PREFIXTERM:
term = jj_consume_token(PREFIXTERM);
prefix=true;
break;
case WILDTERM:
term = jj_consume_token(WILDTERM);
wildcard=true;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -