fix parsing of sloppy phrase queries and wildcard queries

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@219965 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Naber 2005-07-20 19:11:43 +00:00
parent 025022251d
commit eaa580b853
2 changed files with 34 additions and 2 deletions

View File

@ -21,6 +21,8 @@ import java.util.Vector;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
/** /**
@ -59,19 +61,31 @@ public class MultiFieldQueryParser extends QueryParser
this.fields = fields; 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) { if (field == null) {
Vector clauses = new Vector(); Vector clauses = new Vector();
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
Query q = super.getFieldQuery(fields[i], queryText); 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)); clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
} }
}
return getBooleanQuery(clauses, true); return getBooleanQuery(clauses, true);
} }
return super.getFieldQuery(field, queryText); 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)} * @deprecated use {@link #getFieldQuery(String, String)}
*/ */
@ -113,6 +127,18 @@ public class MultiFieldQueryParser extends QueryParser
return super.getPrefixQuery(field, termStr); 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 /** @throws ParseException
* @deprecated use {@link #getRangeQuery(String, String, String, boolean)} * @deprecated use {@link #getRangeQuery(String, String, String, boolean)}
*/ */

View File

@ -63,12 +63,18 @@ public class TestMultiFieldQueryParser extends TestCase {
q = mfqp.parse("[a TO c] two"); q = mfqp.parse("[a TO c] two");
assertEquals("(b:[a TO c] t:[a TO c]) (b:two t:two)", q.toString()); 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\""); q = mfqp.parse("\"foo bar\"");
assertEquals("b:\"foo bar\" t:\"foo bar\"", q.toString()); assertEquals("b:\"foo bar\" t:\"foo bar\"", q.toString());
q = mfqp.parse("\"aa bb cc\" \"dd ee\""); 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()); 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: // make sure that terms which have a field are not touched:
q = mfqp.parse("one f:two"); q = mfqp.parse("one f:two");
assertEquals("(b:one t:one) f:two", q.toString()); assertEquals("(b:one t:one) f:two", q.toString());