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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 namespace Mono.Lucene.Net.Search
24 /// <summary>A Scorer for queries with a required subscorer
25 /// and an excluding (prohibited) sub DocIdSetIterator.
27 /// This <code>Scorer</code> implements {@link Scorer#SkipTo(int)},
28 /// and it uses the skipTo() on the given scorers.
30 class ReqExclScorer:Scorer
32 private Scorer reqScorer;
33 private DocIdSetIterator exclDisi;
34 private int doc = - 1;
36 /// <summary>Construct a <code>ReqExclScorer</code>.</summary>
37 /// <param name="reqScorer">The scorer that must match, except where
39 /// <param name="exclDisi">indicates exclusion.
41 public ReqExclScorer(Scorer reqScorer, DocIdSetIterator exclDisi):base(null)
42 { // No similarity used.
43 this.reqScorer = reqScorer;
44 this.exclDisi = exclDisi;
47 /// <deprecated> use {@link #NextDoc()} instead.
49 [Obsolete("use NextDoc() instead. ")]
50 public override bool Next()
52 return NextDoc() != NO_MORE_DOCS;
55 public override int NextDoc()
57 if (reqScorer == null)
61 doc = reqScorer.NextDoc();
62 if (doc == NO_MORE_DOCS)
64 reqScorer = null; // exhausted, nothing left
71 return doc = ToNonExcluded();
74 /// <summary>Advance to non excluded doc.
77 /// <li>reqScorer != null, </li>
78 /// <li>exclScorer != null, </li>
79 /// <li>reqScorer was advanced once via next() or skipTo()
80 /// and reqScorer.doc() may still be excluded.</li>
82 /// Advances reqScorer a non excluded required doc, if any.
84 /// <returns> true iff there is a non excluded required doc.
86 private int ToNonExcluded()
88 int exclDoc = exclDisi.DocID();
89 int reqDoc = reqScorer.DocID(); // may be excluded
94 return reqDoc; // reqScorer advanced to before exclScorer, ie. not excluded
96 else if (reqDoc > exclDoc)
98 exclDoc = exclDisi.Advance(reqDoc);
99 if (exclDoc == NO_MORE_DOCS)
101 exclDisi = null; // exhausted, no more exclusions
104 if (exclDoc > reqDoc)
106 return reqDoc; // not excluded
110 while ((reqDoc = reqScorer.NextDoc()) != NO_MORE_DOCS);
111 reqScorer = null; // exhausted, nothing left
115 /// <deprecated> use {@link #DocID()} instead.
117 [Obsolete("use DocID() instead.")]
118 public override int Doc()
120 return reqScorer.Doc(); // reqScorer may be null when next() or skipTo() already return false
123 public override int DocID()
128 /// <summary>Returns the score of the current document matching the query.
129 /// Initially invalid, until {@link #Next()} is called the first time.
131 /// <returns> The score of the required scorer.
133 public override float Score()
135 return reqScorer.Score(); // reqScorer may be null when next() or skipTo() already return false
138 /// <deprecated> use {@link #Advance(int)} instead.
140 [Obsolete("use Advance(int) instead.")]
141 public override bool SkipTo(int target)
143 return Advance(target) != NO_MORE_DOCS;
146 public override int Advance(int target)
148 if (reqScorer == null)
150 return doc = NO_MORE_DOCS;
152 if (exclDisi == null)
154 return doc = reqScorer.Advance(target);
156 if (reqScorer.Advance(target) == NO_MORE_DOCS)
159 return doc = NO_MORE_DOCS;
161 return doc = ToNonExcluded();
164 public override Explanation Explain(int doc)
166 Explanation res = new Explanation();
167 if (exclDisi.Advance(doc) == doc)
169 res.SetDescription("excluded");
173 res.SetDescription("not excluded");
174 res.AddDetail(reqScorer.Explain(doc));