mirror of https://github.com/apache/lucene.git
Add escape character to query parser (backslash); add unit test for escape character; wrap TokenMgrError with ParseException in QueryParser.parse
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@149736 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fe527de308
commit
2a96b329a8
|
@ -198,6 +198,7 @@
|
||||||
<!-- ================================================================== -->
|
<!-- ================================================================== -->
|
||||||
<target name="demo" depends="compile" if="javacc.present">
|
<target name="demo" depends="compile" if="javacc.present">
|
||||||
<mkdir dir="${build.demo}"/>
|
<mkdir dir="${build.demo}"/>
|
||||||
|
<mkdir dir="${build.demo.src}" />
|
||||||
|
|
||||||
<copy todir="${build.demo.src}">
|
<copy todir="${build.demo.src}">
|
||||||
<fileset dir="${demo.src}">
|
<fileset dir="${demo.src}">
|
||||||
|
@ -211,7 +212,6 @@
|
||||||
javacchome="${javacc.zip.dir}"
|
javacchome="${javacc.zip.dir}"
|
||||||
outputdirectory="${build.demo.src}/org/apache/lucene/demo/html"
|
outputdirectory="${build.demo.src}/org/apache/lucene/demo/html"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<mkdir dir="${build.demo.classes}"/>
|
<mkdir dir="${build.demo.classes}"/>
|
||||||
|
|
||||||
<javac
|
<javac
|
||||||
|
|
|
@ -111,13 +111,17 @@ public class QueryParser {
|
||||||
* @param field the default field for query terms.
|
* @param field the default field for query terms.
|
||||||
* @param analyzer used to find terms in the query text.
|
* @param analyzer used to find terms in the query text.
|
||||||
* @throws ParseException if the parsing fails
|
* @throws ParseException if the parsing fails
|
||||||
* @throws TokenMgrError if the parsing fails
|
|
||||||
*/
|
*/
|
||||||
static public Query parse(String query, String field, Analyzer analyzer)
|
static public Query parse(String query, String field, Analyzer analyzer)
|
||||||
throws ParseException, TokenMgrError {
|
throws ParseException {
|
||||||
|
try {
|
||||||
QueryParser parser = new QueryParser(field, analyzer);
|
QueryParser parser = new QueryParser(field, analyzer);
|
||||||
return parser.parse(query);
|
return parser.parse(query);
|
||||||
}
|
}
|
||||||
|
catch (TokenMgrError tme) {
|
||||||
|
throw new ParseException(tme.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Analyzer analyzer;
|
Analyzer analyzer;
|
||||||
String field;
|
String field;
|
||||||
|
@ -269,9 +273,12 @@ PARSER_END(QueryParser)
|
||||||
|
|
||||||
<*> TOKEN : {
|
<*> TOKEN : {
|
||||||
<#_NUM_CHAR: ["0"-"9"] >
|
<#_NUM_CHAR: ["0"-"9"] >
|
||||||
| <#_TERM_START_CHAR: ~[ " ", "\t", "+", "-", "!", "(", ")", ":", "^",
|
| <#_ESCAPED_CHAR: "\\" [ "\\", "+", "-", "!", "(", ")", ":", "^",
|
||||||
"[", "]", "\"", "{", "}", "~", "*" ] >
|
"[", "]", "\"", "{", "}", "~", "*" ] >
|
||||||
| <#_TERM_CHAR: <_TERM_START_CHAR> >
|
| <#_TERM_START_CHAR: ( ~[ " ", "\t", "+", "-", "!", "(", ")", ":", "^",
|
||||||
|
"[", "]", "\"", "{", "}", "~", "*" ]
|
||||||
|
| <_ESCAPED_CHAR> ) >
|
||||||
|
| <#_TERM_CHAR: ( <_TERM_START_CHAR> | <_ESCAPED_CHAR> ) >
|
||||||
| <#_WHITESPACE: ( " " | "\t" ) >
|
| <#_WHITESPACE: ( " " | "\t" ) >
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -246,4 +246,13 @@ public class TestQueryParser extends TestCase {
|
||||||
assertQueryEquals("( bar blar { a z}) ", null, "bar blar {a-z}");
|
assertQueryEquals("( bar blar { a z}) ", null, "bar blar {a-z}");
|
||||||
assertQueryEquals("gack ( bar blar { a z}) ", null, "gack (bar blar {a-z})");
|
assertQueryEquals("gack ( bar blar { a z}) ", null, "gack (bar blar {a-z})");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testEscaped() throws Exception {
|
||||||
|
Analyzer a = new WhitespaceAnalyzer();
|
||||||
|
assertQueryEquals("\\[brackets", a, "\\[brackets");
|
||||||
|
assertQueryEquals("\\[brackets", null, "brackets");
|
||||||
|
assertQueryEquals("\\\\", a, "\\\\");
|
||||||
|
assertQueryEquals("\\+blah", a, "\\+blah");
|
||||||
|
assertQueryEquals("\\(blah", a, "\\(blah");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue