You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by eh...@apache.org on 2003/10/03 05:12:42 UTC
cvs commit: jakarta-lucene/src/java/org/apache/lucene/queryParser QueryParser.java QueryParser.jj
ehatcher 2003/10/02 20:12:42
Modified: src/java/org/apache/lucene/queryParser QueryParser.java
QueryParser.jj
Log:
Add locale option to QueryParser, allowing date range to be dealt with a bit more flexibly.
Revision Changes Path
1.3 +32 -8 jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.java
Index: QueryParser.java
===================================================================
RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- QueryParser.java 30 Sep 2003 04:38:36 -0000 1.2
+++ QueryParser.java 3 Oct 2003 03:12:42 -0000 1.3
@@ -73,9 +73,12 @@
Analyzer analyzer;
String field;
int phraseSlop = 0;
+ Locale locale;
/** Parses a query string, returning a {@link org.apache.lucene.search.Query}.
+ * Default locale is used for date range parsing.
+ * Use {@link #parse(String, String, Analyzer, Locale)} for non-default locale handling.
* @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.
@@ -92,14 +95,42 @@
}
}
+ /** 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.
+ * @param locale locale to use for date range parsing
+ * @throws ParseException if the parsing fails
+ */
+ static public Query parse(String query, String field, Analyzer analyzer, Locale locale)
+ throws ParseException {
+ try {
+ QueryParser parser = new QueryParser(field, analyzer, locale);
+ return parser.parse(query);
+ }
+ catch (TokenMgrError tme) {
+ throw new ParseException(tme.getMessage());
+ }
+ }
+
/** Constructs a query parser.
* @param f the default field for query terms.
* @param a used to find terms in the query text.
*/
public QueryParser(String f, Analyzer a) {
+ this(f, a, Locale.getDefault());
+ }
+
+ /** Constructs a query parser.
+ * @param f the default field for query terms.
+ * @param a used to find terms in the query text.
+ * @param locale
+ */
+ public QueryParser(String f, Analyzer a, Locale locale) {
this(new FastCharStream(new StringReader("")));
analyzer = a;
field = f;
+ this.locale = locale;
}
/** Parses a query string, returning a
@@ -243,22 +274,15 @@
String part2,
boolean inclusive) throws ParseException
{
- boolean isDate = false;
-
try {
- DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
+ DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
df.setLenient(true);
Date d1 = df.parse(part1);
Date d2 = df.parse(part2);
part1 = DateField.dateToString(d1);
part2 = DateField.dateToString(d2);
- isDate = true;
}
catch (Exception e) { }
-
- if (!isDate) {
- // @@@ Add number support
- }
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
1.31 +32 -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.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- QueryParser.jj 30 Sep 2003 04:38:36 -0000 1.30
+++ QueryParser.jj 3 Oct 2003 03:12:42 -0000 1.31
@@ -135,9 +135,12 @@
Analyzer analyzer;
String field;
int phraseSlop = 0;
+ Locale locale;
/** Parses a query string, returning a {@link org.apache.lucene.search.Query}.
+ * Default locale is used for date range parsing.
+ * Use {@link #parse(String, String, Analyzer, Locale)} for non-default locale handling.
* @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.
@@ -154,14 +157,42 @@
}
}
+ /** 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.
+ * @param locale locale to use for date range parsing
+ * @throws ParseException if the parsing fails
+ */
+ static public Query parse(String query, String field, Analyzer analyzer, Locale locale)
+ throws ParseException {
+ try {
+ QueryParser parser = new QueryParser(field, analyzer, locale);
+ return parser.parse(query);
+ }
+ catch (TokenMgrError tme) {
+ throw new ParseException(tme.getMessage());
+ }
+ }
+
/** Constructs a query parser.
* @param f the default field for query terms.
* @param a used to find terms in the query text.
*/
public QueryParser(String f, Analyzer a) {
+ this(f, a, Locale.getDefault());
+ }
+
+ /** Constructs a query parser.
+ * @param f the default field for query terms.
+ * @param a used to find terms in the query text.
+ * @param locale
+ */
+ public QueryParser(String f, Analyzer a, Locale locale) {
this(new FastCharStream(new StringReader("")));
analyzer = a;
field = f;
+ this.locale = locale;
}
/** Parses a query string, returning a
@@ -305,22 +336,15 @@
String part2,
boolean inclusive) throws ParseException
{
- boolean isDate = false;
-
try {
- DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
+ DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
df.setLenient(true);
Date d1 = df.parse(part1);
Date d2 = df.parse(part2);
part1 = DateField.dateToString(d1);
part2 = DateField.dateToString(d2);
- isDate = true;
}
catch (Exception e) { }
-
- if (!isDate) {
- // @@@ Add number support
- }
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org