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
ReqExclScorer.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 
20 namespace Lucene.Net.Search
21 {
22 
23 
24  /// <summary>A Scorer for queries with a required subscorer
25  /// and an excluding (prohibited) sub DocIdSetIterator.
26  /// <br/>
27  /// This <c>Scorer</c> implements <see cref="DocIdSetIterator.Advance(int)" />,
28  /// and it uses the skipTo() on the given scorers.
29  /// </summary>
31  {
32  private Scorer reqScorer;
33  private DocIdSetIterator exclDisi;
34  private int doc = - 1;
35 
36  /// <summary>Construct a <c>ReqExclScorer</c>.</summary>
37  /// <param name="reqScorer">The scorer that must match, except where
38  /// </param>
39  /// <param name="exclDisi">indicates exclusion.
40  /// </param>
41  public ReqExclScorer(Scorer reqScorer, DocIdSetIterator exclDisi):base(null)
42  { // No similarity used.
43  this.reqScorer = reqScorer;
44  this.exclDisi = exclDisi;
45  }
46 
47  public override int NextDoc()
48  {
49  if (reqScorer == null)
50  {
51  return doc;
52  }
53  doc = reqScorer.NextDoc();
54  if (doc == NO_MORE_DOCS)
55  {
56  reqScorer = null; // exhausted, nothing left
57  return doc;
58  }
59  if (exclDisi == null)
60  {
61  return doc;
62  }
63  return doc = ToNonExcluded();
64  }
65 
66  /// <summary>Advance to non excluded doc.
67  /// <br/>On entry:
68  /// <list type="bullet">
69  /// <item>reqScorer != null, </item>
70  /// <item>exclScorer != null, </item>
71  /// <item>reqScorer was advanced once via next() or skipTo()
72  /// and reqScorer.doc() may still be excluded.</item>
73  /// </list>
74  /// Advances reqScorer a non excluded required doc, if any.
75  /// </summary>
76  /// <returns> true iff there is a non excluded required doc.
77  /// </returns>
78  private int ToNonExcluded()
79  {
80  int exclDoc = exclDisi.DocID();
81  int reqDoc = reqScorer.DocID(); // may be excluded
82  do
83  {
84  if (reqDoc < exclDoc)
85  {
86  return reqDoc; // reqScorer advanced to before exclScorer, ie. not excluded
87  }
88  else if (reqDoc > exclDoc)
89  {
90  exclDoc = exclDisi.Advance(reqDoc);
91  if (exclDoc == NO_MORE_DOCS)
92  {
93  exclDisi = null; // exhausted, no more exclusions
94  return reqDoc;
95  }
96  if (exclDoc > reqDoc)
97  {
98  return reqDoc; // not excluded
99  }
100  }
101  }
102  while ((reqDoc = reqScorer.NextDoc()) != NO_MORE_DOCS);
103  reqScorer = null; // exhausted, nothing left
104  return NO_MORE_DOCS;
105  }
106 
107  public override int DocID()
108  {
109  return doc;
110  }
111 
112  /// <summary>Returns the score of the current document matching the query.
113  /// Initially invalid, until <see cref="NextDoc()" /> is called the first time.
114  /// </summary>
115  /// <returns> The score of the required scorer.
116  /// </returns>
117  public override float Score()
118  {
119  return reqScorer.Score(); // reqScorer may be null when next() or skipTo() already return false
120  }
121 
122  public override int Advance(int target)
123  {
124  if (reqScorer == null)
125  {
126  return doc = NO_MORE_DOCS;
127  }
128  if (exclDisi == null)
129  {
130  return doc = reqScorer.Advance(target);
131  }
132  if (reqScorer.Advance(target) == NO_MORE_DOCS)
133  {
134  reqScorer = null;
135  return doc = NO_MORE_DOCS;
136  }
137  return doc = ToNonExcluded();
138  }
139  }
140 }