MtasSpanSequenceQuery.java.html
21.6 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
455
456
<?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;
import mtas.search.spans.util.MtasSpanWeight;
import mtas.search.spans.util.MtasSpans;
/**
* 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="L50"> super(null, null);</span>
<span class="fc" id="L51"> Integer minimum = 0;</span>
<span class="fc" id="L52"> Integer maximum = 0;</span>
<span class="fc" id="L53"> this.items = items;</span>
// get field and do checks
<span class="fc bfc" id="L55" title="All 2 branches covered."> for (MtasSpanSequenceItem item : items) {</span>
<span class="fc bfc" id="L56" title="All 2 branches covered."> if (field == null) {</span>
<span class="fc" id="L57"> field = item.getQuery().getField();</span>
<span class="pc bpc" id="L58" title="1 of 2 branches missed."> } else if (item.getQuery().getField() != null</span>
<span class="pc bpc" id="L59" title="1 of 2 branches missed."> && !item.getQuery().getField().equals(field)) {</span>
<span class="nc" id="L60"> throw new IllegalArgumentException("Clauses must have same field.");</span>
}
<span class="fc bfc" id="L62" title="All 4 branches covered."> if (minimum != null && !item.isOptional()) {</span>
<span class="fc bfc" id="L63" title="All 2 branches covered."> minimum = item.getQuery().getMinimumWidth() != null</span>
<span class="fc" id="L64"> ? minimum + item.getQuery().getMinimumWidth() : null;</span>
}
<span class="fc bfc" id="L66" title="All 2 branches covered."> if (maximum != null) {</span>
<span class="fc bfc" id="L67" title="All 2 branches covered."> maximum = item.getQuery().getMaximumWidth() != null</span>
<span class="fc" id="L68"> ? maximum + item.getQuery().getMaximumWidth() : null;</span>
}
<span class="fc" id="L70"> }</span>
// check ignore
<span class="pc bpc" id="L72" title="1 of 4 branches missed."> if (field != null && ignoreQuery != null) {</span>
<span class="pc bpc" id="L73" title="1 of 2 branches missed."> if (ignoreQuery.getField() == null</span>
<span class="pc bpc" id="L74" title="1 of 2 branches missed."> || field.equals(ignoreQuery.getField())) {</span>
<span class="fc" id="L75"> this.ignoreQuery = ignoreQuery;</span>
<span class="pc bpc" id="L76" title="1 of 2 branches missed."> if (maximumIgnoreLength == null) {</span>
<span class="nc" id="L77"> this.maximumIgnoreLength = MtasIgnoreItem.DEFAULT_MAXIMUM_IGNORE_LENGTH;</span>
} else {
<span class="fc" id="L79"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
}
} else {
<span class="nc" id="L82"> throw new IllegalArgumentException(</span>
"ignore must have same field as clauses");
}
<span class="pc bpc" id="L85" title="2 of 4 branches missed."> if (maximum != null && items.size() > 1) {</span>
<span class="pc bpc" id="L86" title="1 of 2 branches missed."> if (this.ignoreQuery.getMaximumWidth() != null) {</span>
<span class="fc" id="L87"> maximum += (items.size() - 1) * this.maximumIgnoreLength</span>
<span class="fc" id="L88"> * this.ignoreQuery.getMaximumWidth();</span>
} else {
<span class="nc" id="L90"> maximum = null;</span>
}
}
} else {
<span class="fc" id="L94"> this.ignoreQuery = null;</span>
<span class="fc" id="L95"> this.maximumIgnoreLength = null;</span>
}
<span class="fc" id="L97"> setWidth(minimum, maximum);</span>
<span class="fc" id="L98"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanQuery#getField()
*/
@Override
public String getField() {
<span class="fc" id="L107"> return field;</span>
}
/**
* Gets the items.
*
* @return the items
*/
public List<MtasSpanSequenceItem> getItems() {
<span class="fc" id="L116"> return items;</span>
}
/**
* Gets the ignore query.
*
* @return the ignore query
*/
public MtasSpanQuery getIgnoreQuery() {
<span class="fc" id="L125"> return ignoreQuery;</span>
}
/**
* Gets the maximum ignore length.
*
* @return the maximum ignore length
*/
public Integer getMaximumIgnoreLength() {
<span class="nc" id="L134"> 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="fc bfc" id="L145" title="All 2 branches covered."> if (items.size() == 1) {</span>
<span class="fc" id="L146"> return items.get(0).getQuery().rewrite(reader);</span>
} else {
MtasSpanSequenceItem newItem;
<span class="fc" id="L149"> MtasSpanSequenceItem previousNewItem = null;</span>
<span class="fc" id="L150"> ArrayList<MtasSpanSequenceItem> newItems = new ArrayList<>(items.size());</span>
<span class="fc bfc" id="L151" title="All 2 branches covered."> MtasSpanQuery newIgnoreClause = ignoreQuery != null</span>
<span class="fc" id="L152"> ? ignoreQuery.rewrite(reader) : null;</span>
<span class="fc bfc" id="L153" title="All 2 branches covered."> boolean actuallyRewritten = ignoreQuery != null</span>
<span class="fc bfc" id="L154" title="All 2 branches covered."> ? !newIgnoreClause.equals(ignoreQuery) : false;</span>
<span class="fc bfc" id="L155" title="All 2 branches covered."> for (int i = 0; i < items.size(); i++) {</span>
<span class="fc" id="L156"> newItem = items.get(i).rewrite(reader);</span>
<span class="pc bpc" id="L157" title="1 of 2 branches missed."> if (newItem.getQuery() instanceof MtasSpanMatchNoneQuery) {</span>
<span class="nc bnc" id="L158" title="All 2 branches missed."> if (!newItem.isOptional()) {</span>
<span class="nc" id="L159"> return new MtasSpanMatchNoneQuery(field);</span>
} else {
<span class="nc" id="L161"> actuallyRewritten = true;</span>
}
} else {
<span class="fc bfc" id="L164" title="All 2 branches covered."> actuallyRewritten |= !items.get(i).equals(newItem);</span>
<span class="fc" id="L165"> MtasSpanSequenceItem previousMergedItem = MtasSpanSequenceItem.merge(</span>
previousNewItem, newItem, ignoreQuery, maximumIgnoreLength);
<span class="pc bpc" id="L167" title="1 of 2 branches missed."> if (previousMergedItem != null) {</span>
<span class="nc" id="L168"> newItems.set((newItems.size() - 1), previousMergedItem);</span>
<span class="nc" id="L169"> actuallyRewritten = true;</span>
} else {
<span class="fc" id="L171"> newItems.add(newItem);</span>
}
<span class="fc" id="L173"> previousNewItem = newItem;</span>
}
}
<span class="fc bfc" id="L176" title="All 2 branches covered."> if (!actuallyRewritten) {</span>
<span class="fc" id="L177"> return super.rewrite(reader);</span>
} else {
<span class="pc bpc" id="L179" title="1 of 2 branches missed."> if (!newItems.isEmpty()) {</span>
<span class="fc" id="L180"> return new MtasSpanSequenceQuery(newItems, newIgnoreClause,</span>
<span class="fc" id="L181"> maximumIgnoreLength).rewrite(reader);</span>
} else {
<span class="nc" id="L183"> 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="L196"> StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L197"> buffer.append(this.getClass().getSimpleName() + "([");</span>
<span class="nc" id="L198"> Iterator<MtasSpanSequenceItem> i = items.iterator();</span>
<span class="nc bnc" id="L199" title="All 2 branches missed."> while (i.hasNext()) {</span>
<span class="nc" id="L200"> MtasSpanSequenceItem item = i.next();</span>
<span class="nc" id="L201"> MtasSpanQuery clause = item.getQuery();</span>
<span class="nc" id="L202"> buffer.append(clause.toString(field));</span>
<span class="nc bnc" id="L203" title="All 2 branches missed."> if (item.isOptional()) {</span>
<span class="nc" id="L204"> buffer.append("{OPTIONAL}");</span>
}
<span class="nc bnc" id="L206" title="All 2 branches missed."> if (i.hasNext()) {</span>
<span class="nc" id="L207"> buffer.append(", ");</span>
}
<span class="nc" id="L209"> }</span>
<span class="nc" id="L210"> buffer.append("]");</span>
<span class="nc" id="L211"> buffer.append(", ");</span>
<span class="nc" id="L212"> buffer.append(ignoreQuery);</span>
<span class="nc" id="L213"> buffer.append(")");</span>
<span class="nc" id="L214"> 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="L224" title="All 2 branches covered."> if (this == obj)</span>
<span class="fc" id="L225"> return true;</span>
<span class="pc bpc" id="L226" title="1 of 2 branches missed."> if (obj == null)</span>
<span class="nc" id="L227"> return false;</span>
<span class="fc bfc" id="L228" title="All 2 branches covered."> if (getClass() != obj.getClass())</span>
<span class="fc" id="L229"> return false;</span>
<span class="fc" id="L230"> MtasSpanSequenceQuery other = (MtasSpanSequenceQuery) obj;</span>
<span class="pc bpc" id="L231" 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="L234" 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="L244"> int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L245"> h = (h * 3) ^ field.hashCode();</span>
<span class="fc" id="L246"> h = (h * 5) ^ items.hashCode();</span>
<span class="pc bpc" id="L247" title="1 of 2 branches missed."> if (ignoreQuery != null) {</span>
<span class="nc" id="L248"> h = (h * 7) ^ ignoreQuery.hashCode();</span>
<span class="nc" id="L249"> h = (h * 11) ^ maximumIgnoreLength.hashCode();</span>
}
<span class="fc" id="L251"> return h;</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) throws IOException {
<span class="fc" id="L264"> List<MtasSpanSequenceQueryWeight> subWeights = new ArrayList<>();</span>
<span class="fc" id="L265"> SpanWeight ignoreWeight = null;</span>
<span class="fc bfc" id="L266" title="All 2 branches covered."> for (MtasSpanSequenceItem item : items) {</span>
<span class="fc" id="L267"> subWeights.add(new MtasSpanSequenceQueryWeight(</span>
<span class="fc" id="L268"> item.getQuery().createWeight(searcher, false), item.isOptional()));</span>
<span class="fc" id="L269"> }</span>
<span class="fc bfc" id="L270" title="All 2 branches covered."> if (ignoreQuery != null) {</span>
<span class="fc" id="L271"> ignoreWeight = ignoreQuery.createWeight(searcher, false);</span>
}
<span class="pc bpc" id="L273" title="1 of 2 branches missed."> return new SpanSequenceWeight(subWeights, ignoreWeight, maximumIgnoreLength,</span>
<span class="pc" id="L274"> 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="L285"> List<SpanWeight> weights = new ArrayList<>();</span>
<span class="nc bnc" id="L286" title="All 2 branches missed."> for (MtasSpanSequenceQueryWeight item : items) {</span>
<span class="nc" id="L287"> weights.add(item.spanWeight);</span>
<span class="nc" id="L288"> }</span>
<span class="nc" id="L289"> return getTermContexts(weights);</span>
}
/**
* The Class SpanSequenceWeight.
*/
protected class SpanSequenceWeight extends MtasSpanWeight {
/** 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="L319"> throws IOException {</span>
<span class="fc" id="L320"> super(MtasSpanSequenceQuery.this, searcher, terms);</span>
<span class="fc" id="L321"> this.subWeights = subWeights;</span>
<span class="fc" id="L322"> this.ignoreWeight = ignoreWeight;</span>
<span class="fc" id="L323"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
<span class="fc" id="L324"> }</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="L335" title="All 2 branches covered."> for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="fc" id="L336"> w.spanWeight.extractTermContexts(contexts);</span>
<span class="fc" id="L337"> }</span>
<span class="pc bpc" id="L338" title="1 of 2 branches missed."> if (ignoreWeight != null) {</span>
<span class="nc" id="L339"> ignoreWeight.extractTermContexts(contexts);</span>
}
<span class="fc" id="L341"> }</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="pc bpc" id="L354" title="1 of 2 branches missed."> if (field == null) {</span>
<span class="nc" id="L355"> return null;</span>
} else {
<span class="fc" id="L357"> Terms terms = context.reader().terms(field);</span>
<span class="pc bpc" id="L358" title="1 of 2 branches missed."> if (terms == null) {</span>
<span class="nc" id="L359"> return null; // field does not exist</span>
}
<span class="fc" id="L361"> List<MtasSpanSequenceQuerySpans> setSequenceSpans = new ArrayList<>(</span>
<span class="fc" id="L362"> items.size());</span>
<span class="fc" id="L363"> Spans ignoreSpans = null;</span>
<span class="fc" id="L364"> boolean allSpansEmpty = true;</span>
<span class="fc bfc" id="L365" title="All 2 branches covered."> for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="fc" id="L366"> Spans sequenceSpans = w.spanWeight.getSpans(context,</span>
requiredPostings);
<span class="pc bpc" id="L368" title="1 of 2 branches missed."> if (sequenceSpans != null) {</span>
<span class="fc" id="L369"> setSequenceSpans</span>
<span class="fc" id="L370"> .add(new MtasSpanSequenceQuerySpans(sequenceSpans, w.optional));</span>
<span class="fc" id="L371"> allSpansEmpty = false;</span>
} else {
<span class="nc bnc" id="L373" title="All 2 branches missed."> if (w.optional) {</span>
<span class="nc" id="L374"> setSequenceSpans</span>
<span class="nc" id="L375"> .add(new MtasSpanSequenceQuerySpans(null, w.optional));</span>
} else {
<span class="nc" id="L377"> return null;</span>
}
}
<span class="fc" id="L380"> }</span>
<span class="pc bpc" id="L381" title="1 of 2 branches missed."> if (allSpansEmpty) {</span>
<span class="nc" id="L382"> return null; // at least one required</span>
<span class="fc bfc" id="L383" title="All 2 branches covered."> } else if (ignoreWeight != null) {</span>
<span class="fc" id="L384"> ignoreSpans = ignoreWeight.getSpans(context, requiredPostings);</span>
}
<span class="fc" id="L386"> 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="L398" title="All 2 branches missed."> for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="nc" id="L399"> w.spanWeight.extractTerms(terms);</span>
<span class="nc" id="L400"> }</span>
<span class="nc bnc" id="L401" title="All 2 branches missed."> if (ignoreWeight != null) {</span>
<span class="nc" id="L402"> ignoreWeight.extractTerms(terms);</span>
}
<span class="nc" id="L404"> }</span>
}
/**
* The Class MtasSpanSequenceQuerySpans.
*/
protected static 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="L425"> public MtasSpanSequenceQuerySpans(Spans spans, boolean optional) {</span>
<span class="pc bpc" id="L426" title="1 of 2 branches missed."> this.spans = spans != null ? spans : new MtasSpanMatchNoneSpans();</span>
<span class="fc" id="L427"> this.optional = optional;</span>
<span class="fc" id="L428"> }</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="L449"> boolean optional) {</span>
<span class="fc" id="L450"> this.spanWeight = spanWeight;</span>
<span class="fc" id="L451"> this.optional = optional;</span>
<span class="fc" id="L452"> }</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>