Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
MultiFieldQueryParser.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using Lucene.Net.Search;
21 using Analyzer = Lucene.Net.Analysis.Analyzer;
22 using BooleanClause = Lucene.Net.Search.BooleanClause;
23 using BooleanQuery = Lucene.Net.Search.BooleanQuery;
24 using MultiPhraseQuery = Lucene.Net.Search.MultiPhraseQuery;
25 using PhraseQuery = Lucene.Net.Search.PhraseQuery;
26 using Query = Lucene.Net.Search.Query;
27 using Version = Lucene.Net.Util.Version;
28 
29 namespace Lucene.Net.QueryParsers
30 {
31 
32  /// <summary> A QueryParser which constructs queries to search multiple fields.
33  ///
34  /// </summary>
35  /// <version> $Revision: 829231 $
36  /// </version>
38  {
39  protected internal string[] fields;
40  protected internal IDictionary<string, float> boosts;
41 
42  /// <summary> Creates a MultiFieldQueryParser. Allows passing of a map with term to
43  /// Boost, and the boost to apply to each term.
44  ///
45  /// <p/>
46  /// It will, when parse(String query) is called, construct a query like this
47  /// (assuming the query consists of two terms and you specify the two fields
48  /// <c>title</c> and <c>body</c>):
49  /// <p/>
50  ///
51  /// <code>
52  /// (title:term1 body:term1) (title:term2 body:term2)
53  /// </code>
54  ///
55  /// <p/>
56  /// When setDefaultOperator(AND_OPERATOR) is set, the result will be:
57  /// <p/>
58  ///
59  /// <code>
60  /// +(title:term1 body:term1) +(title:term2 body:term2)
61  /// </code>
62  ///
63  /// <p/>
64  /// When you pass a boost (title=>5 body=>10) you can get
65  /// <p/>
66  ///
67  /// <code>
68  /// +(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0)
69  /// </code>
70  ///
71  /// <p/>
72  /// In other words, all the query's terms must appear, but it doesn't matter
73  /// in what fields they appear.
74  /// <p/>
75  /// </summary>
76  public MultiFieldQueryParser(Version matchVersion, string[] fields, Analyzer analyzer, IDictionary<string, float> boosts)
77  : this(matchVersion, fields, analyzer)
78  {
79  this.boosts = boosts;
80  }
81 
82  /// <summary> Creates a MultiFieldQueryParser.
83  ///
84  /// <p/>
85  /// It will, when parse(String query) is called, construct a query like this
86  /// (assuming the query consists of two terms and you specify the two fields
87  /// <c>title</c> and <c>body</c>):
88  /// <p/>
89  ///
90  /// <code>
91  /// (title:term1 body:term1) (title:term2 body:term2)
92  /// </code>
93  ///
94  /// <p/>
95  /// When setDefaultOperator(AND_OPERATOR) is set, the result will be:
96  /// <p/>
97  ///
98  /// <code>
99  /// +(title:term1 body:term1) +(title:term2 body:term2)
100  /// </code>
101  ///
102  /// <p/>
103  /// In other words, all the query's terms must appear, but it doesn't matter
104  /// in what fields they appear.
105  /// <p/>
106  /// </summary>
107  public MultiFieldQueryParser(Version matchVersion, System.String[] fields, Analyzer analyzer)
108  : base(matchVersion, null, analyzer)
109  {
110  this.fields = fields;
111  }
112 
113  protected internal override Query GetFieldQuery(string field, string queryText, int slop)
114  {
115  if (field == null)
116  {
117  IList<BooleanClause> clauses = new List<BooleanClause>();
118  for (int i = 0; i < fields.Length; i++)
119  {
120  Query q = base.GetFieldQuery(fields[i], queryText);
121  if (q != null)
122  {
123  //If the user passes a map of boosts
124  if (boosts != null)
125  {
126  //Get the boost from the map and apply them
127  Single boost = boosts[fields[i]];
128  q.Boost = boost;
129  }
130  ApplySlop(q, slop);
131  clauses.Add(new BooleanClause(q, Occur.SHOULD));
132  }
133  }
134  if (clauses.Count == 0)
135  // happens for stopwords
136  return null;
137  return GetBooleanQuery(clauses, true);
138  }
139  Query q2 = base.GetFieldQuery(field, queryText);
140  ApplySlop(q2, slop);
141  return q2;
142  }
143 
144  private void ApplySlop(Query q, int slop)
145  {
146  if (q is PhraseQuery)
147  {
148  ((PhraseQuery)q).Slop = slop;
149  }
150  else if (q is MultiPhraseQuery)
151  {
152  ((MultiPhraseQuery)q).Slop = slop;
153  }
154  }
155 
156 
157  protected internal override Query GetFieldQuery(System.String field, System.String queryText)
158  {
159  return GetFieldQuery(field, queryText, 0);
160  }
161 
162 
163  protected internal override Query GetFuzzyQuery(System.String field, System.String termStr, float minSimilarity)
164  {
165  if (field == null)
166  {
167  IList<BooleanClause> clauses = new List<BooleanClause>();
168  for (int i = 0; i < fields.Length; i++)
169  {
170  clauses.Add(new BooleanClause(GetFuzzyQuery(fields[i], termStr, minSimilarity), Occur.SHOULD));
171  }
172  return GetBooleanQuery(clauses, true);
173  }
174  return base.GetFuzzyQuery(field, termStr, minSimilarity);
175  }
176 
177  protected internal override Query GetPrefixQuery(System.String field, System.String termStr)
178  {
179  if (field == null)
180  {
181  IList<BooleanClause> clauses = new List<BooleanClause>();
182  for (int i = 0; i < fields.Length; i++)
183  {
184  clauses.Add(new BooleanClause(GetPrefixQuery(fields[i], termStr), Occur.SHOULD));
185  }
186  return GetBooleanQuery(clauses, true);
187  }
188  return base.GetPrefixQuery(field, termStr);
189  }
190 
191  protected internal override Query GetWildcardQuery(System.String field, System.String termStr)
192  {
193  if (field == null)
194  {
195  IList<BooleanClause> clauses = new List<BooleanClause>();
196  for (int i = 0; i < fields.Length; i++)
197  {
198  clauses.Add(new BooleanClause(GetWildcardQuery(fields[i], termStr), Occur.SHOULD));
199  }
200  return GetBooleanQuery(clauses, true);
201  }
202  return base.GetWildcardQuery(field, termStr);
203  }
204 
205 
206  protected internal override Query GetRangeQuery(System.String field, System.String part1, System.String part2, bool inclusive)
207  {
208  if (field == null)
209  {
210  IList<BooleanClause> clauses = new List<BooleanClause>();
211  for (int i = 0; i < fields.Length; i++)
212  {
213  clauses.Add(new BooleanClause(GetRangeQuery(fields[i], part1, part2, inclusive), Occur.SHOULD));
214  }
215  return GetBooleanQuery(clauses, true);
216  }
217  return base.GetRangeQuery(field, part1, part2, inclusive);
218  }
219 
220  /// <summary> Parses a query which searches on the fields specified.
221  /// <p/>
222  /// If x fields are specified, this effectively constructs:
223  ///
224  /// <code>
225  /// (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
226  /// </code>
227  ///
228  /// </summary>
229  /// <param name="matchVersion">Lucene version to match; this is passed through to
230  /// QueryParser.
231  /// </param>
232  /// <param name="queries">Queries strings to parse
233  /// </param>
234  /// <param name="fields">Fields to search on
235  /// </param>
236  /// <param name="analyzer">Analyzer to use
237  /// </param>
238  /// <throws> ParseException </throws>
239  /// <summary> if query parsing fails
240  /// </summary>
241  /// <throws> IllegalArgumentException </throws>
242  /// <summary> if the length of the queries array differs from the length of
243  /// the fields array
244  /// </summary>
245  public static Query Parse(Version matchVersion, System.String[] queries, System.String[] fields, Analyzer analyzer)
246  {
247  if (queries.Length != fields.Length)
248  throw new System.ArgumentException("queries.length != fields.length");
249  BooleanQuery bQuery = new BooleanQuery();
250  for (int i = 0; i < fields.Length; i++)
251  {
252  QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
253  Query q = qp.Parse(queries[i]);
254  if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0))
255  {
256  bQuery.Add(q, Occur.SHOULD);
257  }
258  }
259  return bQuery;
260  }
261 
262  /// <summary> Parses a query, searching on the fields specified. Use this if you need
263  /// to specify certain fields as required, and others as prohibited.
264  /// <p/>
265  /// Uasge:
266  /// <code>
267  /// String[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
268  /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
269  /// BooleanClause.Occur.MUST,
270  /// BooleanClause.Occur.MUST_NOT};
271  /// MultiFieldQueryParser.parse(&quot;query&quot;, fields, flags, analyzer);
272  /// </code>
273  /// <p/>
274  /// The code above would construct a query:
275  ///
276  /// <code>
277  /// (filename:query) +(contents:query) -(description:query)
278  /// </code>
279  ///
280  /// </summary>
281  /// <param name="matchVersion">Lucene version to match; this is passed through to
282  /// QueryParser.
283  /// </param>
284  /// <param name="query">Query string to parse
285  /// </param>
286  /// <param name="fields">Fields to search on
287  /// </param>
288  /// <param name="flags">Flags describing the fields
289  /// </param>
290  /// <param name="analyzer">Analyzer to use
291  /// </param>
292  /// <throws> ParseException </throws>
293  /// <summary> if query parsing fails
294  /// </summary>
295  /// <throws> IllegalArgumentException </throws>
296  /// <summary> if the length of the fields array differs from the length of
297  /// the flags array
298  /// </summary>
299  public static Query Parse(Version matchVersion, System.String query, System.String[] fields, Occur[] flags, Analyzer analyzer)
300  {
301  if (fields.Length != flags.Length)
302  throw new System.ArgumentException("fields.length != flags.length");
303  BooleanQuery bQuery = new BooleanQuery();
304  for (int i = 0; i < fields.Length; i++)
305  {
306  QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
307  Query q = qp.Parse(query);
308  if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0))
309  {
310  bQuery.Add(q, flags[i]);
311  }
312  }
313  return bQuery;
314  }
315 
316  /// <summary> Parses a query, searching on the fields specified. Use this if you need
317  /// to specify certain fields as required, and others as prohibited.
318  /// <p/>
319  /// Usage:
320  /// <code>
321  /// String[] query = {&quot;query1&quot;, &quot;query2&quot;, &quot;query3&quot;};
322  /// String[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
323  /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
324  /// BooleanClause.Occur.MUST,
325  /// BooleanClause.Occur.MUST_NOT};
326  /// MultiFieldQueryParser.parse(query, fields, flags, analyzer);
327  /// </code>
328  /// <p/>
329  /// The code above would construct a query:
330  ///
331  /// <code>
332  /// (filename:query1) +(contents:query2) -(description:query3)
333  /// </code>
334  ///
335  /// </summary>
336  /// <param name="matchVersion">Lucene version to match; this is passed through to
337  /// QueryParser.
338  /// </param>
339  /// <param name="queries">Queries string to parse
340  /// </param>
341  /// <param name="fields">Fields to search on
342  /// </param>
343  /// <param name="flags">Flags describing the fields
344  /// </param>
345  /// <param name="analyzer">Analyzer to use
346  /// </param>
347  /// <throws> ParseException </throws>
348  /// <summary> if query parsing fails
349  /// </summary>
350  /// <throws> IllegalArgumentException </throws>
351  /// <summary> if the length of the queries, fields, and flags array differ
352  /// </summary>
353  public static Query Parse(Version matchVersion, System.String[] queries, System.String[] fields, Occur[] flags, Analyzer analyzer)
354  {
355  if (!(queries.Length == fields.Length && queries.Length == flags.Length))
356  throw new System.ArgumentException("queries, fields, and flags array have have different length");
357  BooleanQuery bQuery = new BooleanQuery();
358  for (int i = 0; i < fields.Length; i++)
359  {
360  QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
361  Query q = qp.Parse(queries[i]);
362  if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0))
363  {
364  bQuery.Add(q, flags[i]);
365  }
366  }
367  return bQuery;
368  }
369  }
370 }