Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 {
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 
89  public override Lucene.Net.Search.Spans.Spans GetSpans(IndexReader reader)
90  {
91  throw new InvalidOperationException("Query should have been rewritten");
92  }
93 
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 
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 
139  public override bool Equals(object obj)
140  {
141  if (obj as SpanRegexQuery == null) return false;
142 
143  return Equals((SpanRegexQuery) obj);
144  }
145 
150  public override int GetHashCode()
151  {
152  return 29 * _regexImpl.GetHashCode() + _term.GetHashCode();
153  }
154  }
155 }