MtasSpanFullyAlignedWithQuery.java.html
15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?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>MtasSpanFullyAlignedWithQuery.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</a> > <span class="el_source">MtasSpanFullyAlignedWithQuery.java</span></div><h1>MtasSpanFullyAlignedWithQuery.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.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 MtasSpanFullyAlignedWithQuery.
*/
public class MtasSpanFullyAlignedWithQuery extends MtasSpanQuery {
/** The field. */
private String field;
/** The q 1. */
private MtasSpanQuery q1;
/** The q 2. */
private MtasSpanQuery q2;
/**
* Instantiates a new mtas span fully aligned with query.
*
* @param q1 the q 1
* @param q2 the q 2
*/
public MtasSpanFullyAlignedWithQuery(MtasSpanQuery q1, MtasSpanQuery q2) {
<span class="pc bpc" id="L42" title="2 of 4 branches missed."> super(q1 != null ? q1.getMinimumWidth() : null,</span>
<span class="pc" id="L43"> q1 != null ? q1.getMaximumWidth() : null);</span>
<span class="pc bpc" id="L44" title="2 of 4 branches missed."> if (q1 != null && (field = q1.getField()) != null) {</span>
<span class="pc bpc" id="L45" title="3 of 6 branches missed."> if (q2 != null && q2.getField() != null && !q2.getField().equals(field)) {</span>
<span class="nc" id="L46"> throw new IllegalArgumentException("Clauses must have same field.");</span>
}
<span class="nc bnc" id="L48" title="All 2 branches missed."> } else if (q2 != null) {</span>
<span class="nc" id="L49"> field = q2.getField();</span>
} else {
<span class="nc" id="L51"> field = null;</span>
}
<span class="fc" id="L53"> this.q1 = q1;</span>
<span class="fc" id="L54"> this.q2 = q2;</span>
<span class="fc" id="L55"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanQuery#getField()
*/
@Override
public String getField() {
<span class="fc" id="L64"> return field;</span>
}
/*
* (non-Javadoc)
*
* @see
* org.apache.lucene.search.spans.SpanQuery#createWeight(org.apache.lucene.
* search.IndexSearcher, boolean)
*/
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
boolean needsScores, float boost) throws IOException {
<span class="pc bpc" id="L77" title="2 of 4 branches missed."> if (q1 == null || q2 == null) {</span>
<span class="nc" id="L78"> return null;</span>
} else {
<span class="fc" id="L80"> MtasSpanFullyAlignedWithQueryWeight w1 = new MtasSpanFullyAlignedWithQueryWeight(</span>
<span class="fc" id="L81"> q1.createWeight(searcher, needsScores, boost));</span>
<span class="fc" id="L82"> MtasSpanFullyAlignedWithQueryWeight w2 = new MtasSpanFullyAlignedWithQueryWeight(</span>
<span class="fc" id="L83"> q2.createWeight(searcher, needsScores, boost));</span>
// subWeights
<span class="fc" id="L85"> List<MtasSpanFullyAlignedWithQueryWeight> subWeights = new ArrayList<>();</span>
<span class="fc" id="L86"> subWeights.add(w1);</span>
<span class="fc" id="L87"> subWeights.add(w2);</span>
// return
<span class="pc bpc" id="L89" title="1 of 2 branches missed."> return new SpanFullyAlignedWithWeight(w1, w2, searcher,</span>
<span class="pc" id="L90"> needsScores ? getTermContexts(subWeights) : null, boost);</span>
}
}
/**
* Gets the term contexts.
*
* @param items the items
* @return the term contexts
*/
protected Map<Term, TermContext> getTermContexts(
List<MtasSpanFullyAlignedWithQueryWeight> items) {
<span class="nc" id="L102"> List<SpanWeight> weights = new ArrayList<>();</span>
<span class="nc bnc" id="L103" title="All 2 branches missed."> for (MtasSpanFullyAlignedWithQueryWeight item : items) {</span>
<span class="nc" id="L104"> weights.add(item.spanWeight);</span>
<span class="nc" id="L105"> }</span>
<span class="nc" id="L106"> return getTermContexts(weights);</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Query#toString(java.lang.String)
*/
@Override
public String toString(String field) {
<span class="nc" id="L116"> StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L117"> buffer.append(this.getClass().getSimpleName() + "([");</span>
<span class="nc bnc" id="L118" title="All 2 branches missed."> if (q1 != null) {</span>
<span class="nc" id="L119"> buffer.append(q1.toString(q1.getField()));</span>
} else {
<span class="nc" id="L121"> buffer.append("null");</span>
}
<span class="nc" id="L123"> buffer.append(",");</span>
<span class="nc bnc" id="L124" title="All 2 branches missed."> if (q2 != null) {</span>
<span class="nc" id="L125"> buffer.append(q2.toString(q2.getField()));</span>
} else {
<span class="nc" id="L127"> buffer.append("null");</span>
}
<span class="nc" id="L129"> buffer.append("])");</span>
<span class="nc" id="L130"> 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="L140" title="All 2 branches covered."> if (this == obj)</span>
<span class="fc" id="L141"> return true;</span>
<span class="pc bpc" id="L142" title="1 of 2 branches missed."> if (obj == null)</span>
<span class="nc" id="L143"> return false;</span>
<span class="pc bpc" id="L144" title="1 of 2 branches missed."> if (getClass() != obj.getClass())</span>
<span class="nc" id="L145"> return false;</span>
<span class="fc" id="L146"> final MtasSpanFullyAlignedWithQuery other = (MtasSpanFullyAlignedWithQuery) obj;</span>
<span class="fc bfc" id="L147" title="All 4 branches covered."> return q1.equals(other.q1) && q2.equals(other.q2);</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Query#hashCode()
*/
@Override
public int hashCode() {
<span class="nc" id="L157"> int h = Integer.rotateLeft(classHash(), 1);</span>
<span class="nc" id="L158"> h ^= q1.hashCode();</span>
<span class="nc" id="L159"> h = Integer.rotateLeft(h, 1);</span>
<span class="nc" id="L160"> h ^= q2.hashCode();</span>
<span class="nc" id="L161"> return h;</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="L172"> MtasSpanQuery newQ1 = (MtasSpanQuery) q1.rewrite(reader);</span>
<span class="fc" id="L173"> MtasSpanQuery newQ2 = (MtasSpanQuery) q2.rewrite(reader);</span>
<span class="pc bpc" id="L174" title="4 of 8 branches missed."> if (newQ1 == null || newQ1 instanceof MtasSpanMatchNoneQuery</span>
|| newQ2 == null || newQ2 instanceof MtasSpanMatchNoneQuery) {
<span class="nc" id="L176"> return new MtasSpanMatchNoneQuery(field);</span>
<span class="fc bfc" id="L177" title="All 4 branches covered."> } else if (!newQ1.equals(q1) || !newQ2.equals(q2)) {</span>
<span class="fc" id="L178"> return new MtasSpanFullyAlignedWithQuery(newQ1, newQ2).rewrite(reader);</span>
<span class="fc bfc" id="L179" title="All 2 branches covered."> } else if (newQ1.equals(newQ2)) {</span>
<span class="fc" id="L180"> return newQ1;</span>
} else {
boolean returnNone;
<span class="pc bpc" id="L183" title="1 of 2 branches missed."> returnNone = newQ1.getMaximumWidth() != null</span>
<span class="pc bpc" id="L184" title="1 of 2 branches missed."> && newQ1.getMaximumWidth() == 0;</span>
<span class="pc bpc" id="L185" title="1 of 2 branches missed."> returnNone |= newQ2.getMaximumWidth() != null</span>
<span class="pc bpc" id="L186" title="1 of 2 branches missed."> && newQ2.getMaximumWidth() == 0;</span>
<span class="pc bpc" id="L187" title="1 of 2 branches missed."> returnNone |= newQ1.getMinimumWidth() != null</span>
<span class="pc bpc" id="L188" title="1 of 2 branches missed."> && newQ2.getMaximumWidth() != null</span>
<span class="pc bpc" id="L189" title="1 of 2 branches missed."> && newQ1.getMinimumWidth() > newQ2.getMaximumWidth();</span>
<span class="pc bpc" id="L190" title="1 of 2 branches missed."> returnNone |= newQ2.getMinimumWidth() != null</span>
<span class="pc bpc" id="L191" title="1 of 2 branches missed."> && newQ1.getMaximumWidth() != null</span>
<span class="fc bfc" id="L192" title="All 2 branches covered."> && newQ2.getMinimumWidth() > newQ1.getMaximumWidth();</span>
<span class="pc bpc" id="L193" title="1 of 2 branches missed."> returnNone |= newQ2.getMinimumWidth() != null</span>
<span class="pc bpc" id="L194" title="1 of 2 branches missed."> && newQ1.getMaximumWidth() != null</span>
<span class="fc bfc" id="L195" title="All 2 branches covered."> && newQ2.getMinimumWidth() > newQ1.getMaximumWidth();</span>
<span class="fc bfc" id="L196" title="All 2 branches covered."> if (returnNone) {</span>
<span class="fc" id="L197"> return new MtasSpanMatchNoneQuery(this.getField());</span>
} else {
<span class="fc" id="L199"> return super.rewrite(reader);</span>
}
}
}
/*
* (non-Javadoc)
*
* @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
*/
@Override
public void disableTwoPhaseIterator() {
<span class="fc" id="L211"> super.disableTwoPhaseIterator();</span>
<span class="fc" id="L212"> q1.disableTwoPhaseIterator();</span>
<span class="fc" id="L213"> q2.disableTwoPhaseIterator();</span>
<span class="fc" id="L214"> }</span>
/**
* The Class SpanFullyAlignedWithWeight.
*/
protected class SpanFullyAlignedWithWeight extends MtasSpanWeight {
/** The w 1. */
MtasSpanFullyAlignedWithQueryWeight w1;
/** The w 2. */
MtasSpanFullyAlignedWithQueryWeight w2;
/**
* Instantiates a new span fully aligned with weight.
*
* @param w1 the w 1
* @param w2 the w 2
* @param searcher the searcher
* @param terms the terms
* @throws IOException Signals that an I/O exception has occurred.
*/
public SpanFullyAlignedWithWeight(MtasSpanFullyAlignedWithQueryWeight w1,
MtasSpanFullyAlignedWithQueryWeight w2, IndexSearcher searcher,
<span class="fc" id="L238"> Map<Term, TermContext> terms, float boost) throws IOException {</span>
<span class="fc" id="L239"> super(MtasSpanFullyAlignedWithQuery.this, searcher, terms, boost);</span>
<span class="fc" id="L240"> this.w1 = w1;</span>
<span class="fc" id="L241"> this.w2 = w2;</span>
<span class="fc" id="L242"> }</span>
/*
* (non-Javadoc)
*
* @see
* org.apache.lucene.search.spans.SpanWeight#extractTermContexts(java.util.
* Map)
*/
@Override
public void extractTermContexts(Map<Term, TermContext> contexts) {
<span class="nc" id="L253"> w1.spanWeight.extractTermContexts(contexts);</span>
<span class="nc" id="L254"> w2.spanWeight.extractTermContexts(contexts);</span>
<span class="nc" id="L255"> }</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="L268"> Terms terms = context.reader().terms(field);</span>
<span class="pc bpc" id="L269" title="1 of 2 branches missed."> if (terms == null) {</span>
<span class="nc" id="L270"> return null; // field does not exist</span>
}
<span class="fc" id="L272"> MtasSpanFullyAlignedWithQuerySpans s1 = new MtasSpanFullyAlignedWithQuerySpans(</span>
MtasSpanFullyAlignedWithQuery.this,
<span class="fc" id="L274"> w1.spanWeight.getSpans(context, requiredPostings));</span>
<span class="fc" id="L275"> MtasSpanFullyAlignedWithQuerySpans s2 = new MtasSpanFullyAlignedWithQuerySpans(</span>
MtasSpanFullyAlignedWithQuery.this,
<span class="fc" id="L277"> w2.spanWeight.getSpans(context, requiredPostings));</span>
<span class="fc" id="L278"> return new MtasSpanFullyAlignedWithSpans(</span>
MtasSpanFullyAlignedWithQuery.this, s1, s2);
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Weight#extractTerms(java.util.Set)
*/
@Override
public void extractTerms(Set<Term> terms) {
<span class="nc" id="L289"> w1.spanWeight.extractTerms(terms);</span>
<span class="nc" id="L290"> w2.spanWeight.extractTerms(terms);</span>
<span class="nc" id="L291"> }</span>
}
/**
* The Class MtasSpanFullyAlignedWithQuerySpans.
*/
protected static class MtasSpanFullyAlignedWithQuerySpans {
/** The spans. */
public Spans spans;
/**
* Instantiates a new mtas span fully aligned with query spans.
*
* @param query the query
* @param spans the spans
*/
public MtasSpanFullyAlignedWithQuerySpans(
<span class="fc" id="L310"> MtasSpanFullyAlignedWithQuery query, Spans spans) {</span>
<span class="pc bpc" id="L311" title="1 of 2 branches missed."> this.spans = spans != null ? spans : new MtasSpanMatchNoneSpans(query);</span>
<span class="fc" id="L312"> }</span>
}
/**
* The Class MtasSpanFullyAlignedWithQueryWeight.
*/
private static class MtasSpanFullyAlignedWithQueryWeight {
/** The span weight. */
public SpanWeight spanWeight;
/**
* Instantiates a new mtas span fully aligned with query weight.
*
* @param spanWeight the span weight
*/
<span class="fc" id="L329"> public MtasSpanFullyAlignedWithQueryWeight(SpanWeight spanWeight) {</span>
<span class="fc" id="L330"> this.spanWeight = spanWeight;</span>
<span class="fc" id="L331"> }</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>