MultiFieldQueryParserpublic class MultiFieldQueryParser extends QueryParser A QueryParser which constructs queries to search multiple fields. |
Fields Summary |
---|
protected String[] | fields | protected Map | boosts |
Constructors Summary |
---|
public MultiFieldQueryParser(String[] fields, Analyzer analyzer, Map boosts)Creates a MultiFieldQueryParser.
Allows passing of a map with term to Boost, and the boost to apply to each term.
It will, when parse(String query)
is called, construct a query like this (assuming the query consists of
two terms and you specify the two fields title and body ):
(title:term1 body:term1) (title:term2 body:term2)
When setDefaultOperator(AND_OPERATOR) is set, the result will be:
+(title:term1 body:term1) +(title:term2 body:term2)
When you pass a boost (title=>5 body=>10) you can get
+(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0)
In other words, all the query's terms must appear, but it doesn't matter in
what fields they appear.
this(fields,analyzer);
this.boosts = boosts;
| public MultiFieldQueryParser(String[] fields, Analyzer analyzer)Creates a MultiFieldQueryParser.
It will, when parse(String query)
is called, construct a query like this (assuming the query consists of
two terms and you specify the two fields title and body ):
(title:term1 body:term1) (title:term2 body:term2)
When setDefaultOperator(AND_OPERATOR) is set, the result will be:
+(title:term1 body:term1) +(title:term2 body:term2)
In other words, all the query's terms must appear, but it doesn't matter in
what fields they appear.
super(null, analyzer);
this.fields = fields;
|
Methods Summary |
---|
protected org.apache.lucene.search.Query | getFieldQuery(java.lang.String field, java.lang.String queryText, int slop)
if (field == null) {
Vector clauses = new Vector();
for (int i = 0; i < fields.length; i++) {
Query q = getFieldQuery(fields[i], queryText);
if (q != null) {
//If the user passes a map of boosts
if (boosts != null) {
//Get the boost from the map and apply them
Float boost = (Float)boosts.get(fields[i]);
if (boost != null) {
q.setBoost(boost.floatValue());
}
}
if (q instanceof PhraseQuery) {
((PhraseQuery) q).setSlop(slop);
}
if (q instanceof MultiPhraseQuery) {
((MultiPhraseQuery) q).setSlop(slop);
}
clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
}
}
if (clauses.size() == 0) // happens for stopwords
return null;
return getBooleanQuery(clauses, true);
}
return super.getFieldQuery(field, queryText);
| protected org.apache.lucene.search.Query | getFieldQuery(java.lang.String field, java.lang.String queryText)
return getFieldQuery(field, queryText, 0);
| protected org.apache.lucene.search.Query | getFuzzyQuery(java.lang.String field, java.lang.String termStr, float minSimilarity)
if (field == null) {
Vector clauses = new Vector();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getFuzzyQuery(fields[i], termStr, minSimilarity),
BooleanClause.Occur.SHOULD));
}
return getBooleanQuery(clauses, true);
}
return super.getFuzzyQuery(field, termStr, minSimilarity);
| protected org.apache.lucene.search.Query | getPrefixQuery(java.lang.String field, java.lang.String termStr)
if (field == null) {
Vector clauses = new Vector();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getPrefixQuery(fields[i], termStr),
BooleanClause.Occur.SHOULD));
}
return getBooleanQuery(clauses, true);
}
return super.getPrefixQuery(field, termStr);
| protected org.apache.lucene.search.Query | getRangeQuery(java.lang.String field, java.lang.String part1, java.lang.String part2, boolean inclusive)
if (field == null) {
Vector clauses = new Vector();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getRangeQuery(fields[i], part1, part2, inclusive),
BooleanClause.Occur.SHOULD));
}
return getBooleanQuery(clauses, true);
}
return super.getRangeQuery(field, part1, part2, inclusive);
| protected org.apache.lucene.search.Query | getWildcardQuery(java.lang.String field, java.lang.String termStr)
if (field == null) {
Vector clauses = new Vector();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getWildcardQuery(fields[i], termStr),
BooleanClause.Occur.SHOULD));
}
return getBooleanQuery(clauses, true);
}
return super.getWildcardQuery(field, termStr);
| public static org.apache.lucene.search.Query | parse(java.lang.String query, java.lang.String[] fields, org.apache.lucene.search.BooleanClause$Occur[] flags, org.apache.lucene.analysis.Analyzer analyzer)Parses a query, searching on the fields specified.
Use this if you need to specify certain fields as required,
and others as prohibited.
Usage:
String[] fields = {"filename", "contents", "description"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST_NOT};
MultiFieldQueryParser.parse("query", fields, flags, analyzer);
The code above would construct a query:
(filename:query) +(contents:query) -(description:query)
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;
| public static org.apache.lucene.search.Query | parse(java.lang.String[] queries, java.lang.String[] fields, org.apache.lucene.search.BooleanClause$Occur[] flags, org.apache.lucene.analysis.Analyzer analyzer)Parses a query, searching on the fields specified.
Use this if you need to specify certain fields as required,
and others as prohibited.
Usage:
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);
The code above would construct a query:
(filename:query1) +(contents:query2) -(description:query3)
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;
| public static org.apache.lucene.search.Query | parse(java.lang.String[] queries, java.lang.String[] fields, org.apache.lucene.analysis.Analyzer analyzer)Parses a query which searches on the fields specified.
If x fields are specified, this effectively constructs:
(field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
if (queries.length != fields.length)
throw new IllegalArgumentException("queries.length != fields.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, BooleanClause.Occur.SHOULD);
}
return bQuery;
|
|