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
TermsFilter.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 System.Linq;
21 using System.Text;
22 
23 using Lucene.Net.Index;
24 using Lucene.Net.Util;
25 
26 namespace Lucene.Net.Search
27 {
28  /// <summary>
29  /// A filter that contains multiple terms.
30  /// </summary>
31  public class TermsFilter : Filter
32  {
33  /// <summary>
34  /// The set of terms for this filter.
35  /// </summary>
36  protected ISet<Term> terms = new SortedSet<Term>();
37 
38  /// <summary>
39  /// Add a term to the set.
40  /// </summary>
41  /// <param name="term">The term to add.</param>
42  public void AddTerm(Term term)
43  {
44  terms.Add(term);
45  }
46 
47  /// <summary>
48  /// Get the DocIdSet.
49  /// </summary>
50  /// <param name="reader">Applcible reader.</param>
51  /// <returns>The set.</returns>
52  public override DocIdSet GetDocIdSet(IndexReader reader)
53  {
54  OpenBitSet result = new OpenBitSet(reader.MaxDoc);
55  TermDocs td = reader.TermDocs();
56  try
57  {
58  foreach (Term t in this.terms)
59  {
60  td.Seek(t);
61  while (td.Next())
62  {
63  result.Set(td.Doc);
64  }
65  }
66  }
67  finally
68  {
69  td.Close();
70  }
71 
72  return result;
73  }
74 
75  public override bool Equals(Object obj)
76  {
77  if (this == obj)
78  {
79  return true;
80  }
81  if ((obj == null) || !(obj is TermsFilter))
82  {
83  return false;
84  }
85  TermsFilter test = (TermsFilter)obj;
86  // TODO: Does SortedSet have an issues like List<T>? see EquatableList in Support
87  return (terms == test.terms || (terms != null && terms.Equals(test.terms)));
88  }
89 
90  public override int GetHashCode()
91  {
92  int hash = 9;
93  foreach (Term t in this.terms)
94  {
95  hash = 31 * hash + t.GetHashCode();
96  }
97  return hash;
98  }
99 
100  public override string ToString()
101  {
102  StringBuilder sb = new StringBuilder();
103  sb.Append("(");
104  foreach (Term t in this.terms)
105  {
106  sb.AppendFormat(" {0}:{1}", t.Field, t.Text);
107  }
108  sb.Append(" )");
109  return sb.ToString();
110  }
111  }
112 }