MtasSpanWithinQuery.java.html
20 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
<?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>MtasSpanWithinQuery.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">MtasSpanWithinQuery.java</span></div><h1>MtasSpanWithinQuery.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 org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.SpanWeight;
import org.apache.lucene.search.spans.SpanWithinQuery;
import mtas.search.spans.util.MtasSpanMaximumExpandQuery;
import mtas.search.spans.util.MtasSpanQuery;
/**
* The Class MtasSpanWithinQuery.
*/
public class MtasSpanWithinQuery extends MtasSpanQuery {
/** The base query. */
private SpanWithinQuery baseQuery;
/** The small query. */
private MtasSpanQuery smallQuery;
/** The big query. */
private MtasSpanQuery bigQuery;
/** The left boundary minimum. */
private int leftBoundaryMinimum;
/** The left boundary maximum. */
private int leftBoundaryMaximum;
/** The right boundary maximum. */
private int rightBoundaryMaximum;
/** The right boundary minimum. */
private int rightBoundaryMinimum;
/** The auto adjust big query. */
private boolean autoAdjustBigQuery;
/** The field. */
String field;
/**
* Instantiates a new mtas span within query.
*
* @param q1 the q 1
* @param q2 the q 2
*/
public MtasSpanWithinQuery(MtasSpanQuery q1, MtasSpanQuery q2) {
<span class="fc" id="L53"> this(q1, q2, 0, 0, 0, 0, true);</span>
<span class="fc" id="L54"> }</span>
/**
* Instantiates a new mtas span within query.
*
* @param q1 the q 1
* @param q2 the q 2
* @param leftMinimum the left minimum
* @param leftMaximum the left maximum
* @param rightMinimum the right minimum
* @param rightMaximum the right maximum
* @param adjustBigQuery the adjust big query
*/
public MtasSpanWithinQuery(MtasSpanQuery q1, MtasSpanQuery q2,
int leftMinimum, int leftMaximum, int rightMinimum, int rightMaximum,
boolean adjustBigQuery) {
<span class="pc bpc" id="L70" title="2 of 4 branches missed."> super(q1 != null ? q1.getMinimumWidth() : null,</span>
<span class="pc" id="L71"> q1 != null ? q1.getMaximumWidth() : null);</span>
<span class="pc bpc" id="L72" title="1 of 4 branches missed."> if (q2 != null && q2.getMinimumWidth() != null) {</span>
<span class="fc bfc" id="L73" title="All 2 branches covered."> if (this.getMinimumWidth() == null</span>
<span class="pc bpc" id="L74" title="1 of 2 branches missed."> || this.getMinimumWidth() < q2.getMinimumWidth()) {</span>
<span class="fc" id="L75"> this.setWidth(q2.getMinimumWidth(), this.getMaximumWidth());</span>
}
}
<span class="fc" id="L78"> bigQuery = q1;</span>
<span class="fc" id="L79"> smallQuery = q2;</span>
<span class="fc" id="L80"> leftBoundaryMinimum = leftMinimum;</span>
<span class="fc" id="L81"> leftBoundaryMaximum = leftMaximum;</span>
<span class="fc" id="L82"> rightBoundaryMinimum = rightMinimum;</span>
<span class="fc" id="L83"> rightBoundaryMaximum = rightMaximum;</span>
<span class="fc" id="L84"> autoAdjustBigQuery = adjustBigQuery;</span>
<span class="pc bpc" id="L85" title="2 of 4 branches missed."> if (bigQuery != null && bigQuery.getField() != null) {</span>
<span class="fc" id="L86"> field = bigQuery.getField();</span>
<span class="nc bnc" id="L87" title="All 4 branches missed."> } else if (smallQuery != null && smallQuery.getField() != null) {</span>
<span class="nc" id="L88"> field = smallQuery.getField();</span>
} else {
<span class="nc" id="L90"> field = null;</span>
}
<span class="pc bpc" id="L92" title="1 of 2 branches missed."> if (field != null) {</span>
<span class="fc" id="L93"> baseQuery = new SpanWithinQuery(</span>
new MtasSpanMaximumExpandQuery(bigQuery, leftBoundaryMinimum,
leftBoundaryMaximum, rightBoundaryMinimum, rightBoundaryMaximum),
smallQuery);
} else {
<span class="nc" id="L98"> baseQuery = null;</span>
}
<span class="fc" id="L100"> }</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="L110"> MtasSpanQuery newBigQuery = bigQuery.rewrite(reader);</span>
<span class="fc" id="L111"> MtasSpanQuery newSmallQuery = smallQuery.rewrite(reader);</span>
<span class="pc bpc" id="L113" title="4 of 8 branches missed."> if (newBigQuery == null || newBigQuery instanceof MtasSpanMatchNoneQuery</span>
|| newSmallQuery == null
|| newSmallQuery instanceof MtasSpanMatchNoneQuery) {
<span class="nc" id="L116"> return new MtasSpanMatchNoneQuery(field);</span>
}
<span class="pc bpc" id="L119" title="1 of 2 branches missed."> if (autoAdjustBigQuery) {</span>
<span class="pc bpc" id="L120" title="1 of 2 branches missed."> if (newBigQuery instanceof MtasSpanRecurrenceQuery) {</span>
<span class="nc" id="L121"> MtasSpanRecurrenceQuery recurrenceQuery = (MtasSpanRecurrenceQuery) newBigQuery;</span>
<span class="nc bnc" id="L122" title="All 2 branches missed."> if (recurrenceQuery.getIgnoreQuery() == null</span>
<span class="nc bnc" id="L123" title="All 2 branches missed."> && recurrenceQuery.getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L124"> rightBoundaryMaximum += leftBoundaryMaximum</span>
<span class="nc" id="L125"> + recurrenceQuery.getMaximumRecurrence();</span>
<span class="nc" id="L126"> rightBoundaryMinimum += leftBoundaryMinimum</span>
<span class="nc" id="L127"> + recurrenceQuery.getMinimumRecurrence();</span>
<span class="nc" id="L128"> leftBoundaryMaximum = 0;</span>
<span class="nc" id="L129"> leftBoundaryMinimum = 0;</span>
<span class="nc" id="L130"> newBigQuery = new MtasSpanMatchAllQuery(field);</span>
// System.out.println("REPLACE WITH " + newBigQuery + " (["
// + leftBoundaryMinimum + "," + leftBoundaryMaximum + "],["
// + rightBoundaryMinimum + "," + rightBoundaryMaximum + "])");
<span class="nc" id="L134"> return new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
leftBoundaryMinimum, leftBoundaryMaximum, rightBoundaryMinimum,
<span class="nc" id="L136"> rightBoundaryMaximum, autoAdjustBigQuery).rewrite(reader);</span>
}
<span class="pc bpc" id="L138" title="1 of 2 branches missed."> } else if (newBigQuery instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc bnc" id="L139" title="All 2 branches missed."> if (leftBoundaryMaximum > 0) {</span>
<span class="nc" id="L140"> rightBoundaryMaximum += leftBoundaryMaximum;</span>
<span class="nc" id="L141"> rightBoundaryMinimum += leftBoundaryMinimum;</span>
<span class="nc" id="L142"> leftBoundaryMaximum = 0;</span>
<span class="nc" id="L143"> leftBoundaryMinimum = 0;</span>
// System.out.println("REPLACE WITH " + newBigQuery + " (["
// + leftBoundaryMinimum + "," + leftBoundaryMaximum + "],["
// + rightBoundaryMinimum + "," + rightBoundaryMaximum + "])");
<span class="nc" id="L147"> return new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
leftBoundaryMinimum, leftBoundaryMaximum, rightBoundaryMinimum,
<span class="nc" id="L149"> rightBoundaryMaximum, autoAdjustBigQuery).rewrite(reader);</span>
}
<span class="fc bfc" id="L151" title="All 2 branches covered."> } else if (newBigQuery instanceof MtasSpanSequenceQuery) {</span>
<span class="fc" id="L152"> MtasSpanSequenceQuery sequenceQuery = (MtasSpanSequenceQuery) newBigQuery;</span>
<span class="pc bpc" id="L153" title="1 of 2 branches missed."> if (sequenceQuery.getIgnoreQuery() == null) {</span>
<span class="fc" id="L154"> List<MtasSpanSequenceItem> items = sequenceQuery.getItems();</span>
<span class="fc" id="L155"> List<MtasSpanSequenceItem> newItems = new ArrayList<MtasSpanSequenceItem>();</span>
<span class="fc" id="L156"> int newLeftBoundaryMinimum = 0;</span>
<span class="fc" id="L157"> int newLeftBoundaryMaximum = 0;</span>
<span class="fc" id="L158"> int newRightBoundaryMinimum = 0;</span>
<span class="fc" id="L159"> int newRightBoundaryMaximum = 0;</span>
<span class="fc bfc" id="L160" title="All 2 branches covered."> for (int i = 0; i < items.size(); i++) {</span>
// first item
<span class="fc bfc" id="L162" title="All 2 branches covered."> if (i == 0) {</span>
<span class="pc bpc" id="L163" title="1 of 2 branches missed."> if (items.get(i).getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L164"> newLeftBoundaryMaximum++;</span>
<span class="nc bnc" id="L165" title="All 2 branches missed."> if (!items.get(i).isOptional()) {</span>
<span class="nc" id="L166"> newLeftBoundaryMinimum++;</span>
}
<span class="fc" id="L168"> } else if (items.get(i)</span>
<span class="fc bfc" id="L169" title="All 2 branches covered."> .getQuery() instanceof MtasSpanRecurrenceQuery) {</span>
<span class="fc" id="L170"> MtasSpanRecurrenceQuery msrq = (MtasSpanRecurrenceQuery) items</span>
<span class="fc" id="L171"> .get(i).getQuery();</span>
<span class="pc bpc" id="L172" title="1 of 2 branches missed."> if (msrq.getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="fc" id="L173"> newLeftBoundaryMaximum += msrq.getMaximumRecurrence();</span>
<span class="pc bpc" id="L174" title="1 of 2 branches missed."> if (!items.get(i).isOptional()) {</span>
<span class="nc" id="L175"> newLeftBoundaryMinimum += msrq.getMinimumRecurrence();</span>
}
} else {
<span class="nc" id="L178"> newItems.add(items.get(i));</span>
}
<span class="fc" id="L180"> } else {</span>
<span class="fc" id="L181"> newItems.add(items.get(i));</span>
}
// last item
<span class="fc bfc" id="L184" title="All 2 branches covered."> } else if (i == (items.size() - 1)) {</span>
<span class="pc bpc" id="L185" title="1 of 2 branches missed."> if (items.get(i).getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L186"> newRightBoundaryMaximum++;</span>
<span class="nc bnc" id="L187" title="All 2 branches missed."> if (!items.get(i).isOptional()) {</span>
<span class="nc" id="L188"> newRightBoundaryMinimum++;</span>
}
<span class="fc" id="L190"> } else if (items.get(i)</span>
<span class="fc bfc" id="L191" title="All 2 branches covered."> .getQuery() instanceof MtasSpanRecurrenceQuery) {</span>
<span class="fc" id="L192"> MtasSpanRecurrenceQuery msrq = (MtasSpanRecurrenceQuery) items</span>
<span class="fc" id="L193"> .get(i).getQuery();</span>
<span class="pc bpc" id="L194" title="1 of 2 branches missed."> if (msrq.getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="fc" id="L195"> newRightBoundaryMaximum += msrq.getMaximumRecurrence();</span>
<span class="pc bpc" id="L196" title="1 of 2 branches missed."> if (!items.get(i).isOptional()) {</span>
<span class="fc" id="L197"> newRightBoundaryMinimum += msrq.getMinimumRecurrence();</span>
}
} else {
<span class="nc" id="L200"> newItems.add(items.get(i));</span>
}
<span class="fc" id="L202"> } else {</span>
<span class="fc" id="L203"> newItems.add(items.get(i));</span>
}
// other items
} else {
<span class="fc" id="L207"> newItems.add(items.get(i));</span>
}
}
<span class="fc" id="L210"> leftBoundaryMaximum += newLeftBoundaryMaximum;</span>
<span class="fc" id="L211"> leftBoundaryMinimum += newLeftBoundaryMinimum;</span>
<span class="fc" id="L212"> rightBoundaryMaximum += newRightBoundaryMaximum;</span>
<span class="fc" id="L213"> rightBoundaryMinimum += newRightBoundaryMinimum;</span>
<span class="pc bpc" id="L214" title="1 of 2 branches missed."> if (newItems.isEmpty()) {</span>
<span class="nc" id="L215"> rightBoundaryMaximum = Math.max(0,</span>
rightBoundaryMaximum + leftBoundaryMaximum - 1);
<span class="nc" id="L217"> rightBoundaryMinimum = Math.max(0,</span>
rightBoundaryMinimum + leftBoundaryMinimum - 1);
<span class="nc" id="L219"> leftBoundaryMaximum = 0;</span>
<span class="nc" id="L220"> leftBoundaryMinimum = 0;</span>
<span class="nc" id="L221"> newItems.add(new MtasSpanSequenceItem(</span>
new MtasSpanMatchAllQuery(field), false));
}
<span class="pc bpc" id="L224" title="2 of 6 branches missed."> if (!items.equals(newItems) || newLeftBoundaryMaximum > 0</span>
|| newRightBoundaryMaximum > 0) {
<span class="fc" id="L226"> newBigQuery = (new MtasSpanSequenceQuery(newItems, null, null))</span>
<span class="fc" id="L227"> .rewrite(reader);</span>
// System.out.println("REPLACE WITH " + newBigQuery + " (["
// + leftBoundaryMinimum + "," + leftBoundaryMaximum + "],["
// + rightBoundaryMinimum + "," + rightBoundaryMaximum + "])");
<span class="fc" id="L231"> return new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
leftBoundaryMinimum, leftBoundaryMaximum, rightBoundaryMinimum,
<span class="fc" id="L233"> rightBoundaryMaximum, autoAdjustBigQuery).rewrite(reader);</span>
}
}
}
}
<span class="pc bpc" id="L239" title="1 of 4 branches missed."> if (!newBigQuery.equals(bigQuery) || !newSmallQuery.equals(smallQuery)) {</span>
<span class="fc" id="L240"> return (new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
leftBoundaryMinimum, leftBoundaryMaximum, rightBoundaryMinimum,
<span class="fc" id="L242"> rightBoundaryMaximum, autoAdjustBigQuery)).rewrite(reader);</span>
<span class="pc bpc" id="L243" title="1 of 2 branches missed."> } else if (newBigQuery.equals(newSmallQuery)) {</span>
<span class="nc" id="L244"> return newBigQuery;</span>
} else {
<span class="fc" id="L246"> baseQuery = (SpanWithinQuery) baseQuery.rewrite(reader);</span>
<span class="fc" id="L247"> return super.rewrite(reader);</span>
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanQuery#getField()
*/
@Override
public String getField() {
<span class="fc" id="L258"> return field;</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="L271"> return baseQuery.createWeight(searcher, needsScores);</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Query#toString(java.lang.String)
*/
@Override
public String toString(String field) {
<span class="nc" id="L281"> StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L282"> buffer.append(this.getClass().getSimpleName() + "([");</span>
<span class="nc bnc" id="L283" title="All 2 branches missed."> if (smallQuery != null) {</span>
<span class="nc" id="L284"> buffer.append(smallQuery.toString(smallQuery.getField()));</span>
} else {
<span class="nc" id="L286"> buffer.append("null");</span>
}
<span class="nc" id="L288"> buffer.append(",");</span>
<span class="nc bnc" id="L289" title="All 2 branches missed."> if (bigQuery != null) {</span>
<span class="nc" id="L290"> buffer.append(bigQuery.toString(bigQuery.getField()));</span>
} else {
<span class="nc" id="L292"> buffer.append("null");</span>
}
<span class="nc" id="L294"> buffer.append("],[" + leftBoundaryMinimum + "," + leftBoundaryMaximum</span>
+ "],[" + rightBoundaryMinimum + "," + rightBoundaryMaximum + "])");
<span class="nc" id="L296"> 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="L306" title="All 2 branches covered."> if (this == obj)</span>
<span class="fc" id="L307"> return true;</span>
<span class="pc bpc" id="L308" title="1 of 2 branches missed."> if (obj == null)</span>
<span class="nc" id="L309"> return false;</span>
<span class="pc bpc" id="L310" title="1 of 2 branches missed."> if (getClass() != obj.getClass())</span>
<span class="nc" id="L311"> return false;</span>
<span class="fc" id="L312"> final MtasSpanWithinQuery that = (MtasSpanWithinQuery) obj;</span>
<span class="pc bpc" id="L313" title="4 of 10 branches missed."> return baseQuery.equals(that.baseQuery)</span>
&& leftBoundaryMinimum == that.leftBoundaryMinimum
&& leftBoundaryMaximum == that.leftBoundaryMaximum
&& rightBoundaryMinimum == that.rightBoundaryMinimum
&& rightBoundaryMaximum == that.rightBoundaryMaximum;
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Query#hashCode()
*/
@Override
public int hashCode() {
<span class="fc" id="L327"> int h = Integer.rotateLeft(classHash(), 1);</span>
<span class="fc" id="L328"> h ^= smallQuery.hashCode();</span>
<span class="fc" id="L329"> h = Integer.rotateLeft(h, 1);</span>
<span class="fc" id="L330"> h ^= bigQuery.hashCode();</span>
<span class="fc" id="L331"> h = Integer.rotateLeft(h, leftBoundaryMinimum) + leftBoundaryMinimum;</span>
<span class="fc" id="L332"> h ^= 2;</span>
<span class="fc" id="L333"> h = Integer.rotateLeft(h, leftBoundaryMaximum) + leftBoundaryMaximum;</span>
<span class="fc" id="L334"> h ^= 3;</span>
<span class="fc" id="L335"> h = Integer.rotateLeft(h, rightBoundaryMinimum) + rightBoundaryMinimum;</span>
<span class="fc" id="L336"> h ^= 5;</span>
<span class="fc" id="L337"> h = Integer.rotateLeft(h, rightBoundaryMaximum) + rightBoundaryMaximum;</span>
<span class="fc" id="L338"> 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>