Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 
38  {
39  protected internal string[] fields;
40  protected internal IDictionary<string, float> boosts;
41 
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 
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 
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 
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 
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 }