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
SpanRegexQuery.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.Text;
21 using Lucene.Net.Index;
22 using Lucene.Net.Search;
23 using Lucene.Net.Search.Spans;
24 using Lucene.Net.Util;
25 
26 namespace Contrib.Regex
27 {
28  /// <summary>
29  /// A SpanQuery version of <see cref="RegexQuery"/> allowing regular expression queries to be nested
30  /// within other SpanQuery subclasses.
31  /// </summary>
32  /// <remarks>http://www.java2s.com/Open-Source/Java-Document/Net/lucene-connector/org/apache/lucene/search/regex/SpanRegexQuery.java.htm</remarks>
33  public class SpanRegexQuery : SpanQuery, IRegexQueryCapable, IEquatable<SpanRegexQuery>
34  {
35  private IRegexCapabilities _regexImpl = new CSharpRegexCapabilities();
36  private readonly Term _term;
37 
38  public SpanRegexQuery(Term term)
39  {
40  _term = term;
41  }
42 
43  public Term Term
44  {
45  get { return _term; }
46  }
47 
48  public override string ToString(string field)
49  {
50  StringBuilder sb = new StringBuilder();
51  sb.Append("SpanRegexQuery(");
52  sb.Append(_term);
53  sb.Append(')');
54  sb.Append(ToStringUtils.Boost(Boost));
55  return sb.ToString();
56  }
57 
58  public override Query Rewrite(IndexReader reader)
59  {
60  RegexQuery orig = new RegexQuery(_term);
61  orig.RegexImplementation = _regexImpl;
62 
63  // RegexQuery (via MultiTermQuery).Rewrite always returns a BooleanQuery
65  BooleanQuery bq = (BooleanQuery) orig.Rewrite(reader);
66 
67  BooleanClause[] clauses = bq.GetClauses();
68  SpanQuery[] sqs = new SpanQuery[clauses.Length];
69  for (int i = 0; i < clauses.Length; i++)
70  {
71  BooleanClause clause = clauses[i];
72 
73  // Clauses from RegexQuery.Rewrite are always TermQuery's
74  TermQuery tq = (TermQuery) clause.Query;
75 
76  sqs[i] = new SpanTermQuery(tq.Term);
77  sqs[i].Boost = tq.Boost;
78  } //efor
79 
80  SpanOrQuery query = new SpanOrQuery(sqs);
81  query.Boost = orig.Boost;
82 
83  return query;
84  }
85 
86  /// <summary>Expert: Returns the matches for this query in an index. Used internally
87  /// to search for spans.
88  /// </summary>
89  public override Lucene.Net.Search.Spans.Spans GetSpans(IndexReader reader)
90  {
91  throw new InvalidOperationException("Query should have been rewritten");
92  }
93 
94  /// <summary>Returns the name of the field matched by this query.</summary>
95  public override string Field
96  {
97  get
98  {
99  return _term.Field;
100  }
101  }
102 
103  public ICollection<Term> GetTerms()
104  {
105  ICollection<Term> terms = new List<Term>(){_term};
106  return terms;
107  }
108 
109  public IRegexCapabilities RegexImplementation
110  {
111  set { _regexImpl = value; }
112  get { return _regexImpl; }
113  }
114 
115  /// <summary>
116  /// Indicates whether the current object is equal to another object of the same type.
117  /// </summary>
118  /// <returns>
119  /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
120  /// </returns>
121  /// <param name="other">An object to compare with this object.
122  /// </param>
123  public bool Equals(SpanRegexQuery other)
124  {
125  if (other == null) return false;
126  if (ReferenceEquals(this, other)) return true;
127 
128  if (!_regexImpl.Equals(other._regexImpl)) return false;
129  if (!_term.Equals(other._term)) return false;
130 
131  return true;
132  }
133 
134  /// <summary>
135  /// True if this object equals the specified object.
136  /// </summary>
137  /// <param name="obj">object</param>
138  /// <returns>true on equality</returns>
139  public override bool Equals(object obj)
140  {
141  if (obj as SpanRegexQuery == null) return false;
142 
143  return Equals((SpanRegexQuery) obj);
144  }
145 
146  /// <summary>
147  /// Get hash code for this object.
148  /// </summary>
149  /// <returns>hash code</returns>
150  public override int GetHashCode()
151  {
152  return 29 * _regexImpl.GetHashCode() + _term.GetHashCode();
153  }
154  }
155 }