MtasSpanNotQuery.java.html 12.7 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>MtasSpanNotQuery.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">MtasSpanNotQuery.java</span></div><h1>MtasSpanNotQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext;
import org.apache.lucene.index.Terms;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.SpanNotQuery;
import org.apache.lucene.search.spans.SpanWeight;
import org.apache.lucene.search.spans.Spans;

import mtas.search.spans.util.MtasSpanQuery;
import mtas.search.spans.util.MtasSpanWeight;
import mtas.search.spans.util.MtasSpans;

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

  /** The field. */
  private String field;

  /** The base query. */
  private SpanNotQuery baseQuery;

  /** The q 1. */
  private MtasSpanQuery q1;

  /** The q 2. */
  private MtasSpanQuery q2;

  /**
   * Instantiates a new mtas span not query.
   *
   * @param q1 the q 1
   * @param q2 the q 2
   */
  public MtasSpanNotQuery(MtasSpanQuery q1, MtasSpanQuery q2) {
<span class="pc bpc" id="L47" title="2 of 4 branches missed.">    super(q1 != null ? q1.getMinimumWidth() : null,</span>
<span class="pc" id="L48">        q2 != null ? q2.getMaximumWidth() : null);</span>
<span class="pc bpc" id="L49" title="2 of 4 branches missed.">    if (q1 != null &amp;&amp; (field = q1.getField()) != null) {</span>
<span class="pc bpc" id="L50" title="3 of 6 branches missed.">      if (q2 != null &amp;&amp; q2.getField() != null &amp;&amp; !q2.getField().equals(field)) {</span>
<span class="nc" id="L51">        throw new IllegalArgumentException(&quot;Clauses must have same field.&quot;);</span>
      }
<span class="nc bnc" id="L53" title="All 2 branches missed.">    } else if (q2 != null) {</span>
<span class="nc" id="L54">      field = q2.getField();</span>
    } else {
<span class="nc" id="L56">      field = null;</span>
    }
<span class="fc" id="L58">    this.q1 = q1;</span>
<span class="fc" id="L59">    this.q2 = q2;</span>
<span class="fc" id="L60">    baseQuery = new SpanNotQuery(q1, q2);</span>
<span class="fc" id="L61">  }</span>

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

  /*
   * (non-Javadoc)
   * 
   * @see
   * mtas.search.spans.util.MtasSpanQuery#createWeight(org.apache.lucene.search.
   * IndexSearcher, boolean)
   */
  @Override
  public MtasSpanWeight createWeight(IndexSearcher searcher,
      boolean needsScores, float boost) throws IOException {
    // return baseQuery.createWeight(searcher, needsScores);
<span class="pc bpc" id="L84" title="2 of 4 branches missed.">    if (q1 == null || q2 == null) {</span>
<span class="nc" id="L85">      return null;</span>
    } else {
<span class="fc" id="L87">      MtasSpanNotQueryWeight w1 = new MtasSpanNotQueryWeight(</span>
<span class="fc" id="L88">          q1.createWeight(searcher, needsScores, boost));</span>
<span class="fc" id="L89">      MtasSpanNotQueryWeight w2 = new MtasSpanNotQueryWeight(</span>
<span class="fc" id="L90">          q2.createWeight(searcher, needsScores, boost));</span>
      // subWeights
<span class="fc" id="L92">      List&lt;MtasSpanNotQueryWeight&gt; subWeights = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L93">      subWeights.add(w1);</span>
<span class="fc" id="L94">      subWeights.add(w2);</span>
      // return
<span class="pc bpc" id="L96" title="1 of 2 branches missed.">      return new SpanNotWeight(w1, w2, searcher,</span>
<span class="pc" id="L97">          needsScores ? getTermContexts(subWeights) : null, boost);</span>
    }
  }

  /**
   * Gets the term contexts.
   *
   * @param items the items
   * @return the term contexts
   */
  protected Map&lt;Term, TermContext&gt; getTermContexts(
      List&lt;MtasSpanNotQueryWeight&gt; items) {
<span class="nc" id="L109">    List&lt;SpanWeight&gt; weights = new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L110" title="All 2 branches missed.">    for (MtasSpanNotQueryWeight item : items) {</span>
<span class="nc" id="L111">      weights.add(item.spanWeight);</span>
<span class="nc" id="L112">    }</span>
<span class="nc" id="L113">    return getTermContexts(weights);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpanQuery#rewrite(org.apache.lucene.index.
   * IndexReader)
   */
  @Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
