19 using System.Collections;
20 using Lucene.Net.Index;
21 using Lucene.Net.Support;
24 using Occur = Lucene.Net.Search.Occur;
26 namespace Lucene.Net.Search
34 public class BooleanQuery :
Query, System.Collections.Generic.IEnumerable<BooleanClause>, System.ICloneable
41 this.enclosingInstance = enclosingInstance;
48 return enclosingInstance;
52 internal AnonymousClassSimilarityDelegator(
BooleanQuery enclosingInstance, Lucene.Net.Search.Similarity Param1):base(Param1)
54 InitBlock(enclosingInstance);
56 public override float Coord(
int overlap,
int maxOverlap)
62 private static int _maxClauses = 1024;
72 public override System.String Message
76 return "maxClauseCount is set to " + Lucene.Net.Search.BooleanQuery._maxClauses;
86 public static int MaxClauseCount
88 get {
return _maxClauses; }
92 throw new ArgumentException(
"maxClauseCount must be >= 1");
97 private EquatableList<BooleanClause> clauses =
new EquatableList<BooleanClause>();
98 private bool disableCoord;
117 this.disableCoord = disableCoord;
125 public virtual bool IsCoordDisabled()
134 Similarity result = base.GetSimilarity(searcher);
138 result =
new AnonymousClassSimilarityDelegator(
this, result);
143 protected internal int minNrShouldMatch = 0;
159 public virtual int MinimumNumberShouldMatch
161 set { this.minNrShouldMatch = value; }
162 get {
return minNrShouldMatch; }
182 if (clauses.Count >= _maxClauses)
191 return clauses.ToArray();
195 public virtual System.Collections.Generic.List<
BooleanClause> Clauses
197 get {
return clauses; }
204 public System.Collections.Generic.IEnumerator<
BooleanClause> GetEnumerator()
206 return clauses.GetEnumerator();
215 protected internal class BooleanWeight:
Weight
219 this.enclosingInstance = enclosingInstance;
226 return enclosingInstance;
232 protected internal System.Collections.Generic.List<
Weight> weights;
236 InitBlock(enclosingInstance);
237 this.similarity = Enclosing_Instance.GetSimilarity(searcher);
238 weights =
new System.Collections.Generic.List<
Weight>(Enclosing_Instance.clauses.Count);
239 for (
int i = 0; i < Enclosing_Instance.clauses.Count; i++)
241 weights.Add(Enclosing_Instance.clauses[i].Query.CreateWeight(searcher));
247 get {
return Enclosing_Instance; }
250 public override float Value
252 get {
return Enclosing_Instance.Boost; }
255 public override float GetSumOfSquaredWeights()
258 for (
int i = 0; i < weights.Count; i++)
261 float s = weights[i].GetSumOfSquaredWeights();
262 if (!Enclosing_Instance.clauses[i].IsProhibited)
267 sum *= Enclosing_Instance.Boost*Enclosing_Instance.Boost;
273 public override void Normalize(
float norm)
275 norm *= Enclosing_Instance.Boost;
276 foreach (
Weight w
in weights)
285 int minShouldMatch = Enclosing_Instance.MinimumNumberShouldMatch;
287 sumExpl.Description =
"sum of:";
292 int shouldMatchCount = 0;
293 System.Collections.Generic.IEnumerator<
BooleanClause> cIter = Enclosing_Instance.clauses.GetEnumerator();
294 for (System.Collections.Generic.IEnumerator<
Weight> wIter = weights.GetEnumerator(); wIter.MoveNext(); )
299 if (w.Scorer(reader,
true,
true) == null)
310 sumExpl.AddDetail(e);
318 sumExpl.AddDetail(r);
321 if (c.Occur ==
Occur.SHOULD)
324 else if (c.IsRequired)
328 sumExpl.AddDetail(r);
334 System.Boolean tempAux =
false;
335 sumExpl.Match = tempAux;
336 sumExpl.Value = 0.0f;
337 sumExpl.Description =
"Failure to meet condition(s) of required/prohibited clause(s)";
340 else if (shouldMatchCount < minShouldMatch)
342 System.Boolean tempAux2 =
false;
343 sumExpl.Match = tempAux2;
344 sumExpl.Value = 0.0f;
345 sumExpl.Description =
"Failure to match minimum number " +
"of optional clauses: " + minShouldMatch;
349 sumExpl.Match = 0 < coord?
true:
false;
352 float coordFactor = similarity.Coord(coord, maxCoord);
353 if (coordFactor == 1.0f)
360 result.AddDetail(sumExpl);
361 result.AddDetail(
new Explanation(coordFactor,
"coord(" + coord +
"/" + maxCoord +
")"));
368 var required =
new System.Collections.Generic.List<
Scorer>();
369 var prohibited =
new System.Collections.Generic.List<
Scorer>();
370 var optional =
new System.Collections.Generic.List<
Scorer>();
372 System.Collections.Generic.IEnumerator<
BooleanClause> cIter = Enclosing_Instance.clauses.GetEnumerator();
373 foreach (
Weight w
in weights)
377 Scorer subScorer = w.Scorer(reader,
true,
false);
378 if (subScorer == null)
385 else if (c.IsRequired)
387 required.Add(subScorer);
389 else if (c.IsProhibited)
391 prohibited.Add(subScorer);
395 optional.Add(subScorer);
400 if (!scoreDocsInOrder && topScorer && required.Count == 0 && prohibited.Count < 32)
402 return new BooleanScorer(similarity, Enclosing_Instance.minNrShouldMatch, optional, prohibited);
405 if (required.Count == 0 && optional.Count == 0)
410 else if (optional.Count < Enclosing_Instance.minNrShouldMatch)
419 return new BooleanScorer2(similarity, Enclosing_Instance.minNrShouldMatch, required, prohibited, optional);
422 public override bool GetScoresDocsOutOfOrder()
424 int numProhibited = 0;
431 else if (c.IsProhibited)
437 if (numProhibited > 32)
450 return new BooleanWeight(
this, searcher);
455 if (minNrShouldMatch == 0 && clauses.Count == 1)
468 if (query == c.
Query)
479 for (
int i = 0; i < clauses.Count; i++)
483 if (query != c.
Query)
500 public override void ExtractTerms(System.Collections.Generic.ISet<
Term> terms)
508 public override System.Object Clone()
511 clone.clauses = (EquatableList<BooleanClause>) this.clauses.Clone();
516 public override System.String ToString(System.String field)
518 System.Text.StringBuilder buffer =
new System.Text.StringBuilder();
519 bool needParens = (Boost != 1.0) || (MinimumNumberShouldMatch > 0);
525 for (
int i = 0; i < clauses.Count; i++)
534 if (subQuery != null)
540 buffer.Append(subQuery.
ToString(field));
545 buffer.Append(subQuery.
ToString(field));
550 buffer.Append(
"null");
553 if (i != clauses.Count - 1)
562 if (MinimumNumberShouldMatch > 0)
565 buffer.Append(MinimumNumberShouldMatch);
577 public override bool Equals(System.Object o)
582 return (this.Boost == other.
Boost)
583 && this.clauses.Equals(other.clauses)
585 && this.disableCoord == other.disableCoord;
589 public override int GetHashCode()
591 return BitConverter.ToInt32(BitConverter.GetBytes(Boost), 0) ^ clauses.GetHashCode() + MinimumNumberShouldMatch + (disableCoord ? 17 : 0);
594 IEnumerator IEnumerable.GetEnumerator()
596 return GetEnumerator();