MtasSpanRegexpQuery.java.html 8.31 KB
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasSpanRegexpQuery.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> &gt; <a href="index.source.html" class="el_package">mtas.search.spans</a> &gt; <span class="el_source">MtasSpanRegexpQuery.java</span></div><h1>MtasSpanRegexpQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;

import mtas.analysis.token.MtasToken;
import mtas.codec.util.CodecUtil;
import mtas.search.spans.util.MtasSpanQuery;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
import org.apache.lucene.search.spans.SpanOrQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.search.spans.SpanWeight;

/**
 * The Class MtasSpanRegexpQuery.
 */
public class MtasSpanRegexpQuery extends MtasSpanQuery {

  /** The Constant MTAS_REGEXP_EXPAND_BOUNDARY. */
  private static final int MTAS_REGEXP_EXPAND_BOUNDARY = 1000000;

  /** The prefix. */
  private String prefix;

  /** The value. */
  private String value;

  /** The single position. */
  private boolean singlePosition;

  /** The term. */
  private Term term;

  /** The query. */
  private SpanMultiTermQueryWrapper&lt;RegexpQuery&gt; query;

  /**
   * Instantiates a new mtas span regexp query.
   *
   * @param term the term
   */
  public MtasSpanRegexpQuery(Term term) {
<span class="nc" id="L49">    this(term, true);</span>
<span class="nc" id="L50">  }</span>

  /**
   * Instantiates a new mtas span regexp query.
   *
   * @param term the term
   * @param singlePosition the single position
   */
  public MtasSpanRegexpQuery(Term term, boolean singlePosition) {
<span class="fc bfc" id="L59" title="All 4 branches covered.">    super(singlePosition ? 1 : null, singlePosition ? 1 : null);</span>
<span class="fc" id="L60">    RegexpQuery req = new RegexpQuery(term);</span>
<span class="fc" id="L61">    query = new SpanMultiTermQueryWrapper&lt;&gt;(req);</span>
<span class="fc" id="L62">    this.term = term;</span>
<span class="fc" id="L63">    this.singlePosition = singlePosition;</span>
<span class="fc" id="L64">    int i = term.text().indexOf(MtasToken.DELIMITER);</span>
<span class="pc bpc" id="L65" title="1 of 2 branches missed.">    if (i &gt;= 0) {</span>
<span class="fc" id="L66">      prefix = term.text().substring(0, i);</span>
<span class="fc" id="L67">      value = term.text().substring((i + MtasToken.DELIMITER.length()));</span>
<span class="pc bpc" id="L68" title="1 of 2 branches missed.">      value = (value.length() &gt; 0) ? value : null;</span>
    } else {
<span class="nc" id="L70">      prefix = term.text();</span>
<span class="nc" id="L71">      value = null;</span>
    }
<span class="fc" id="L73">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader)
   */
  @Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
<span class="fc" id="L83">    Query q = query.rewrite(reader);</span>
<span class="pc bpc" id="L84" title="1 of 2 branches missed.">    if (q instanceof SpanOrQuery) {</span>
<span class="fc" id="L85">      SpanQuery[] clauses = ((SpanOrQuery) q).getClauses();</span>
<span class="pc bpc" id="L86" title="1 of 2 branches missed.">      if (clauses.length &gt; MTAS_REGEXP_EXPAND_BOUNDARY) {</span>
        // forward index solution ?
<span class="nc" id="L88">        throw new IOException(&quot;Regexp \&quot;&quot; + CodecUtil.termValue(term.text())</span>
            + &quot;\&quot; expands to &quot; + clauses.length + &quot; terms, too many (boundary &quot;
            + MTAS_REGEXP_EXPAND_BOUNDARY + &quot;)!&quot;);
      }
<span class="fc" id="L92">      MtasSpanQuery[] newClauses = new MtasSpanQuery[clauses.length];</span>
<span class="fc bfc" id="L93" title="All 2 branches covered.">      for (int i = 0; i &lt; clauses.length; i++) {</span>
<span class="pc bpc" id="L94" title="1 of 2 branches missed.">        if (clauses[i] instanceof SpanTermQuery) {</span>
<span class="fc" id="L95">          newClauses[i] = new MtasSpanTermQuery((SpanTermQuery) clauses[i],</span>
<span class="fc" id="L96">              singlePosition).rewrite(reader);</span>
        } else {
<span class="nc" id="L98">          throw new IOException(&quot;no SpanTermQuery after rewrite&quot;);</span>
        }
      }
<span class="fc" id="L101">      return new MtasSpanOrQuery(newClauses).rewrite(reader);</span>
    } else {
<span class="nc" id="L103">      throw new IOException(&quot;no SpanOrQuery after rewrite&quot;);</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.SpanTermQuery#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L115">    StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L116">    buffer.append(this.getClass().getSimpleName() + &quot;([&quot;);</span>
<span class="nc bnc" id="L117" title="All 2 branches missed.">    if (value == null) {</span>
<span class="nc" id="L118">      buffer.append(this.query.getField() + &quot;:&quot; + prefix);</span>
    } else {
<span class="nc" id="L120">      buffer.append(this.query.getField() + &quot;:&quot; + prefix + &quot;=&quot; + value);</span>
    }
<span class="nc" id="L122">    buffer.append(&quot;])&quot;);</span>
<span class="nc" id="L123">    return buffer.toString();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanQuery#getField()
   */
  @Override
  public String getField() {
<span class="fc" id="L133">    return term.field();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.SpanQuery#createWeight(org.apache.lucene.
   * search.IndexSearcher, boolean)
   */
  @Override
  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores)
      throws IOException {
<span class="nc" id="L146">    return ((SpanQuery) searcher.rewrite(query)).createWeight(searcher,</span>
        needsScores);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
<span class="pc bpc" id="L157" title="1 of 2 branches missed.">    if (this == obj)</span>
<span class="nc" id="L158">      return true;</span>
<span class="pc bpc" id="L159" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L160">      return false;</span>
<span class="fc bfc" id="L161" title="All 2 branches covered.">    if (getClass() != obj.getClass())</span>
<span class="fc" id="L162">      return false;</span>
<span class="fc" id="L163">    MtasSpanRegexpQuery that = (MtasSpanRegexpQuery) obj;</span>
<span class="pc bpc" id="L164" title="1 of 4 branches missed.">    return term.equals(that.term) &amp;&amp; singlePosition == that.singlePosition;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#hashCode()
   */
  @Override
  public int hashCode() {
<span class="fc" id="L174">    int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L175">    h = (h * 7) ^ term.hashCode();</span>
<span class="pc bpc" id="L176" title="1 of 2 branches missed.">    h += (singlePosition ? 1 : 0);</span>
<span class="fc" id="L177">    return h;</span>
  }

}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>