/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 System.Collections.Generic;
using System.Text;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Search.Spans;
using Lucene.Net.Util;
namespace Contrib.Regex
{
///
/// A SpanQuery version of allowing regular expression queries to be nested
/// within other SpanQuery subclasses.
///
/// http://www.java2s.com/Open-Source/Java-Document/Net/lucene-connector/org/apache/lucene/search/regex/SpanRegexQuery.java.htm
public class SpanRegexQuery : SpanQuery, IRegexQueryCapable, IEquatable
{
private IRegexCapabilities _regexImpl = new CSharpRegexCapabilities();
private readonly Term _term;
public SpanRegexQuery(Term term)
{
_term = term;
}
public Term Term
{
get { return _term; }
}
public override string ToString(string field)
{
StringBuilder sb = new StringBuilder();
sb.Append("SpanRegexQuery(");
sb.Append(_term);
sb.Append(')');
sb.Append(ToStringUtils.Boost(Boost));
return sb.ToString();
}
public override Query Rewrite(IndexReader reader)
{
RegexQuery orig = new RegexQuery(_term);
orig.RegexImplementation = _regexImpl;
// RegexQuery (via MultiTermQuery).Rewrite always returns a BooleanQuery
orig.RewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; //@@
BooleanQuery bq = (BooleanQuery) orig.Rewrite(reader);
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.Query;
sqs[i] = new SpanTermQuery(tq.Term);
sqs[i].Boost = tq.Boost;
} //efor
SpanOrQuery query = new SpanOrQuery(sqs);
query.Boost = orig.Boost;
return query;
}
/// Expert: Returns the matches for this query in an index. Used internally
/// to search for spans.
///
public override Lucene.Net.Search.Spans.Spans GetSpans(IndexReader reader)
{
throw new InvalidOperationException("Query should have been rewritten");
}
/// Returns the name of the field matched by this query.
public override string Field
{
get
{
return _term.Field;
}
}
public ICollection GetTerms()
{
ICollection terms = new List(){_term};
return terms;
}
public IRegexCapabilities RegexImplementation
{
set { _regexImpl = value; }
get { return _regexImpl; }
}
///
/// Indicates whether the current object is equal to another object of the same type.
///
///
/// true if the current object is equal to the parameter; otherwise, false.
///
/// An object to compare with this object.
///
public bool Equals(SpanRegexQuery other)
{
if (other == null) return false;
if (ReferenceEquals(this, other)) return true;
if (!_regexImpl.Equals(other._regexImpl)) return false;
if (!_term.Equals(other._term)) return false;
return true;
}
///
/// True if this object equals the specified object.
///
/// object
/// true on equality
public override bool Equals(object obj)
{
if (obj as SpanRegexQuery == null) return false;
return Equals((SpanRegexQuery) obj);
}
///
/// Get hash code for this object.
///
/// hash code
public override int GetHashCode()
{
return 29 * _regexImpl.GetHashCode() + _term.GetHashCode();
}
}
}