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
MatchAllDocsQuery.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 Lucene.Net.Index;
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 using TermDocs = Lucene.Net.Index.TermDocs;
22 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
23 
24 namespace Lucene.Net.Search
25 {
26 
27  /// <summary> A query that matches all documents.
28  ///
29  /// </summary>
30  [Serializable]
31  public class MatchAllDocsQuery:Query
32  {
33 
34  public MatchAllDocsQuery():this(null)
35  {
36  }
37 
38  private System.String normsField;
39 
40  /// <param name="normsField">Field used for normalization factor (document boost). Null if nothing.
41  /// </param>
42  public MatchAllDocsQuery(System.String normsField)
43  {
44  this.normsField = normsField;
45  }
46 
47  private class MatchAllScorer:Scorer
48  {
49  private void InitBlock(MatchAllDocsQuery enclosingInstance)
50  {
51  this.enclosingInstance = enclosingInstance;
52  }
53  private MatchAllDocsQuery enclosingInstance;
54  public MatchAllDocsQuery Enclosing_Instance
55  {
56  get
57  {
58  return enclosingInstance;
59  }
60 
61  }
62  internal TermDocs termDocs;
63  internal float score;
64  internal byte[] norms;
65  private int doc = - 1;
66 
67  internal MatchAllScorer(MatchAllDocsQuery enclosingInstance, IndexReader reader, Similarity similarity, Weight w, byte[] norms):base(similarity)
68  {
69  InitBlock(enclosingInstance);
70  this.termDocs = reader.TermDocs(null);
71  score = w.Value;
72  this.norms = norms;
73  }
74 
75  public override int DocID()
76  {
77  return doc;
78  }
79 
80  public override int NextDoc()
81  {
82  return doc = termDocs.Next()?termDocs.Doc:NO_MORE_DOCS;
83  }
84 
85  public override float Score()
86  {
87  return norms == null?score:score * Similarity.DecodeNorm(norms[DocID()]);
88  }
89 
90  public override int Advance(int target)
91  {
92  return doc = termDocs.SkipTo(target)?termDocs.Doc:NO_MORE_DOCS;
93  }
94  }
95 
96  [Serializable]
97  private class MatchAllDocsWeight:Weight
98  {
99  private void InitBlock(MatchAllDocsQuery enclosingInstance)
100  {
101  this.enclosingInstance = enclosingInstance;
102  }
103  private MatchAllDocsQuery enclosingInstance;
104  public MatchAllDocsQuery Enclosing_Instance
105  {
106  get
107  {
108  return enclosingInstance;
109  }
110 
111  }
112  private Similarity similarity;
113  private float queryWeight;
114  private float queryNorm;
115 
116  public MatchAllDocsWeight(MatchAllDocsQuery enclosingInstance, Searcher searcher)
117  {
118  InitBlock(enclosingInstance);
119  this.similarity = searcher.Similarity;
120  }
121 
122  public override System.String ToString()
123  {
124  return "weight(" + Enclosing_Instance + ")";
125  }
126 
127  public override Query Query
128  {
129  get { return Enclosing_Instance; }
130  }
131 
132  public override float Value
133  {
134  get { return queryWeight; }
135  }
136 
137  public override float GetSumOfSquaredWeights()
138  {
139  queryWeight = Enclosing_Instance.Boost;
140  return queryWeight*queryWeight;
141  }
142 
143  public override void Normalize(float queryNorm)
144  {
145  this.queryNorm = queryNorm;
146  queryWeight *= this.queryNorm;
147  }
148 
149  public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
150  {
151  return new MatchAllScorer(enclosingInstance, reader, similarity, this, Enclosing_Instance.normsField != null?reader.Norms(Enclosing_Instance.normsField):null);
152  }
153 
154  public override Explanation Explain(IndexReader reader, int doc)
155  {
156  // explain query weight
157  Explanation queryExpl = new ComplexExplanation(true, Value, "MatchAllDocsQuery, product of:");
158  if (Enclosing_Instance.Boost != 1.0f)
159  {
160  queryExpl.AddDetail(new Explanation(Enclosing_Instance.Boost, "boost"));
161  }
162  queryExpl.AddDetail(new Explanation(queryNorm, "queryNorm"));
163 
164  return queryExpl;
165  }
166  }
167 
168  public override Weight CreateWeight(Searcher searcher)
169  {
170  return new MatchAllDocsWeight(this, searcher);
171  }
172 
173  public override void ExtractTerms(System.Collections.Generic.ISet<Term> terms)
174  {
175  }
176 
177  public override System.String ToString(System.String field)
178  {
179  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
180  buffer.Append("*:*");
181  buffer.Append(ToStringUtils.Boost(Boost));
182  return buffer.ToString();
183  }
184 
185  public override bool Equals(System.Object o)
186  {
187  if (!(o is MatchAllDocsQuery))
188  return false;
189  MatchAllDocsQuery other = (MatchAllDocsQuery) o;
190  return this.Boost == other.Boost;
191  }
192 
193  public override int GetHashCode()
194  {
195  return BitConverter.ToInt32(BitConverter.GetBytes(Boost), 0) ^ 0x1AA71190;
196  }
197  }
198 }