/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using IndexReaderLucene.Net.Index.IndexReader;
using TermLucene.Net.Index.Term;
using BooleanClauseLucene.Net.Search.BooleanClause;
using BooleanQueryLucene.Net.Search.BooleanQuery;
using QueryLucene.Net.Search.Query;
using TermQueryLucene.Net.Search.TermQuery;
using SpanOrQueryLucene.Net.Search.Spans.SpanOrQuery;
using SpanQueryLucene.Net.Search.Spans.SpanQuery;
using SpanTermQueryLucene.Net.Search.Spans.SpanTermQuery;
using ToStringUtilsLucene.Net.Util.ToStringUtils;
namespace Lucene.Net.Search.Regex{
[Serializable]
public class SpanRegexQuery:SpanQuery
{
virtual public Term Term
{
get
{
return term;
}
}
private Term term;
public SpanRegexQuery(Term term)
{
this.term = term;
}
public override Query Rewrite(IndexReader reader)
{
Query orig = new RegexQuery(term).Rewrite(reader);
// RegexQuery (via MultiTermQuery).rewrite always returns a BooleanQuery
BooleanQuery bq = (BooleanQuery) orig;
BooleanClause[] clauses = bq.GetClauses();
SpanQuery[] sqs = new SpanQuery[clauses.Length];
for (int i = 0; i < clauses.Length; i++)
{
BooleanClause clause = clauses[i];
// Clauses from RegexQuery.rewrite are always TermQuery's
TermQuery tq = (TermQuery) clause.GetQuery();
sqs[i] = new SpanTermQuery(tq.GetTerm());
sqs[i].SetBoost(tq.GetBoost());
}
SpanOrQuery query = new SpanOrQuery(sqs);
query.SetBoost(orig.GetBoost());
return query;
}
public override Lucene.Net.Search.Spans.Spans GetSpans(IndexReader reader)
{
throw new System.NotSupportedException("Query should have been rewritten");
}
public override System.String GetField()
{
return term.Field();
}
public override System.Collections.ICollection GetTerms()
{
System.Collections.ArrayList terms = new System.Collections.ArrayList();
terms.Add(term);
return terms;
}
public override bool Equals(System.Object o)
{
if (this == o)
return true;
if (o == null || GetType() != o.GetType())
return false;
SpanRegexQuery that = (SpanRegexQuery) o;
return term.Equals(that.term) && GetBoost() == that.GetBoost();
}
public override int GetHashCode()
{
return term.GetHashCode();
}
public override System.String ToString(System.String field)
{
System.Text.StringBuilder buffer = new System.Text.StringBuilder();
buffer.Append("spanRegexQuery(");
buffer.Append(term);
buffer.Append(")");
buffer.Append(ToStringUtils.Boost(GetBoost()));
return buffer.ToString();
}
}
}
|