<span class="fc" id="L124">    MtasSpanQuery newQ1 = (MtasSpanQuery) q1.rewrite(reader);</span>
<span class="fc" id="L125">    MtasSpanQuery newQ2 = (MtasSpanQuery) q2.rewrite(reader);</span>
<span class="pc bpc" id="L126" title="1 of 4 branches missed.">    if (!newQ1.equals(q1) || !newQ2.equals(q2)) {</span>
<span class="fc" id="L127">      return new MtasSpanNotQuery(newQ1, newQ2).rewrite(reader);</span>
    } else {
<span class="fc" id="L129">      baseQuery = (SpanNotQuery) baseQuery.rewrite(reader);</span>
<span class="fc" id="L130">      return super.rewrite(reader);</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L141">    StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L142">    buffer.append(this.getClass().getSimpleName() + &quot;([&quot;);</span>
<span class="nc bnc" id="L143" title="All 2 branches missed.">    if (q1 != null) {</span>
<span class="nc" id="L144">      buffer.append(q1.toString(q1.getField()));</span>
    } else {
<span class="nc" id="L146">      buffer.append(&quot;null&quot;);</span>
    }
<span class="nc" id="L148">    buffer.append(&quot;,&quot;);</span>
<span class="nc bnc" id="L149" title="All 2 branches missed.">    if (q2 != null) {</span>
<span class="nc" id="L150">      buffer.append(q2.toString(q2.getField()));</span>
    } else {
<span class="nc" id="L152">      buffer.append(&quot;null&quot;);</span>
    }
<span class="nc" id="L154">    buffer.append(&quot;])&quot;);</span>
<span class="nc" id="L155">    return buffer.toString();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
<span class="fc bfc" id="L165" title="All 2 branches covered.">    if (this == obj)</span>
<span class="fc" id="L166">      return true;</span>
<span class="pc bpc" id="L167" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L168">      return false;</span>
<span class="fc bfc" id="L169" title="All 2 branches covered.">    if (getClass() != obj.getClass())</span>
<span class="fc" id="L170">      return false;</span>
<span class="fc" id="L171">    final MtasSpanNotQuery that = (MtasSpanNotQuery) obj;</span>
<span class="fc" id="L172">    return baseQuery.equals(that.baseQuery);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#hashCode()
   */
  @Override
  public int hashCode() {
<span class="nc" id="L182">    return baseQuery.hashCode();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
   */
  @Override
  public void disableTwoPhaseIterator() {
<span class="fc" id="L192">    super.disableTwoPhaseIterator();</span>
<span class="fc" id="L193">    q1.disableTwoPhaseIterator();</span>
<span class="fc" id="L194">    q2.disableTwoPhaseIterator();</span>
<span class="fc" id="L195">  }</span>

  /**
   * The Class SpanNotWeight.
   */
  protected class SpanNotWeight extends MtasSpanWeight {

    /** The w 1. */
    MtasSpanNotQueryWeight w1;

    /** The w 2. */
    MtasSpanNotQueryWeight w2;

    /**
     * Instantiates a new span not weight.
     *
     * @param w1 the w 1
     * @param w2 the w 2
     * @param searcher the searcher
     * @param termContexts the term contexts
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public SpanNotWeight(MtasSpanNotQueryWeight w1, MtasSpanNotQueryWeight w2,
        IndexSearcher searcher, Map&lt;Term, TermContext&gt; termContexts, float boost)
<span class="fc" id="L219">        throws IOException {</span>
<span class="fc" id="L220">      super(MtasSpanNotQuery.this, searcher, termContexts, boost);</span>
<span class="fc" id="L221">      this.w1 = w1;</span>
<span class="fc" id="L222">      this.w2 = w2;</span>
<span class="fc" id="L223">    }</span>

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.lucene.search.spans.SpanWeight#extractTermContexts(java.util.
     * Map)
     */
    @Override
    public void extractTermContexts(Map&lt;Term, TermContext&gt; contexts) {
<span class="nc" id="L234">      w1.spanWeight.extractTermContexts(contexts);</span>
<span class="nc" id="L235">      w2.spanWeight.extractTermContexts(contexts);</span>
<span class="nc" id="L236">    }</span>

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.lucene.search.spans.SpanWeight#getSpans(org.apache.lucene.
     * index.LeafReaderContext,
     * org.apache.lucene.search.spans.SpanWeight.Postings)
     */
    @Override
    public MtasSpans getSpans(LeafReaderContext context,
        Postings requiredPostings) throws IOException {
<span class="fc" id="L249">      Terms terms = context.reader().terms(field);</span>
<span class="pc bpc" id="L250" title="1 of 2 branches missed.">      if (terms == null) {</span>
<span class="nc" id="L251">        return null; // field does not exist</span>
      }
<span class="fc" id="L253">      MtasSpanNotQuerySpans s1 = new MtasSpanNotQuerySpans(</span>
          MtasSpanNotQuery.this,
<span class="fc" id="L255">          w1.spanWeight.getSpans(context, requiredPostings));</span>
<span class="fc" id="L256">      MtasSpanNotQuerySpans s2 = new MtasSpanNotQuerySpans(</span>
          MtasSpanNotQuery.this,
<span class="fc" id="L258">          w2.spanWeight.getSpans(context, requiredPostings));</span>
<span class="fc" id="L259">      return new MtasSpanNotSpans(MtasSpanNotQuery.this, s1, s2);</span>
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.lucene.search.Weight#extractTerms(java.util.Set)
     */
    @Override
    public void extractTerms(Set&lt;Term&gt; terms) {
<span class="nc" id="L269">      w1.spanWeight.extractTerms(terms);</span>
<span class="nc" id="L270">      w2.spanWeight.extractTerms(terms);</span>
<span class="nc" id="L271">    }</span>

  }

  /**
   * The Class MtasSpanNotQuerySpans.
   */
  protected static class MtasSpanNotQuerySpans {

    /** The spans. */
    public Spans spans;

    /**
     * Instantiates a new mtas span not query spans.
     *
     * @param query the query
     * @param spans the spans
     */
<span class="fc" id="L289">    public MtasSpanNotQuerySpans(MtasSpanNotQuery query, Spans spans) {</span>
<span class="pc bpc" id="L290" title="1 of 2 branches missed.">      this.spans = spans != null ? spans : new MtasSpanMatchNoneSpans(query);</span>
<span class="fc" id="L291">    }</span>

  }

  /**
   * The Class MtasSpanNotQueryWeight.
   */
  private static class MtasSpanNotQueryWeight {

    /** The span weight. */
    public SpanWeight spanWeight;

    /**
     * Instantiates a new mtas span not query weight.
     *
     * @param spanWeight the span weight
     */
<span class="fc" id="L308">    public MtasSpanNotQueryWeight(SpanWeight spanWeight) {</span>
<span class="fc" id="L309">      this.spanWeight = spanWeight;</span>
<span class="fc" id="L310">    }</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>