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
SpanQueryFilter.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 System.Collections.Generic;
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 using OpenBitSet = Lucene.Net.Util.OpenBitSet;
22 using SpanQuery = Lucene.Net.Search.Spans.SpanQuery;
23 
24 namespace Lucene.Net.Search
25 {
26 
27  /// <summary> Constrains search results to only match those which also match a provided
28  /// query. Also provides position information about where each document matches
29  /// at the cost of extra space compared with the QueryWrapperFilter.
30  /// There is an added cost to this above what is stored in a <see cref="QueryWrapperFilter" />. Namely,
31  /// the position information for each matching document is stored.
32  /// <p/>
33  /// This filter does not cache. See the <see cref="Lucene.Net.Search.CachingSpanFilter" /> for a wrapper that
34  /// caches.
35  ///
36  ///
37  /// </summary>
38  /// <version> $Id:$
39  /// </version>
40  [Serializable]
42  {
43  protected internal SpanQuery internalQuery;
44 
45  protected internal SpanQueryFilter()
46  {
47  }
48 
49  /// <summary>Constructs a filter which only matches documents matching
50  /// <c>query</c>.
51  /// </summary>
52  /// <param name="query">The <see cref="Lucene.Net.Search.Spans.SpanQuery" /> to use as the basis for the Filter.
53  /// </param>
54  public SpanQueryFilter(SpanQuery query)
55  {
56  this.internalQuery = query;
57  }
58 
59  public override DocIdSet GetDocIdSet(IndexReader reader)
60  {
61  SpanFilterResult result = BitSpans(reader);
62  return result.DocIdSet;
63  }
64 
65  public override SpanFilterResult BitSpans(IndexReader reader)
66  {
67 
68  OpenBitSet bits = new OpenBitSet(reader.MaxDoc);
69  Lucene.Net.Search.Spans.Spans spans = internalQuery.GetSpans(reader);
70  IList<SpanFilterResult.PositionInfo> tmp = new List<SpanFilterResult.PositionInfo>(20);
71  int currentDoc = - 1;
72  SpanFilterResult.PositionInfo currentInfo = null;
73  while (spans.Next())
74  {
75  int doc = spans.Doc();
76  bits.Set(doc);
77  if (currentDoc != doc)
78  {
79  currentInfo = new SpanFilterResult.PositionInfo(doc);
80  tmp.Add(currentInfo);
81  currentDoc = doc;
82  }
83  currentInfo.AddPosition(spans.Start(), spans.End());
84  }
85  return new SpanFilterResult(bits, tmp);
86  }
87 
88 
89  public virtual SpanQuery Query
90  {
91  get { return internalQuery; }
92  }
93 
94  public override System.String ToString()
95  {
96  return "SpanQueryFilter(" + internalQuery + ")";
97  }
98 
99  public override bool Equals(System.Object o)
100  {
101  return o is SpanQueryFilter && this.internalQuery.Equals(((SpanQueryFilter) o).internalQuery);
102  }
103 
104  public override int GetHashCode()
105  {
106  return internalQuery.GetHashCode() ^ unchecked((int) 0x923F64B9);
107  }
108  }
109 }