diff --git a/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java b/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java index 4fb597ff155..395e7bfdd19 100644 --- a/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java +++ b/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java @@ -21,6 +21,8 @@ import java.util.Vector; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.MultiPhraseQuery; +import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; /** @@ -59,19 +61,31 @@ public class MultiFieldQueryParser extends QueryParser this.fields = fields; } - protected Query getFieldQuery(String field, String queryText) throws ParseException { + protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException { if (field == null) { Vector clauses = new Vector(); for (int i = 0; i < fields.length; i++) { Query q = super.getFieldQuery(fields[i], queryText); - if (q != null) + if (q != null) { + if (q instanceof PhraseQuery) { + ((PhraseQuery) q).setSlop(slop); + } + if (q instanceof MultiPhraseQuery) { + ((MultiPhraseQuery) q).setSlop(slop); + } clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD)); + } } return getBooleanQuery(clauses, true); } return super.getFieldQuery(field, queryText); } + + protected Query getFieldQuery(String field, String queryText) throws ParseException { + return getFieldQuery(field, queryText, 0); + } + /** * @deprecated use {@link #getFieldQuery(String, String)} */ @@ -113,6 +127,18 @@ public class MultiFieldQueryParser extends QueryParser return super.getPrefixQuery(field, termStr); } + protected Query getWildcardQuery(String field, String termStr) throws ParseException { + if (field == null) { + Vector clauses = new Vector(); + for (int i = 0; i < fields.length; i++) { + clauses.add(new BooleanClause(super.getWildcardQuery(fields[i], termStr), + BooleanClause.Occur.SHOULD)); + } + return getBooleanQuery(clauses, true); + } + return super.getWildcardQuery(field, termStr); + } + /** @throws ParseException * @deprecated use {@link #getRangeQuery(String, String, String, boolean)} */ diff --git a/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java b/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java index e242dea712f..6ab65b4a357 100644 --- a/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java +++ b/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java @@ -63,12 +63,18 @@ public class TestMultiFieldQueryParser extends TestCase { q = mfqp.parse("[a TO c] two"); assertEquals("(b:[a TO c] t:[a TO c]) (b:two t:two)", q.toString()); + q = mfqp.parse("w?ldcard"); + assertEquals("b:w?ldcard t:w?ldcard", q.toString()); + q = mfqp.parse("\"foo bar\""); assertEquals("b:\"foo bar\" t:\"foo bar\"", q.toString()); q = mfqp.parse("\"aa bb cc\" \"dd ee\""); assertEquals("(b:\"aa bb cc\" t:\"aa bb cc\") (b:\"dd ee\" t:\"dd ee\")", q.toString()); + q = mfqp.parse("\"foo bar\"~4"); + assertEquals("b:\"foo bar\"~4 t:\"foo bar\"~4", q.toString()); + // make sure that terms which have a field are not touched: q = mfqp.parse("one f:two"); assertEquals("(b:one t:one) f:two", q.toString());