<?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>MtasExtendedSpanTermQuery.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> > <a href="index.source.html" class="el_package">mtas.search.spans.util</a> > <span class="el_source">MtasExtendedSpanTermQuery.java</span></div><h1>MtasExtendedSpanTermQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans.util; import java.io.IOException; import java.lang.reflect.Method; import java.util.Collections; import java.util.Map; import java.util.Set; import mtas.analysis.token.MtasToken; import mtas.codec.util.CodecUtil; import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.IndexReaderContext; import org.apache.lucene.index.LeafReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermContext; import org.apache.lucene.index.TermState; import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.spans.FilterSpans; import org.apache.lucene.search.spans.SpanTermQuery; import org.apache.lucene.search.spans.SpanWeight; import org.apache.lucene.search.spans.Spans; /** * The Class MtasExtendedSpanTermQuery. */ public class MtasExtendedSpanTermQuery extends SpanTermQuery { /** The prefix. */ private String prefix; /** The value. */ private String value; /** The single position. */ private boolean singlePosition; /** The local term. */ private Term localTerm; /** * Instantiates a new mtas extended span term query. * * @param term the term */ public MtasExtendedSpanTermQuery(Term term) { <span class="nc" id="L49"> this(term, true);</span> <span class="nc" id="L50"> }</span> /** * Instantiates a new mtas extended span term query. * * @param term the term * @param singlePosition the single position */ private MtasExtendedSpanTermQuery(Term term, boolean singlePosition) { <span class="nc" id="L59"> this(new SpanTermQuery(term), singlePosition);</span> <span class="nc" id="L60"> }</span> /** * Instantiates a new mtas extended span term query. * * @param query the query * @param singlePosition the single position */ public MtasExtendedSpanTermQuery(SpanTermQuery query, boolean singlePosition) { <span class="fc" id="L70"> super(query.getTerm());</span> <span class="fc" id="L71"> localTerm = query.getTerm();</span> <span class="fc" id="L72"> this.singlePosition = singlePosition;</span> <span class="fc" id="L73"> int i = localTerm.text().indexOf(MtasToken.DELIMITER);</span> <span class="pc bpc" id="L74" title="1 of 2 branches missed."> if (i >= 0) {</span> <span class="fc" id="L75"> prefix = localTerm.text().substring(0, i);</span> <span class="fc" id="L76"> value = localTerm.text().substring((i + MtasToken.DELIMITER.length()));</span> <span class="fc bfc" id="L77" title="All 2 branches covered."> value = (value.length() > 0) ? value : null;</span> } else { <span class="nc" id="L79"> prefix = localTerm.text();</span> <span class="nc" id="L80"> value = null;</span> } <span class="fc" id="L82"> }</span> /* * (non-Javadoc) * * @see * org.apache.lucene.search.spans.SpanTermQuery#createWeight(org.apache.lucene * .search.IndexSearcher, boolean) */ @Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { final TermContext context; <span class="fc" id="L95"> final IndexReaderContext topContext = searcher.getTopReaderContext();</span> <span class="pc bpc" id="L96" title="1 of 2 branches missed."> if (termContext == null) {</span> <span class="fc" id="L97"> context = TermContext.build(topContext, localTerm);</span> } else { <span class="nc" id="L99"> context = termContext;</span> } <span class="pc bpc" id="L101" title="1 of 2 branches missed."> return new SpanTermWeight(context, searcher,</span> <span class="pc" id="L102"> needsScores ? Collections.singletonMap(localTerm, context) : null);</span> } /** * The Class SpanTermWeight. */ <span class="pc bpc" id="L108" title="1 of 2 branches missed."> public class SpanTermWeight extends SpanWeight {</span> /** The Constant METHOD_GET_DELEGATE. */ private static final String METHOD_GET_DELEGATE = "getDelegate"; /** The term context. */ final TermContext termContext; /** * Instantiates a new span term weight. * * @param termContext the term context * @param searcher the searcher * @param terms the terms * @throws IOException Signals that an I/O exception has occurred. */ public SpanTermWeight(TermContext termContext, IndexSearcher searcher, <span class="fc" id="L125"> Map<Term, TermContext> terms) throws IOException {</span> <span class="fc" id="L126"> super(MtasExtendedSpanTermQuery.this, searcher, terms);</span> <span class="fc" id="L127"> this.termContext = termContext;</span> <span class="pc bpc" id="L128" title="2 of 4 branches missed."> assert termContext != null : "TermContext must not be null";</span> <span class="fc" id="L129"> }</span> /* * (non-Javadoc) * * @see org.apache.lucene.search.Weight#extractTerms(java.util.Set) */ @Override public void extractTerms(Set<Term> terms) { <span class="nc" id="L138"> terms.add(localTerm);</span> <span class="nc" id="L139"> }</span> /* * (non-Javadoc) * * @see * org.apache.lucene.search.spans.SpanWeight#extractTermContexts(java.util * .Map) */ @Override public void extractTermContexts(Map<Term, TermContext> contexts) { <span class="fc" id="L150"> contexts.put(localTerm, termContext);</span> <span class="fc" id="L151"> }</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 Spans getSpans(final LeafReaderContext context, Postings requiredPostings) throws IOException { <span class="fc" id="L164"> final TermState state = termContext.get(context.ord);</span> <span class="fc bfc" id="L165" title="All 2 branches covered."> if (state == null) { // term is not present in that reader</span> <span class="pc bpc" id="L166" title="2 of 4 branches missed."> assert context.reader().docFreq(</span> <span class="fc" id="L167"> localTerm) == 0 : "no termstate found but term exists in reader term="</span> <span class="nc" id="L168"> + localTerm;</span> <span class="fc" id="L169"> return null;</span> } <span class="fc" id="L172"> final Terms terms = context.reader().terms(localTerm.field());</span> <span class="pc bpc" id="L173" title="1 of 2 branches missed."> if (terms == null) {</span> <span class="nc" id="L174"> return null;</span> } <span class="pc bpc" id="L176" title="1 of 2 branches missed."> if (!terms.hasPositions())</span> <span class="nc" id="L177"> throw new IllegalStateException("field \"" + localTerm.field()</span> + "\" was indexed without position data; cannot run SpanTermQuery (term=" <span class="nc" id="L179"> + localTerm.text() + ")");</span> <span class="fc" id="L181"> final TermsEnum termsEnum = terms.iterator();</span> <span class="fc" id="L182"> termsEnum.seekExact(localTerm.bytes(), state);</span> final PostingsEnum postings; Spans matchSpans; try { // get leafreader <span class="fc" id="L189"> LeafReader r = context.reader();</span> // get delegate <span class="fc" id="L192"> Boolean hasMethod = true;</span> <span class="fc bfc" id="L193" title="All 2 branches covered."> while (hasMethod) {</span> <span class="fc" id="L194"> hasMethod = false;</span> <span class="fc" id="L195"> Method[] methods = r.getClass().getMethods();</span> <span class="fc bfc" id="L196" title="All 2 branches covered."> for (Method m : methods) {</span> <span class="fc bfc" id="L197" title="All 2 branches covered."> if (m.getName().equals(METHOD_GET_DELEGATE)) {</span> <span class="fc" id="L198"> hasMethod = true;</span> <span class="fc" id="L199"> r = (LeafReader) m.invoke(r, (Object[]) null);</span> <span class="fc" id="L200"> break;</span> } } <span class="fc" id="L203"> }</span> <span class="fc" id="L205"> FieldInfo fieldInfo = r.getFieldInfos().fieldInfo(field);</span> <span class="fc bfc" id="L207" title="All 2 branches covered."> if (CodecUtil.isSinglePositionPrefix(fieldInfo, prefix)) {</span> <span class="fc" id="L208"> postings = termsEnum.postings(null,</span> <span class="fc" id="L209"> requiredPostings.getRequiredPostings());</span> <span class="fc" id="L210"> matchSpans = new MtasExtendedTermSpans(postings, localTerm, true);</span> } else { <span class="fc" id="L212"> postings = termsEnum.postings(null, requiredPostings</span> <span class="fc" id="L213"> .atLeast(Postings.PAYLOADS).getRequiredPostings());</span> <span class="fc" id="L214"> matchSpans = new MtasExtendedTermSpans(postings, localTerm, false);</span> } <span class="fc bfc" id="L216" title="All 2 branches covered."> if (singlePosition) {</span> <span class="pc bpc" id="L217" title="1 of 2 branches missed."> return new FilterSpans(matchSpans) {</span> @Override protected AcceptStatus accept(Spans candidate) throws IOException { <span class="pc bpc" id="L220" title="2 of 4 branches missed."> assert candidate.startPosition() != candidate.endPosition();</span> <span class="pc bpc" id="L221" title="1 of 2 branches missed."> if ((candidate.endPosition() - candidate.startPosition()) == 1) {</span> <span class="fc" id="L222"> return AcceptStatus.YES;</span> } else { <span class="nc" id="L224"> return AcceptStatus.NO;</span> } } }; } else { <span class="fc" id="L229"> return matchSpans;</span> } <span class="nc" id="L231"> } catch (Exception e) {</span> // e.printStackTrace(); <span class="nc" id="L233"> throw new IOException("Can't get reader: " + e.getMessage(), e);</span> } } } /* * (non-Javadoc) * * @see * org.apache.lucene.search.spans.SpanTermQuery#toString(java.lang.String) */ @Override public String toString(String field) { <span class="nc" id="L247"> StringBuilder buffer = new StringBuilder();</span> <span class="nc" id="L248"> buffer.append(this.getClass().getSimpleName() + "([");</span> <span class="nc bnc" id="L249" title="All 2 branches missed."> if (value == null) {</span> <span class="nc" id="L250"> buffer.append(field + ":" + prefix);</span> } else { <span class="nc" id="L252"> buffer.append(field + ":" + prefix + "=" + value);</span> } <span class="nc" id="L254"> buffer.append("])");</span> <span class="nc" id="L255"> return buffer.toString();</span> } /* * (non-Javadoc) * * @see org.apache.lucene.search.spans.SpanTermQuery#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { <span class="pc bpc" id="L265" title="1 of 2 branches missed."> if (this == obj)</span> <span class="nc" id="L266"> return true;</span> <span class="pc bpc" id="L267" title="1 of 2 branches missed."> if (obj == null)</span> <span class="nc" id="L268"> return false;</span> <span class="pc bpc" id="L269" title="1 of 2 branches missed."> if (getClass() != obj.getClass())</span> <span class="nc" id="L270"> return false;</span> <span class="fc" id="L271"> MtasExtendedSpanTermQuery other = (MtasExtendedSpanTermQuery) obj;</span> <span class="pc bpc" id="L272" title="1 of 4 branches missed."> return other.localTerm.equals(localTerm)</span> && (other.singlePosition == singlePosition); } /* * (non-Javadoc) * * @see org.apache.lucene.search.spans.SpanTermQuery#hashCode() */ @Override public int hashCode() { <span class="fc" id="L283"> int h = this.getClass().getSimpleName().hashCode();</span> <span class="fc" id="L284"> h = (h * 5) ^ localTerm.hashCode();</span> <span class="fc bfc" id="L285" title="All 2 branches covered."> if (singlePosition) {</span> <span class="fc" id="L286"> h += 1;</span> } <span class="fc" id="L288"> 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>