MtasSpanSequenceQuery.java.html
21.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?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>MtasSpanSequenceQuery.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">MtasSpanSequenceQuery.java</span></div><h1>MtasSpanSequenceQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
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.MtasIgnoreItem;
import mtas.search.spans.util.MtasSpanQuery;
/**
* The Class MtasSpanSequenceQuery.
*/
public class MtasSpanSequenceQuery extends MtasSpanQuery {
/** The items. */
private List<MtasSpanSequenceItem> items;
/** The ignore query. */
private MtasSpanQuery ignoreQuery;
/** The maximum ignore length. */
private Integer maximumIgnoreLength;
/** The field. */
private String field;
/**
* Instantiates a new mtas span sequence query.
*
* @param items the items
* @param ignoreQuery the ignore query
* @param maximumIgnoreLength the maximum ignore length
*/
public MtasSpanSequenceQuery(List<MtasSpanSequenceItem> items,
MtasSpanQuery ignoreQuery, Integer maximumIgnoreLength) {
<span class="fc" id="L48"> super(null, null);</span>
<span class="fc" id="L49"> Integer minimum = 0;</span>
<span class="fc" id="L50"> Integer maximum = 0;</span>
<span class="fc" id="L51"> this.items = items;</span>
// get field and do checks
<span class="fc bfc" id="L53" title="All 2 branches covered."> for (MtasSpanSequenceItem item : items) {</span>
<span class="fc bfc" id="L54" title="All 2 branches covered."> if (field == null) {</span>
<span class="fc" id="L55"> field = item.getQuery().getField();</span>
<span class="pc bpc" id="L56" title="1 of 2 branches missed."> } else if (item.getQuery().getField() != null</span>
<span class="pc bpc" id="L57" title="1 of 2 branches missed."> && !item.getQuery().getField().equals(field)) {</span>
<span class="nc" id="L58"> throw new IllegalArgumentException("Clauses must have same field.");</span>
}
<span class="fc bfc" id="L60" title="All 4 branches covered."> if (minimum != null && !item.isOptional()) {</span>
<span class="fc bfc" id="L61" title="All 2 branches covered."> minimum = item.getQuery().getMinimumWidth() != null</span>
<span class="fc" id="L62"> ? minimum + item.getQuery().getMinimumWidth() : null;</span>
}
<span class="fc bfc" id="L64" title="All 2 branches covered."> if (maximum != null) {</span>
<span class="fc bfc" id="L65" title="All 2 branches covered."> maximum = item.getQuery().getMaximumWidth() != null</span>
<span class="fc" id="L66"> ? maximum + item.getQuery().getMaximumWidth() : null;</span>
}
<span class="fc" id="L68"> }</span>
// check ignore
<span class="pc bpc" id="L70" title="1 of 4 branches missed."> if (field != null && ignoreQuery != null) {</span>
<span class="pc bpc" id="L71" title="1 of 2 branches missed."> if (ignoreQuery.getField() == null</span>
<span class="pc bpc" id="L72" title="1 of 2 branches missed."> || field.equals(ignoreQuery.getField())) {</span>
<span class="fc" id="L73"> this.ignoreQuery = ignoreQuery;</span>
<span class="pc bpc" id="L74" title="1 of 2 branches missed."> if (maximumIgnoreLength == null) {</span>
<span class="nc" id="L75"> this.maximumIgnoreLength = MtasIgnoreItem.DEFAULT_MAXIMUM_IGNORE_LENGTH;</span>
} else {
<span class="fc" id="L77"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
}
} else {
<span class="nc" id="L80"> throw new IllegalArgumentException(</span>
"ignore must have same field as clauses");
}
<span class="pc bpc" id="L83" title="2 of 4 branches missed."> if (maximum != null && items.size() > 1) {</span>
<span class="pc bpc" id="L84" title="1 of 2 branches missed."> if (this.ignoreQuery.getMaximumWidth() != null) {</span>
<span class="fc" id="L85"> maximum += (items.size() - 1) * this.maximumIgnoreLength</span>
<span class="fc" id="L86"> * this.ignoreQuery.getMaximumWidth();</span>
} else {
<span class="nc" id="L88"> maximum = null;</span>
}
}
} else {
<span class="fc" id="L92"> this.ignoreQuery = null;</span>
<span class="fc" id="L93"> this.maximumIgnoreLength = null;</span>
}
<span class="fc" id="L95"> setWidth(minimum, maximum);</span>
<span class="fc" id="L96"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanQuery#getField()
*/
@Override
public String getField() {
<span class="fc" id="L105"> return field;</span>
}
/**
* Gets the items.
*
* @return the items
*/
public List<MtasSpanSequenceItem> getItems() {
<span class="nc" id="L114"> return items;</span>
}
/**
* Gets the ignore query.
*
* @return the ignore query
*/
public MtasSpanQuery getIgnoreQuery() {
<span class="nc" id="L123"> return ignoreQuery;</span>
}
/**
* Gets the maximum ignore length.
*
* @return the maximum ignore length
*/
public Integer getMaximumIgnoreLength() {
<span class="nc" id="L132"> return maximumIgnoreLength;</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="pc bpc" id="L143" title="1 of 2 branches missed."> if (items.size() == 1) {</span>
<span class="nc" id="L144"> return items.get(0).getQuery().rewrite(reader);</span>
} else {
MtasSpanSequenceItem newItem;
<span class="fc" id="L147"> MtasSpanSequenceItem previousNewItem = null;</span>
<span class="fc" id="L148"> ArrayList<MtasSpanSequenceItem> newItems = new ArrayList<>(items.size());</span>
<span class="fc bfc" id="L149" title="All 2 branches covered."> MtasSpanQuery newIgnoreClause = ignoreQuery != null</span>
<span class="fc" id="L150"> ? ignoreQuery.rewrite(reader) : null;</span>
<span class="fc bfc" id="L151" title="All 2 branches covered."> boolean actuallyRewritten = ignoreQuery != null</span>
<span class="fc bfc" id="L152" title="All 2 branches covered."> ? !newIgnoreClause.equals(ignoreQuery) : false;</span>
<span class="fc bfc" id="L153" title="All 2 branches covered."> for (int i = 0; i < items.size(); i++) {</span>
<span class="fc" id="L154"> newItem = items.get(i).rewrite(reader);</span>
<span class="pc bpc" id="L155" title="1 of 2 branches missed."> if (newItem.getQuery() instanceof MtasSpanMatchNoneQuery) {</span>
<span class="nc bnc" id="L156" title="All 2 branches missed."> if (!newItem.isOptional()) {</span>
<span class="nc" id="L157"> return new MtasSpanMatchNoneQuery(field);</span>
} else {
<span class="nc" id="L159"> actuallyRewritten = true;</span>
}
} else {
<span class="fc bfc" id="L162" title="All 2 branches covered."> actuallyRewritten |= !items.get(i).equals(newItem);</span>
<span class="fc" id="L163"> MtasSpanSequenceItem previousMergedItem = MtasSpanSequenceItem.merge(</span>
previousNewItem, newItem, ignoreQuery, maximumIgnoreLength);
<span class="pc bpc" id="L165" title="1 of 2 branches missed."> if (previousMergedItem != null) {</span>
<span class="nc" id="L166"> newItems.set((newItems.size() - 1), previousMergedItem);</span>
<span class="nc" id="L167"> actuallyRewritten = true;</span>
} else {
<span class="fc" id="L169"> newItems.add(newItem);</span>
}
<span class="fc" id="L171"> previousNewItem = newItem;</span>
}
}
<span class="fc bfc" id="L174" title="All 2 branches covered."> if (!actuallyRewritten) {</span>
<span class="fc" id="L175"> return super.rewrite(reader);</span>
} else {
<span class="pc bpc" id="L177" title="1 of 2 branches missed."> if (!newItems.isEmpty()) {</span>
<span class="fc" id="L178"> return new MtasSpanSequenceQuery(newItems, newIgnoreClause,</span>
<span class="fc" id="L179"> maximumIgnoreLength).rewrite(reader);</span>
} else {
<span class="nc" id="L181"> return new MtasSpanMatchNoneQuery(field);</span>
}
}
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Query#toString(java.lang.String)
*/
@Override
public String toString(String field) {
<span class="nc" id="L194"> StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L195"> buffer.append(this.getClass().getSimpleName() + "([");</span>
<span class="nc" id="L196"> Iterator<MtasSpanSequenceItem> i = items.iterator();</span>
<span class="nc bnc" id="L197" title="All 2 branches missed."> while (i.hasNext()) {</span>
<span class="nc" id="L198"> MtasSpanSequenceItem item = i.next();</span>
<span class="nc" id="L199"> MtasSpanQuery clause = item.getQuery();</span>
<span class="nc" id="L200"> buffer.append(clause.toString(field));</span>
<span class="nc bnc" id="L201" title="All 2 branches missed."> if (item.isOptional()) {</span>
<span class="nc" id="L202"> buffer.append("{OPTIONAL}");</span>
}
<span class="nc bnc" id="L204" title="All 2 branches missed."> if (i.hasNext()) {</span>
<span class="nc" id="L205"> buffer.append(", ");</span>
}
<span class="nc" id="L207"> }</span>
<span class="nc" id="L208"> buffer.append("]");</span>
<span class="nc" id="L209"> buffer.append(", ");</span>
<span class="nc" id="L210"> buffer.append(ignoreQuery);</span>
<span class="nc" id="L211"> buffer.append(")");</span>
<span class="nc" id="L212"> 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="L222" title="All 2 branches covered."> if (this == obj)</span>
<span class="fc" id="L223"> return true;</span>
<span class="pc bpc" id="L224" title="1 of 2 branches missed."> if (obj == null)</span>
<span class="nc" id="L225"> return false;</span>
<span class="pc bpc" id="L226" title="1 of 2 branches missed."> if (getClass() != obj.getClass())</span>
<span class="nc" id="L227"> return false;</span>
<span class="fc" id="L228"> MtasSpanSequenceQuery other = (MtasSpanSequenceQuery) obj;</span>
<span class="pc bpc" id="L229" title="7 of 12 branches missed."> return field.equals(other.field) && items.equals(other.items)</span>
&& ((ignoreQuery == null && other.ignoreQuery == null)
|| ignoreQuery != null && other.ignoreQuery != null
<span class="pc bnc" id="L232" title="All 2 branches missed."> && ignoreQuery.equals(other.ignoreQuery));</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Query#hashCode()
*/
@Override
public int hashCode() {
<span class="fc" id="L242"> int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L243"> h = (h * 3) ^ field.hashCode();</span>
<span class="fc" id="L244"> h = (h * 5) ^ items.hashCode();</span>
<span class="pc bpc" id="L245" title="1 of 2 branches missed."> if (ignoreQuery != null) {</span>
<span class="nc" id="L246"> h = (h * 7) ^ ignoreQuery.hashCode();</span>
<span class="nc" id="L247"> h = (h * 11) ^ maximumIgnoreLength.hashCode();</span>
}
<span class="fc" id="L249"> return h;</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="fc" id="L262"> List<MtasSpanSequenceQueryWeight> subWeights = new ArrayList<>();</span>
<span class="fc" id="L263"> SpanWeight ignoreWeight = null;</span>
<span class="fc bfc" id="L264" title="All 2 branches covered."> for (MtasSpanSequenceItem item : items) {</span>
<span class="fc" id="L265"> subWeights.add(new MtasSpanSequenceQueryWeight(</span>
<span class="fc" id="L266"> item.getQuery().createWeight(searcher, false), item.isOptional()));</span>
<span class="fc" id="L267"> }</span>
<span class="fc bfc" id="L268" title="All 2 branches covered."> if (ignoreQuery != null) {</span>
<span class="fc" id="L269"> ignoreWeight = ignoreQuery.createWeight(searcher, false);</span>
}
<span class="pc bpc" id="L271" title="1 of 2 branches missed."> return new SpanSequenceWeight(subWeights, ignoreWeight, maximumIgnoreLength,</span>
<span class="pc" id="L272"> searcher, needsScores ? getTermContexts(subWeights) : null);</span>
}
/**
* Gets the term contexts.
*
* @param items the items
* @return the term contexts
*/
protected Map<Term, TermContext> getTermContexts(
List<MtasSpanSequenceQueryWeight> items) {
<span class="nc" id="L283"> List<SpanWeight> weights = new ArrayList<>();</span>
<span class="nc bnc" id="L284" title="All 2 branches missed."> for (MtasSpanSequenceQueryWeight item : items) {</span>
<span class="nc" id="L285"> weights.add(item.spanWeight);</span>
<span class="nc" id="L286"> }</span>
<span class="nc" id="L287"> return getTermContexts(weights);</span>
}
/**
* The Class SpanSequenceWeight.
*/
protected class SpanSequenceWeight extends SpanWeight {
/** The sub weights. */
final List<MtasSpanSequenceQueryWeight> subWeights;
/** The ignore weight. */
final SpanWeight ignoreWeight;
/** The maximum ignore length. */
final Integer maximumIgnoreLength;
/**
* Instantiates a new span sequence weight.
*
* @param subWeights the sub weights
* @param ignoreWeight the ignore weight
* @param maximumIgnoreLength the maximum ignore length
* @param searcher the searcher
* @param terms the terms
* @throws IOException Signals that an I/O exception has occurred.
*/
public SpanSequenceWeight(List<MtasSpanSequenceQueryWeight> subWeights,
SpanWeight ignoreWeight, Integer maximumIgnoreLength,
IndexSearcher searcher, Map<Term, TermContext> terms)
<span class="fc" id="L317"> throws IOException {</span>
<span class="fc" id="L318"> super(MtasSpanSequenceQuery.this, searcher, terms);</span>
<span class="fc" id="L319"> this.subWeights = subWeights;</span>
<span class="fc" id="L320"> this.ignoreWeight = ignoreWeight;</span>
<span class="fc" id="L321"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
<span class="fc" id="L322"> }</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 bfc" id="L333" title="All 2 branches covered."> for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="fc" id="L334"> w.spanWeight.extractTermContexts(contexts);</span>
<span class="fc" id="L335"> }</span>
<span class="pc bpc" id="L336" title="1 of 2 branches missed."> if (ignoreWeight != null) {</span>
<span class="nc" id="L337"> ignoreWeight.extractTermContexts(contexts);</span>
}
<span class="fc" id="L339"> }</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(LeafReaderContext context, Postings requiredPostings)
throws IOException {
<span class="pc bpc" id="L352" title="1 of 2 branches missed."> if (field == null) {</span>
<span class="nc" id="L353"> return null;</span>
} else {
<span class="fc" id="L355"> Terms terms = context.reader().terms(field);</span>
<span class="pc bpc" id="L356" title="1 of 2 branches missed."> if (terms == null) {</span>
<span class="nc" id="L357"> return null; // field does not exist</span>
}
<span class="fc" id="L359"> List<MtasSpanSequenceQuerySpans> setSequenceSpans = new ArrayList<>(</span>
<span class="fc" id="L360"> items.size());</span>
<span class="fc" id="L361"> Spans ignoreSpans = null;</span>
<span class="fc" id="L362"> boolean allSpansEmpty = true;</span>
<span class="fc bfc" id="L363" title="All 2 branches covered."> for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="fc" id="L364"> Spans sequenceSpans = w.spanWeight.getSpans(context,</span>
requiredPostings);
<span class="pc bpc" id="L366" title="1 of 2 branches missed."> if (sequenceSpans != null) {</span>
<span class="fc" id="L367"> setSequenceSpans</span>
<span class="fc" id="L368"> .add(new MtasSpanSequenceQuerySpans(sequenceSpans, w.optional));</span>
<span class="fc" id="L369"> allSpansEmpty = false;</span>
} else {
<span class="nc bnc" id="L371" title="All 2 branches missed."> if (w.optional) {</span>
<span class="nc" id="L372"> setSequenceSpans</span>
<span class="nc" id="L373"> .add(new MtasSpanSequenceQuerySpans(null, w.optional));</span>
} else {
<span class="nc" id="L375"> return null;</span>
}
}
<span class="fc" id="L378"> }</span>
<span class="pc bpc" id="L379" title="1 of 2 branches missed."> if (allSpansEmpty) {</span>
<span class="nc" id="L380"> return null; // at least one required</span>
<span class="fc bfc" id="L381" title="All 2 branches covered."> } else if (ignoreWeight != null) {</span>
<span class="fc" id="L382"> ignoreSpans = ignoreWeight.getSpans(context, requiredPostings);</span>
}
<span class="fc" id="L384"> return new MtasSpanSequenceSpans(setSequenceSpans, ignoreSpans,</span>
maximumIgnoreLength);
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Weight#extractTerms(java.util.Set)
*/
@Override
public void extractTerms(Set<Term> terms) {
<span class="nc bnc" id="L396" title="All 2 branches missed."> for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="nc" id="L397"> w.spanWeight.extractTerms(terms);</span>
<span class="nc" id="L398"> }</span>
<span class="nc bnc" id="L399" title="All 2 branches missed."> if (ignoreWeight != null) {</span>
<span class="nc" id="L400"> ignoreWeight.extractTerms(terms);</span>
}
<span class="nc" id="L402"> }</span>
}
/**
* The Class MtasSpanSequenceQuerySpans.
*/
protected class MtasSpanSequenceQuerySpans {
/** The spans. */
public Spans spans;
/** The optional. */
public boolean optional;
/**
* Instantiates a new mtas span sequence query spans.
*
* @param spans the spans
* @param optional the optional
*/
<span class="fc" id="L423"> public MtasSpanSequenceQuerySpans(Spans spans, boolean optional) {</span>
<span class="pc bpc" id="L424" title="1 of 2 branches missed."> this.spans = spans != null ? spans : new MtasSpanMatchNoneSpans(field);</span>
<span class="fc" id="L425"> this.optional = optional;</span>
<span class="fc" id="L426"> }</span>
}
/**
* The Class MtasSpanSequenceQueryWeight.
*/
private static class MtasSpanSequenceQueryWeight {
/** The span weight. */
public SpanWeight spanWeight;
/** The optional. */
public boolean optional;
/**
* Instantiates a new mtas span sequence query weight.
*
* @param spanWeight the span weight
* @param optional the optional
*/
public MtasSpanSequenceQueryWeight(SpanWeight spanWeight,
<span class="fc" id="L447"> boolean optional) {</span>
<span class="fc" id="L448"> this.spanWeight = spanWeight;</span>
<span class="fc" id="L449"> this.optional = optional;</span>
<span class="fc" id="L450"> }</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>