deprecate methods that use int flags, adding alternative methods that

use enumeration type instead

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@295117 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Naber 2005-10-05 21:01:28 +00:00
parent 10fb3c64a5
commit fb0fb2693b
2 changed files with 141 additions and 9 deletions

View File

@ -162,8 +162,11 @@ public class MultiFieldQueryParser extends QueryParser
}
/** @deprecated */
public static final int NORMAL_FIELD = 0;
/** @deprecated */
public static final int REQUIRED_FIELD = 1;
/** @deprecated */
public static final int PROHIBITED_FIELD = 2;
/**
@ -283,6 +286,7 @@ public class MultiFieldQueryParser extends QueryParser
* @throws TokenMgrError if query parsing fails
* @throws IllegalArgumentException if the length of the fields array differs
* from the length of the flags array
* @deprecated use {@link #parse(String, String[], BooleanClause.Occur[], Analyzer)} instead
*/
public static Query parse(String query, String[] fields, int[] flags,
Analyzer analyzer) throws ParseException
@ -319,28 +323,73 @@ public class MultiFieldQueryParser extends QueryParser
* Usage:
* <code>
* String[] fields = {"filename", "contents", "description"};
* int[] flags = {MultiFieldQueryParser.NORMAL_FIELD,
* MultiFieldQueryParser.REQUIRED_FIELD,
* MultiFieldQueryParser.PROHIBITED_FIELD,};
* parse(query, fields, flags, analyzer);
* BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
* BooleanClause.Occur.MUST,
* BooleanClause.Occur.MUST_NOT};
* MultiFieldQueryParser.parse("query", fields, flags, analyzer);
* </code>
* </pre>
*<p>
* The code above would construct a query:
* <pre>
* <code>
* (filename:query1) +(contents:query2) -(description:query3)
* (filename:query) +(contents:query) -(description:query)
* </code>
* </pre>
*
* @param query Query string to parse
* @param fields Fields to search on
* @param flags Flags describing the fields
* @param analyzer Analyzer to use
* @throws ParseException if query parsing fails
* @throws TokenMgrError if query parsing fails
* @throws IllegalArgumentException if the length of the fields array differs
* from the length of the flags array
*/
public static Query parse(String query, String[] fields,
BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException {
if (fields.length != flags.length)
throw new IllegalArgumentException("fields.length != flags.length");
BooleanQuery bQuery = new BooleanQuery();
for (int i = 0; i < fields.length; i++) {
QueryParser qp = new QueryParser(fields[i], analyzer);
Query q = qp.parse(query);
bQuery.add(q, flags[i]);
}
return bQuery;
}
/**
* Parses a query, searching on the fields specified. Use this if you need to
* specify certain fields as required, and others as prohibited.
* <p>
* <pre>
* Usage:
* <code>
* String[] fields = { &quot;filename&quot;, &quot;contents&quot;, &quot;description&quot; };
* int[] flags = { MultiFieldQueryParser.NORMAL_FIELD,
* MultiFieldQueryParser.REQUIRED_FIELD,
* MultiFieldQueryParser.PROHIBITED_FIELD, };
* parse(query, fields, flags, analyzer);
* </code>
* </pre>
*
* <p>
* The code above would construct a query:
* <pre>
* <code>
* (filename:query1) +(contents:query2) -(description:query3)
* </code>
* </pre>
*
* @param queries Queries string to parse
* @param fields Fields to search on
* @param flags Flags describing the fields
* @param analyzer Analyzer to use
* @throws ParseException if query parsing fails
* @throws TokenMgrError if query parsing fails
* @throws IllegalArgumentException if the length of the queries, fields,
* and flags array differ
* @throws IllegalArgumentException if the length of the queries, fields, and flags array differ
* @deprecated use {@link #parse(String[], String[], BooleanClause.Occur[], Analyzer)} instead
*/
public static Query parse(String[] queries, String[] fields, int[] flags,
Analyzer analyzer) throws ParseException
@ -368,5 +417,52 @@ public class MultiFieldQueryParser extends QueryParser
}
return bQuery;
}
/**
* Parses a query, searching on the fields specified.
* Use this if you need to specify certain fields as required,
* and others as prohibited.
* <p><pre>
* Usage:
* <code>
* String[] query = {"query1", "query2", "query3"};
* String[] fields = {"filename", "contents", "description"};
* BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
* BooleanClause.Occur.MUST,
* BooleanClause.Occur.MUST_NOT};
* MultiFieldQueryParser.parse(query, fields, flags, analyzer);
* </code>
* </pre>
*<p>
* The code above would construct a query:
* <pre>
* <code>
* (filename:query1) +(contents:query2) -(description:query3)
* </code>
* </pre>
*
* @param queries Queries string to parse
* @param fields Fields to search on
* @param flags Flags describing the fields
* @param analyzer Analyzer to use
* @throws ParseException if query parsing fails
* @throws TokenMgrError if query parsing fails
* @throws IllegalArgumentException if the length of the queries, fields,
* and flags array differ
*/
public static Query parse(String[] queries, String[] fields, BooleanClause.Occur[] flags,
Analyzer analyzer) throws ParseException
{
if (!(queries.length == fields.length && queries.length == flags.length))
throw new IllegalArgumentException("queries, fields, and flags array have have different length");
BooleanQuery bQuery = new BooleanQuery();
for (int i = 0; i < fields.length; i++)
{
QueryParser qp = new QueryParser(fields[i], analyzer);
Query q = qp.parse(queries[i]);
bQuery.add(q, flags[i]);
}
return bQuery;
}
}

View File

@ -27,6 +27,7 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
@ -139,8 +140,26 @@ public class TestMultiFieldQueryParser extends TestCase {
// expected exception, array length differs
}
}
public void testStaticMethod2() throws ParseException {
String[] fields = {"b", "t"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};
Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());
assertEquals("+b:one -t:one", q.toString());
q = MultiFieldQueryParser.parse("one two", fields, flags, new StandardAnalyzer());
assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());
try {
BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
q = MultiFieldQueryParser.parse("blah", fields, flags2, new StandardAnalyzer());
fail();
} catch(IllegalArgumentException e) {
// expected exception, array length differs
}
}
public void testStaticMethod2Old() throws ParseException {
String[] fields = {"b", "t"};
int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());
@ -159,6 +178,23 @@ public class TestMultiFieldQueryParser extends TestCase {
}
public void testStaticMethod3() throws ParseException {
String[] queries = {"one", "two", "three"};
String[] fields = {"f1", "f2", "f3"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST_NOT, BooleanClause.Occur.SHOULD};
Query q = MultiFieldQueryParser.parse(queries, fields, flags, new StandardAnalyzer());
assertEquals("+f1:one -f2:two f3:three", q.toString());
try {
BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
q = MultiFieldQueryParser.parse(queries, fields, flags2, new StandardAnalyzer());
fail();
} catch(IllegalArgumentException e) {
// expected exception, array length differs
}
}
public void testStaticMethod3Old() throws ParseException {
String[] queries = {"one", "two"};
String[] fields = {"b", "t"};
int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};