MtasSpanRecurrenceQuery.java.html
16.2 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
<?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>MtasSpanRecurrenceQuery.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">MtasSpanRecurrenceQuery.java</span></div><h1>MtasSpanRecurrenceQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;
import java.io.IOException;
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 MtasSpanRecurrenceQuery.
*/
public class MtasSpanRecurrenceQuery extends MtasSpanQuery {
/** The query. */
private MtasSpanQuery query;
/** The minimum recurrence. */
private int minimumRecurrence;
/** The maximum recurrence. */
private int maximumRecurrence;
/** The ignore query. */
private MtasSpanQuery ignoreQuery;
/** The maximum ignore length. */
private Integer maximumIgnoreLength;
/** The field. */
private String field;
/**
* Instantiates a new mtas span recurrence query.
*
* @param query the query
* @param minimumRecurrence the minimum recurrence
* @param maximumRecurrence the maximum recurrence
* @param ignoreQuery the ignore query
* @param maximumIgnoreLength the maximum ignore length
*/
public MtasSpanRecurrenceQuery(MtasSpanQuery query, int minimumRecurrence,
int maximumRecurrence, MtasSpanQuery ignoreQuery,
Integer maximumIgnoreLength) {
<span class="fc" id="L55"> super(null, null);</span>
<span class="fc" id="L56"> field = query.getField();</span>
<span class="fc" id="L57"> this.query = query;</span>
<span class="pc bpc" id="L58" title="2 of 4 branches missed."> if (field != null && ignoreQuery != null) {</span>
<span class="nc bnc" id="L59" title="All 2 branches missed."> if (ignoreQuery.getField() == null</span>
<span class="nc bnc" id="L60" title="All 2 branches missed."> || field.equals(ignoreQuery.getField())) {</span>
<span class="nc" id="L61"> this.ignoreQuery = ignoreQuery;</span>
<span class="nc bnc" id="L62" title="All 2 branches missed."> if (maximumIgnoreLength == null) {</span>
<span class="nc" id="L63"> this.maximumIgnoreLength = 1;</span>
} else {
<span class="nc" id="L65"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
}
} else {
<span class="nc" id="L68"> throw new IllegalArgumentException(</span>
"ignore must have same field as clauses");
}
} else {
<span class="fc" id="L72"> this.ignoreQuery = null;</span>
<span class="fc" id="L73"> this.maximumIgnoreLength = null;</span>
}
<span class="fc" id="L75"> setRecurrence(minimumRecurrence, maximumRecurrence);</span>
<span class="fc" id="L76"> }</span>
/**
* Gets the query.
*
* @return the query
*/
public MtasSpanQuery getQuery() {
<span class="fc" id="L84"> return query;</span>
}
/**
* Gets the ignore query.
*
* @return the ignore query
*/
public MtasSpanQuery getIgnoreQuery() {
<span class="nc" id="L93"> return ignoreQuery;</span>
}
/**
* Gets the maximum ignore length.
*
* @return the maximum ignore length
*/
public Integer getMaximumIgnoreLength() {
<span class="nc" id="L102"> return maximumIgnoreLength;</span>
}
/**
* Gets the minimum recurrence.
*
* @return the minimum recurrence
*/
public int getMinimumRecurrence() {
<span class="fc" id="L111"> return minimumRecurrence;</span>
}
/**
* Gets the maximum recurrence.
*
* @return the maximum recurrence
*/
public int getMaximumRecurrence() {
<span class="fc" id="L120"> return maximumRecurrence;</span>
}
/**
* Sets the recurrence.
*
* @param minimumRecurrence the minimum recurrence
* @param maximumRecurrence the maximum recurrence
*/
public void setRecurrence(int minimumRecurrence, int maximumRecurrence) {
<span class="pc bpc" id="L130" title="1 of 2 branches missed."> if (minimumRecurrence > maximumRecurrence) {</span>
<span class="nc" id="L131"> throw new IllegalArgumentException(</span>
"minimumRecurrence > maximumRecurrence");
<span class="pc bpc" id="L133" title="1 of 2 branches missed."> } else if (minimumRecurrence < 1) {</span>
<span class="nc" id="L134"> throw new IllegalArgumentException("minimumRecurrence < 1 not supported");</span>
<span class="pc bpc" id="L135" title="1 of 2 branches missed."> } else if (query == null) {</span>
<span class="nc" id="L136"> throw new IllegalArgumentException("no clause");</span>
}
<span class="fc" id="L138"> this.minimumRecurrence = minimumRecurrence;</span>
<span class="fc" id="L139"> this.maximumRecurrence = maximumRecurrence;</span>
// set minimum/maximum
<span class="fc" id="L141"> Integer minimum = null;</span>
<span class="fc" id="L142"> Integer maximum = null;</span>
<span class="fc bfc" id="L143" title="All 2 branches covered."> if (query.getMinimumWidth() != null) {</span>
<span class="fc" id="L144"> minimum = minimumRecurrence * query.getMinimumWidth();</span>
}
<span class="fc bfc" id="L146" title="All 2 branches covered."> if (query.getMaximumWidth() != null) {</span>
<span class="fc" id="L147"> maximum = maximumRecurrence * query.getMaximumWidth();</span>
<span class="pc bpc" id="L148" title="3 of 4 branches missed."> if (ignoreQuery != null && maximumIgnoreLength != null) {</span>
<span class="nc bnc" id="L149" title="All 2 branches missed."> if (ignoreQuery.getMaximumWidth() != null) {</span>
<span class="nc" id="L150"> maximum += (maximumRecurrence - 1) * maximumIgnoreLength</span>
<span class="nc" id="L151"> * ignoreQuery.getMaximumWidth();</span>
} else {
<span class="nc" id="L153"> maximum = null;</span>
}
}
}
<span class="fc" id="L157"> setWidth(minimum, maximum);</span>
<span class="fc" id="L158"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanQuery#getField()
*/
@Override
public String getField() {
<span class="fc" id="L167"> return field;</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" id="L178"> MtasSpanQuery newQuery = query.rewrite(reader);</span>
<span class="pc bpc" id="L179" title="1 of 2 branches missed."> if (maximumRecurrence == 1) {</span>
<span class="nc" id="L180"> return newQuery;</span>
} else {
<span class="pc bpc" id="L182" title="1 of 2 branches missed."> MtasSpanQuery newIgnoreQuery = (ignoreQuery != null)</span>
<span class="pc" id="L183"> ? ignoreQuery.rewrite(reader) : null;</span>
<span class="pc bpc" id="L184" title="1 of 2 branches missed."> if (newQuery instanceof MtasSpanRecurrenceQuery) {</span>
// for now too difficult, possibly merge later
}
<span class="pc bpc" id="L187" title="1 of 4 branches missed."> if (!newQuery.equals(query)</span>
<span class="nc bnc" id="L188" title="All 2 branches missed."> || (newIgnoreQuery != null && !newIgnoreQuery.equals(ignoreQuery))) {</span>
<span class="fc" id="L189"> return new MtasSpanRecurrenceQuery(newQuery, minimumRecurrence,</span>
maximumRecurrence, newIgnoreQuery, maximumIgnoreLength)
<span class="fc" id="L191"> .rewrite(reader);</span>
} else {
<span class="fc" id="L193"> 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="L205"> StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L206"> buffer.append(this.getClass().getSimpleName() + "([");</span>
<span class="nc" id="L207"> buffer.append(query.toString(query.getField()));</span>
<span class="nc" id="L208"> buffer.append("," + minimumRecurrence + "," + maximumRecurrence);</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="fc bfc" id="L226" title="All 2 branches covered."> if (getClass() != obj.getClass())</span>
<span class="fc" id="L227"> return false;</span>
<span class="fc" id="L228"> final MtasSpanRecurrenceQuery other = (MtasSpanRecurrenceQuery) obj;</span>
boolean result;
<span class="fc" id="L230"> result = query.equals(other.query);</span>
<span class="fc bfc" id="L231" title="All 2 branches covered."> result &= minimumRecurrence == other.minimumRecurrence;</span>
<span class="fc bfc" id="L232" title="All 2 branches covered."> result &= maximumRecurrence == other.maximumRecurrence;</span>
<span class="fc bfc" id="L233" title="All 2 branches covered."> if (result) {</span>
boolean subResult;
<span class="pc bpc" id="L235" title="2 of 4 branches missed."> subResult = ignoreQuery == null && other.ignoreQuery == null;</span>
<span class="pc bpc" id="L236" title="3 of 4 branches missed."> subResult |= ignoreQuery != null && other.ignoreQuery != null</span>
<span class="pc bnc" id="L237" title="All 2 branches missed."> && ignoreQuery.equals(other.ignoreQuery);</span>
<span class="fc" id="L238"> return subResult;</span>
} else {
<span class="fc" id="L240"> return false;</span>
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Query#hashCode()
*/
@Override
public int hashCode() {
<span class="fc" id="L251"> int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L252"> h = (h * 7) ^ query.hashCode();</span>
<span class="fc" id="L253"> h = (h * 11) ^ minimumRecurrence;</span>
<span class="fc" id="L254"> h = (h * 13) ^ maximumRecurrence;</span>
<span class="fc" id="L255"> 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="L268"> SpanWeight subWeight = query.createWeight(searcher, false);</span>
<span class="fc" id="L269"> SpanWeight ignoreWeight = null;</span>
<span class="pc bpc" id="L270" title="1 of 2 branches missed."> if (ignoreQuery != null) {</span>
<span class="nc" id="L271"> ignoreWeight = ignoreQuery.createWeight(searcher, false);</span>
}
<span class="pc bpc" id="L273" title="1 of 2 branches missed."> return new SpanRecurrenceWeight(subWeight, ignoreWeight,</span>
maximumIgnoreLength, searcher,
<span class="pc" id="L275"> needsScores ? getTermContexts(subWeight) : null);</span>
}
/**
* The Class SpanRecurrenceWeight.
*/
protected class SpanRecurrenceWeight extends MtasSpanWeight {
/** The sub weight. */
final SpanWeight subWeight;
/** The ignore weight. */
final SpanWeight ignoreWeight;
/** The maximum ignore length. */
final Integer maximumIgnoreLength;
/**
* Instantiates a new span recurrence weight.
*
* @param subWeight the sub weight
* @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 SpanRecurrenceWeight(SpanWeight subWeight, SpanWeight ignoreWeight,
Integer maximumIgnoreLength, IndexSearcher searcher,
<span class="fc" id="L304"> Map<Term, TermContext> terms) throws IOException {</span>
<span class="fc" id="L305"> super(MtasSpanRecurrenceQuery.this, searcher, terms);</span>
<span class="fc" id="L306"> this.subWeight = subWeight;</span>
<span class="fc" id="L307"> this.ignoreWeight = ignoreWeight;</span>
<span class="fc" id="L308"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
<span class="fc" id="L309"> }</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="L320"> subWeight.extractTermContexts(contexts);</span>
<span class="fc" id="L321"> }</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="L334" title="1 of 2 branches missed."> if (field == null) {</span>
<span class="nc" id="L335"> return null;</span>
} else {
<span class="fc" id="L337"> Terms terms = context.reader().terms(field);</span>
<span class="pc bpc" id="L338" title="1 of 2 branches missed."> if (terms == null) {</span>
<span class="nc" id="L339"> return null; // field does not exist</span>
}
<span class="fc" id="L341"> Spans subSpans = subWeight.getSpans(context, requiredPostings);</span>
<span class="pc bpc" id="L342" title="1 of 2 branches missed."> if (subSpans == null) {</span>
<span class="nc" id="L343"> return null;</span>
} else {
<span class="fc" id="L345"> Spans ignoreSpans = null;</span>
<span class="pc bpc" id="L346" title="1 of 2 branches missed."> if (ignoreWeight != null) {</span>
<span class="nc" id="L347"> ignoreSpans = ignoreWeight.getSpans(context, requiredPostings);</span>
}
<span class="fc" id="L349"> return new MtasSpanRecurrenceSpans(</span>
<span class="fc" id="L350"> subSpans, minimumRecurrence, maximumRecurrence, 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" id="L363"> subWeight.extractTerms(terms);</span>
<span class="nc" id="L364"> }</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>