Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

enhancement to QueryParser to allow slop factor passed to a new getFieldQuery method

Erik Hatcheree00e19246fa
diff --git a/CHANGES.txt b/CHANGES.txt
index 719e21ba9ab..4faccbd6b61 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -11,6 +11,13 @@ $Id$
2. Added new class FieldCache to manage in-memory caches of field term
values. (Tim Jones)
+ 3. Added overloaded getFieldQuery method to QueryParser which
+ accepts the slop factor specified for the phrase (or the default
+ phrase slop for the QueryParser instance). This allows overriding
+ methods to replace a PhraseQuery with a SpanNearQuery instead,
+ keeping the proper slop factor. (Erik Hatcher)
+
+
1.4 RC3
1. Fixed several search bugs introduced by the skipTo() changes in
@@ -265,7 +272,7 @@ $Id$
7. Modified QueryParser to make it possible to programmatically specify the
default Boolean operator (OR or AND).
- (P�ter Hal�csy via otis)
+ (P�ter Hal�csy via otis)
8. Made many search methods and classes non-final, per requests.
This includes IndexWriter and IndexSearcher, among others.
diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.java b/src/java/org/apache/lucene/queryParser/QueryParser.java
index c66d20986c6..e1eefffb013 100644
--- a/src/java/org/apache/lucene/queryParser/QueryParser.java
+++ b/src/java/org/apache/lucene/queryParser/QueryParser.java
@@ -261,6 +261,26 @@ public class QueryParser implements QueryParserConstants {
}
}
+ /**
+ * Base implementation delegates to {@link #getFieldQuery(String,Analyzer,String)}.
+ * This method may be overridden, for example, to return
+ * a SpanNearQuery instead of a PhraseQuery.
+ *
+ * @exception ParseException throw in overridden method to disallow
+ */
+ protected Query getFieldQuery(String field,
+ Analyzer analyzer,
+ String queryText,
+ int slop) throws ParseException {
+ Query query = getFieldQuery(field, analyzer, queryText);
+
+ if (query instanceof PhraseQuery) {
+ ((PhraseQuery) query).setSlop(slop);
+ }
+
+ return query;
+ }
+
/**
* @exception ParseException throw in overridden method to disallow
*/
@@ -770,15 +790,17 @@ public class QueryParser implements QueryParserConstants {
jj_la1[20] = jj_gen;
;
}
- q = getFieldQuery(field, analyzer,
- term.image.substring(1, term.image.length()-1));
- if (slop != null && q instanceof PhraseQuery) {
+ int s = phraseSlop;
+
+ if (slop != null) {
try {
- int s = Float.valueOf(slop.image.substring(1)).intValue();
- ((PhraseQuery) q).setSlop(s);
+ s = Float.valueOf(slop.image.substring(1)).intValue();
}
catch (Exception ignored) { }
}
+ q = getFieldQuery(field, analyzer,
+ term.image.substring(1, term.image.length()-1),
+ s);
break;
default:
jj_la1[21] = jj_gen;
diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.jj b/src/java/org/apache/lucene/queryParser/QueryParser.jj
index 585565f6114..43fbf981fd2 100644
--- a/src/java/org/apache/lucene/queryParser/QueryParser.jj
+++ b/src/java/org/apache/lucene/queryParser/QueryParser.jj
@@ -284,6 +284,26 @@ public class QueryParser {
}
}
+ /**
+ * Base implementation delegates to {@link #getFieldQuery(String,Analyzer,String)}.
+ * This method may be overridden, for example, to return
+ * a SpanNearQuery instead of a PhraseQuery.
+ *
+ * @exception ParseException throw in overridden method to disallow
+ */
+ protected Query getFieldQuery(String field,
+ Analyzer analyzer,
+ String queryText,
+ int slop) throws ParseException {
+ Query query = getFieldQuery(field, analyzer, queryText);
+
+ if (query instanceof PhraseQuery) {
+ ((PhraseQuery) query).setSlop(slop);
+ }
+
+ return query;
+ }
+
/**
* @exception ParseException throw in overridden method to disallow
*/
@@ -655,15 +675,17 @@ Query Term(String field) : {
[ slop=<SLOP> ]
[ <CARAT> boost=<NUMBER> ]
{
- q = getFieldQuery(field, analyzer,
- term.image.substring(1, term.image.length()-1));
- if (slop != null && q instanceof PhraseQuery) {
+ int s = phraseSlop;
+
+ if (slop != null) {
try {
- int s = Float.valueOf(slop.image.substring(1)).intValue();
- ((PhraseQuery) q).setSlop(s);
+ s = Float.valueOf(slop.image.substring(1)).intValue();
}
catch (Exception ignored) { }
}
+ q = getFieldQuery(field, analyzer,
+ term.image.substring(1, term.image.length()-1),
+ s);
}
)
{