/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using Analyzer = Lucene.Net.Analysis.Analyzer;
using BooleanClause = Lucene.Net.Search.BooleanClause;
using BooleanQuery = Lucene.Net.Search.BooleanQuery;
using MultiPhraseQuery = Lucene.Net.Search.MultiPhraseQuery;
using PhraseQuery = Lucene.Net.Search.PhraseQuery;
using Query = Lucene.Net.Search.Query;
using Version = Lucene.Net.Util.Version;
namespace Lucene.Net.QueryParsers
{
/// A QueryParser which constructs queries to search multiple fields.
///
///
/// $Revision: 829134 $
///
public class MultiFieldQueryParser:QueryParser
{
protected internal System.String[] fields;
protected internal System.Collections.IDictionary 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.
///
///
///
/// Please use
/// {@link #MultiFieldQueryParser(Version, String[], Analyzer, Map)}
/// instead
///
[Obsolete("Please use MultiFieldQueryParser(Version, String[], Analyzer, IDictionary) instead")]
public MultiFieldQueryParser(System.String[] fields, Analyzer analyzer, System.Collections.IDictionary boosts):this(Version.LUCENE_24, fields, analyzer)
{
this.boosts = 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.
///
///
public MultiFieldQueryParser(Version matchVersion, System.String[] fields, Analyzer analyzer, System.Collections.IDictionary boosts):this(matchVersion, fields, analyzer)
{
this.boosts = boosts;
}
/// 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.
///
///
///
/// Please use
/// {@link #MultiFieldQueryParser(Version, String[], Analyzer)}
/// instead
///
[Obsolete("Please use MultiFieldQueryParser(Version, String[], Analyzer) instead")]
public MultiFieldQueryParser(System.String[] fields, Analyzer analyzer):this(Version.LUCENE_24, fields, 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.
///
///
public MultiFieldQueryParser(Version matchVersion, System.String[] fields, Analyzer analyzer):base(matchVersion, null, analyzer)
{
this.fields = fields;
}
protected internal override Query GetFieldQuery(System.String field, System.String queryText, int slop)
{
if (field == null)
{
System.Collections.IList clauses = new System.Collections.ArrayList();
for (int i = 0; i < fields.Length; i++)
{
Query q = base.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
if (boosts.Contains(fields[i]))
{
System.Single boost = (System.Single) boosts[fields[i]];
q.SetBoost((float) boost);
}
}
ApplySlop(q, slop);
clauses.Add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
}
}
if (clauses.Count == 0)
// happens for stopwords
return null;
return GetBooleanQuery(clauses, true);
}
Query q2 = base.GetFieldQuery(field, queryText);
ApplySlop(q2, slop);
return q2;
}
private void ApplySlop(Query q, int slop)
{
if (q is PhraseQuery)
{
((PhraseQuery) q).SetSlop(slop);
}
else if (q is MultiPhraseQuery)
{
((MultiPhraseQuery) q).SetSlop(slop);
}
}
public /*protected internal*/ override Query GetFieldQuery(System.String field, System.String queryText)
{
return GetFieldQuery(field, queryText, 0);
}
public /*protected internal*/ override Query GetFuzzyQuery(System.String field, System.String termStr, float minSimilarity)
{
if (field == null)
{
System.Collections.IList clauses = new System.Collections.ArrayList();
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 base.GetFuzzyQuery(field, termStr, minSimilarity);
}
public /*protected internal*/ override Query GetPrefixQuery(System.String field, System.String termStr)
{
if (field == null)
{
System.Collections.IList clauses = new System.Collections.ArrayList();
for (int i = 0; i < fields.Length; i++)
{
clauses.Add(new BooleanClause(GetPrefixQuery(fields[i], termStr), BooleanClause.Occur.SHOULD));
}
return GetBooleanQuery(clauses, true);
}
return base.GetPrefixQuery(field, termStr);
}
public /*protected internal*/ override Query GetWildcardQuery(System.String field, System.String termStr)
{
if (field == null)
{
System.Collections.IList clauses = new System.Collections.ArrayList();
for (int i = 0; i < fields.Length; i++)
{
clauses.Add(new BooleanClause(GetWildcardQuery(fields[i], termStr), BooleanClause.Occur.SHOULD));
}
return GetBooleanQuery(clauses, true);
}
return base.GetWildcardQuery(field, termStr);
}
protected internal override Query GetRangeQuery(System.String field, System.String part1, System.String part2, bool inclusive)
{
if (field == null)
{
System.Collections.IList clauses = new System.Collections.ArrayList();
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 base.GetRangeQuery(field, part1, part2, inclusive);
}
/// Parses a query which searches on the fields specified.
///
/// If x fields are specified, this effectively constructs:
///
///
/// <code>
/// (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
/// </code>
///
///
///
/// Queries strings to parse
///
/// Fields to search on
///
/// Analyzer to use
///
/// ParseException
/// if query parsing fails
///
/// IllegalArgumentException
/// if the length of the queries array differs from the length of
/// the fields array
///
/// Use {@link #Parse(Version,String[],String[],Analyzer)}
/// instead
///
[Obsolete("Use Parse(Version,String[],String[],Analyzer) instead")]
public static Query Parse(System.String[] queries, System.String[] fields, Analyzer analyzer)
{
return Parse(Version.LUCENE_24, queries, fields, analyzer);
}
/// Parses a query which searches on the fields specified.
///
/// If x fields are specified, this effectively constructs:
///
///
/// <code>
/// (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
/// </code>
///
///
///
/// Lucene version to match; this is passed through to
/// QueryParser.
///
/// Queries strings to parse
///
/// Fields to search on
///
/// Analyzer to use
///
/// ParseException
/// if query parsing fails
///
/// IllegalArgumentException
/// if the length of the queries array differs from the length of
/// the fields array
///
public static Query Parse(Version matchVersion, System.String[] queries, System.String[] fields, Analyzer analyzer)
{
if (queries.Length != fields.Length)
throw new System.ArgumentException("queries.length != fields.length");
BooleanQuery bQuery = new BooleanQuery();
for (int i = 0; i < fields.Length; i++)
{
QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
Query q = qp.Parse(queries[i]);
if (q != null && (!(q is BooleanQuery) || ((BooleanQuery) q).GetClauses().Length > 0))
{
bQuery.Add(q, BooleanClause.Occur.SHOULD);
}
}
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.
///
/// 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)
///
///
///
///
/// Query string to parse
///
/// Fields to search on
///
/// Flags describing the fields
///
/// Analyzer to use
///
/// ParseException if query parsing fails
/// IllegalArgumentException if the length of the fields array differs
/// from the length of the flags array
///
/// Use
/// {@link #Parse(Version, String, String[], BooleanClause.Occur[], Analyzer)}
/// instead
///
[Obsolete("Use Parse(Version, String, String[], BooleanClause.Occur[], Analyzer) instead")]
public static Query Parse(System.String query, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
{
return Parse(Version.LUCENE_24, query, fields, flags, 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:
/// <code>
/// 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>
///
///
/// The code above would construct a query:
///
///
/// <code>
/// (filename:query) +(contents:query) -(description:query)
/// </code>
///
///
///
/// Lucene version to match; this is passed through to
/// QueryParser.
///
/// Query string to parse
///
/// Fields to search on
///
/// Flags describing the fields
///
/// Analyzer to use
///
/// ParseException
/// if query parsing fails
///
/// IllegalArgumentException
/// if the length of the fields array differs from the length of
/// the flags array
///
public static Query Parse(Version matchVersion, System.String query, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
{
if (fields.Length != flags.Length)
throw new System.ArgumentException("fields.length != flags.length");
BooleanQuery bQuery = new BooleanQuery();
for (int i = 0; i < fields.Length; i++)
{
QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
Query q = qp.Parse(query);
if (q != null && (!(q is BooleanQuery) || ((BooleanQuery) q).GetClauses().Length > 0))
{
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.
///
/// 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)
///
///
///
///
/// Queries string to parse
///
/// Fields to search on
///
/// Flags describing the fields
///
/// Analyzer to use
///
/// ParseException if query parsing fails
/// IllegalArgumentException if the length of the queries, fields,
/// and flags array differ
///
/// Used
/// {@link #Parse(Version, String[], String[], BooleanClause.Occur[], Analyzer)}
/// instead
///
[Obsolete("Use Parse(Version, String[], String[], BooleanClause.Occur[], Analyzer) instead")]
public static Query Parse(System.String[] queries, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
{
return Parse(Version.LUCENE_24, queries, fields, flags, 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:
/// <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>
///
///
/// The code above would construct a query:
///
///
/// <code>
/// (filename:query1) +(contents:query2) -(description:query3)
/// </code>
///
///
///
/// Lucene version to match; this is passed through to
/// QueryParser.
///
/// Queries string to parse
///
/// Fields to search on
///
/// Flags describing the fields
///
/// Analyzer to use
///
/// ParseException
/// if query parsing fails
///
/// IllegalArgumentException
/// if the length of the queries, fields, and flags array differ
///
public static Query Parse(Version matchVersion, System.String[] queries, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
{
if (!(queries.Length == fields.Length && queries.Length == flags.Length))
throw new System.ArgumentException("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(matchVersion, fields[i], analyzer);
Query q = qp.Parse(queries[i]);
if (q != null && (!(q is BooleanQuery) || ((BooleanQuery) q).GetClauses().Length > 0))
{
bQuery.Add(q, flags[i]);
}
}
return bQuery;
}
}
}