QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / ReqExclScorer.cs
blob5f99a66331f56749ecd6713b77cb431649f71ebb
1 /*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
19 namespace Lucene.Net.Search
23 /// <summary>A Scorer for queries with a required subscorer and an excluding (prohibited) subscorer.
24 /// <br>
25 /// This <code>Scorer</code> implements {@link Scorer#SkipTo(int)},
26 /// and it uses the skipTo() on the given scorers.
27 /// </summary>
28 public class ReqExclScorer : Scorer
30 private Scorer reqScorer, exclScorer;
32 /// <summary>Construct a <code>ReqExclScorer</code>.</summary>
33 /// <param name="reqScorer">The scorer that must match, except where
34 /// </param>
35 /// <param name="exclScorer">indicates exclusion.
36 /// </param>
37 public ReqExclScorer(Scorer reqScorer, Scorer exclScorer) : base(null)
38 { // No similarity used.
39 this.reqScorer = reqScorer;
40 this.exclScorer = exclScorer;
43 private bool firstTime = true;
45 public override bool Next()
47 if (firstTime)
49 if (!exclScorer.Next())
51 exclScorer = null; // exhausted at start
53 firstTime = false;
55 if (reqScorer == null)
57 return false;
59 if (!reqScorer.Next())
61 reqScorer = null; // exhausted, nothing left
62 return false;
64 if (exclScorer == null)
66 return true; // reqScorer.next() already returned true
68 return ToNonExcluded();
71 /// <summary>Advance to non excluded doc.
72 /// <br>On entry:
73 /// <ul>
74 /// <li>reqScorer != null,
75 /// <li>exclScorer != null,
76 /// <li>reqScorer was advanced once via next() or skipTo()
77 /// and reqScorer.doc() may still be excluded.
78 /// </ul>
79 /// Advances reqScorer a non excluded required doc, if any.
80 /// </summary>
81 /// <returns> true iff there is a non excluded required doc.
82 /// </returns>
83 private bool ToNonExcluded()
85 int exclDoc = exclScorer.Doc();
86 do
88 int reqDoc = reqScorer.Doc(); // may be excluded
89 if (reqDoc < exclDoc)
91 return true; // reqScorer advanced to before exclScorer, ie. not excluded
93 else if (reqDoc > exclDoc)
95 if (!exclScorer.SkipTo(reqDoc))
97 exclScorer = null; // exhausted, no more exclusions
98 return true;
100 exclDoc = exclScorer.Doc();
101 if (exclDoc > reqDoc)
103 return true; // not excluded
107 while (reqScorer.Next());
108 reqScorer = null; // exhausted, nothing left
109 return false;
112 public override int Doc()
114 return reqScorer.Doc(); // reqScorer may be null when next() or skipTo() already return false
117 /// <summary>Returns the score of the current document matching the query.
118 /// Initially invalid, until {@link #Next()} is called the first time.
119 /// </summary>
120 /// <returns> The score of the required scorer.
121 /// </returns>
122 public override float Score()
124 return reqScorer.Score(); // reqScorer may be null when next() or skipTo() already return false
127 /// <summary>Skips to the first match beyond the current whose document number is
128 /// greater than or equal to a given target.
129 /// <br>When this method is used the {@link #Explain(int)} method should not be used.
130 /// </summary>
131 /// <param name="target">The target document number.
132 /// </param>
133 /// <returns> true iff there is such a match.
134 /// </returns>
135 public override bool SkipTo(int target)
137 if (firstTime)
139 firstTime = false;
140 if (!exclScorer.SkipTo(target))
142 exclScorer = null; // exhausted
145 if (reqScorer == null)
147 return false;
149 if (exclScorer == null)
151 return reqScorer.SkipTo(target);
153 if (!reqScorer.SkipTo(target))
155 reqScorer = null;
156 return false;
158 return ToNonExcluded();
161 public override Explanation Explain(int doc)
163 Explanation res = new Explanation();
164 if (exclScorer.SkipTo(doc) && (exclScorer.Doc() == doc))
166 res.SetDescription("excluded");
168 else
170 res.SetDescription("not excluded");
171 res.AddDetail(reqScorer.Explain(doc));
173 return res;