mirror of https://github.com/apache/lucene.git
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:
parent
10fb3c64a5
commit
fb0fb2693b
|
@ -162,8 +162,11 @@ public class MultiFieldQueryParser extends QueryParser
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
public static final int NORMAL_FIELD = 0;
|
public static final int NORMAL_FIELD = 0;
|
||||||
|
/** @deprecated */
|
||||||
public static final int REQUIRED_FIELD = 1;
|
public static final int REQUIRED_FIELD = 1;
|
||||||
|
/** @deprecated */
|
||||||
public static final int PROHIBITED_FIELD = 2;
|
public static final int PROHIBITED_FIELD = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -283,6 +286,7 @@ public class MultiFieldQueryParser extends QueryParser
|
||||||
* @throws TokenMgrError if query parsing fails
|
* @throws TokenMgrError if query parsing fails
|
||||||
* @throws IllegalArgumentException if the length of the fields array differs
|
* @throws IllegalArgumentException if the length of the fields array differs
|
||||||
* from the length of the flags array
|
* 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,
|
public static Query parse(String query, String[] fields, int[] flags,
|
||||||
Analyzer analyzer) throws ParseException
|
Analyzer analyzer) throws ParseException
|
||||||
|
@ -319,16 +323,61 @@ public class MultiFieldQueryParser extends QueryParser
|
||||||
* Usage:
|
* Usage:
|
||||||
* <code>
|
* <code>
|
||||||
* String[] fields = {"filename", "contents", "description"};
|
* String[] fields = {"filename", "contents", "description"};
|
||||||
* int[] flags = {MultiFieldQueryParser.NORMAL_FIELD,
|
* BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
|
||||||
* MultiFieldQueryParser.REQUIRED_FIELD,
|
* BooleanClause.Occur.MUST,
|
||||||
* MultiFieldQueryParser.PROHIBITED_FIELD,};
|
* BooleanClause.Occur.MUST_NOT};
|
||||||
* parse(query, fields, flags, analyzer);
|
* MultiFieldQueryParser.parse("query", fields, flags, analyzer);
|
||||||
* </code>
|
* </code>
|
||||||
* </pre>
|
* </pre>
|
||||||
*<p>
|
*<p>
|
||||||
* The code above would construct a query:
|
* The code above would construct a query:
|
||||||
* <pre>
|
* <pre>
|
||||||
* <code>
|
* <code>
|
||||||
|
* (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 = { "filename", "contents", "description" };
|
||||||
|
* 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)
|
* (filename:query1) +(contents:query2) -(description:query3)
|
||||||
* </code>
|
* </code>
|
||||||
* </pre>
|
* </pre>
|
||||||
|
@ -339,8 +388,8 @@ public class MultiFieldQueryParser extends QueryParser
|
||||||
* @param analyzer Analyzer to use
|
* @param analyzer Analyzer to use
|
||||||
* @throws ParseException if query parsing fails
|
* @throws ParseException if query parsing fails
|
||||||
* @throws TokenMgrError if query parsing fails
|
* @throws TokenMgrError if query parsing fails
|
||||||
* @throws IllegalArgumentException if the length of the queries, fields,
|
* @throws IllegalArgumentException if the length of the queries, fields, and flags array differ
|
||||||
* and flags array differ
|
* @deprecated use {@link #parse(String[], String[], BooleanClause.Occur[], Analyzer)} instead
|
||||||
*/
|
*/
|
||||||
public static Query parse(String[] queries, String[] fields, int[] flags,
|
public static Query parse(String[] queries, String[] fields, int[] flags,
|
||||||
Analyzer analyzer) throws ParseException
|
Analyzer analyzer) throws ParseException
|
||||||
|
@ -369,4 +418,51 @@ public class MultiFieldQueryParser extends QueryParser
|
||||||
return bQuery;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.lucene.search.BooleanClause;
|
||||||
import org.apache.lucene.search.Hits;
|
import org.apache.lucene.search.Hits;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
|
@ -141,6 +142,24 @@ public class TestMultiFieldQueryParser extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testStaticMethod2() throws ParseException {
|
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"};
|
String[] fields = {"b", "t"};
|
||||||
int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
|
int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
|
||||||
Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());
|
Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());
|
||||||
|
@ -159,6 +178,23 @@ public class TestMultiFieldQueryParser extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testStaticMethod3() throws ParseException {
|
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[] queries = {"one", "two"};
|
||||||
String[] fields = {"b", "t"};
|
String[] fields = {"b", "t"};
|
||||||
int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
|
int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
|
||||||
|
|
Loading…
Reference in New Issue