MtasSpanMaximumExpandQuery.java.html
19.4 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
<?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>MtasSpanMaximumExpandQuery.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.util</a> > <span class="el_source">MtasSpanMaximumExpandQuery.java</span></div><h1>MtasSpanMaximumExpandQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans.util;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import org.apache.lucene.codecs.FieldsProducer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReader;
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.TwoPhaseIterator;
import org.apache.lucene.search.spans.SpanCollector;
import org.apache.lucene.search.spans.SpanWeight;
import org.apache.lucene.search.spans.Spans;
import mtas.codec.util.CodecInfo;
import mtas.codec.util.CodecInfo.IndexDoc;
import mtas.search.spans.MtasSpanMatchNoneSpans;
/**
* The Class MtasSpanMaximumExpandQuery.
*/
public class MtasSpanMaximumExpandQuery extends MtasSpanQuery {
/** The query. */
MtasSpanQuery query;
/** The minimum left. */
int minimumLeft;
/** The maximum left. */
int maximumLeft;
/** The minimum right. */
int minimumRight;
/** The maximum right. */
int maximumRight;
/**
* Instantiates a new mtas span maximum expand query.
*
* @param query the query
* @param minimumLeft the minimum left
* @param maximumLeft the maximum left
* @param minimumRight the minimum right
* @param maximumRight the maximum right
*/
public MtasSpanMaximumExpandQuery(MtasSpanQuery query, int minimumLeft,
int maximumLeft, int minimumRight, int maximumRight) {
<span class="fc" id="L56"> super(null, null);</span>
<span class="fc" id="L57"> this.query = query;</span>
<span class="pc bpc" id="L58" title="4 of 8 branches missed."> if (minimumLeft > maximumLeft || minimumRight > maximumRight</span>
|| minimumLeft < 0 || minimumRight < 0) {
<span class="nc" id="L60"> throw new IllegalArgumentException();</span>
}
<span class="fc" id="L62"> this.minimumLeft = minimumLeft;</span>
<span class="fc" id="L63"> this.maximumLeft = maximumLeft;</span>
<span class="fc" id="L64"> this.minimumRight = minimumRight;</span>
<span class="fc" id="L65"> this.maximumRight = maximumRight;</span>
<span class="fc" id="L66"> Integer minimum = query.getMinimumWidth();</span>
<span class="fc" id="L67"> Integer maximum = query.getMaximumWidth();</span>
<span class="fc bfc" id="L68" title="All 2 branches covered."> if (minimum != null) {</span>
<span class="fc" id="L69"> minimum += minimumLeft + minimumRight;</span>
}
<span class="fc bfc" id="L71" title="All 2 branches covered."> if (maximum != null) {</span>
<span class="fc" id="L72"> maximum += maximumLeft + maximumRight;</span>
}
<span class="fc" id="L74"> setWidth(minimum, maximum);</span>
<span class="fc" id="L75"> }</span>
/* (non-Javadoc)
* @see mtas.search.spans.util.MtasSpanQuery#createWeight(org.apache.lucene.search.IndexSearcher, boolean)
*/
@Override
public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores)
throws IOException {
<span class="fc" id="L83"> SpanWeight subWeight = query.createWeight(searcher, needsScores);</span>
<span class="pc bpc" id="L84" title="3 of 4 branches missed."> if (maximumLeft == 0 && maximumRight == 0) {</span>
<span class="nc" id="L85"> return subWeight;</span>
} else {
<span class="fc" id="L87"> return new MtasMaximumExpandWeight(subWeight, searcher, needsScores);</span>
}
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.SpanQuery#getField()
*/
@Override
public String getField() {
<span class="fc" id="L96"> return query.getField();</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.Query#toString(java.lang.String)
*/
@Override
public String toString(String field) {
<span class="nc" id="L104"> StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L105"> buffer.append(this.getClass().getSimpleName() + "([");</span>
<span class="nc" id="L106"> buffer.append(query.toString(field) + "]["+minimumLeft+","+maximumLeft+"]["+minimumRight+","+maximumRight+"])");</span>
<span class="nc" id="L107"> return buffer.toString();</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.Query#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
<span class="pc bpc" id="L115" title="1 of 2 branches missed."> if (this == obj)</span>
<span class="nc" id="L116"> return true;</span>
<span class="pc bpc" id="L117" title="1 of 2 branches missed."> if (obj == null)</span>
<span class="nc" id="L118"> return false;</span>
<span class="pc bpc" id="L119" title="1 of 2 branches missed."> if (getClass() != obj.getClass())</span>
<span class="nc" id="L120"> return false;</span>
<span class="fc" id="L121"> final MtasSpanMaximumExpandQuery that = (MtasSpanMaximumExpandQuery) obj;</span>
boolean isEqual;
<span class="fc" id="L123"> isEqual = query.equals(that.query);</span>
<span class="pc bpc" id="L124" title="1 of 2 branches missed."> isEqual &= minimumLeft == that.minimumLeft;</span>
<span class="fc bfc" id="L125" title="All 2 branches covered."> isEqual &= maximumLeft == that.maximumLeft;</span>
<span class="fc bfc" id="L126" title="All 2 branches covered."> isEqual &= minimumRight == that.minimumRight;</span>
<span class="fc bfc" id="L127" title="All 2 branches covered."> isEqual &= maximumRight == that.maximumRight;</span>
<span class="fc" id="L128"> return isEqual;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.Query#hashCode()
*/
@Override
public int hashCode() {
<span class="nc" id="L136"> int h = Integer.rotateLeft(classHash(), 1);</span>
<span class="nc" id="L137"> h ^= query.hashCode();</span>
<span class="nc" id="L138"> h = Integer.rotateLeft(h, minimumLeft) + minimumLeft;</span>
<span class="nc" id="L139"> h ^= 2;</span>
<span class="nc" id="L140"> h = Integer.rotateLeft(h, maximumLeft) + maximumLeft;</span>
<span class="nc" id="L141"> h ^= 3;</span>
<span class="nc" id="L142"> h = Integer.rotateLeft(h, minimumRight) + minimumRight;</span>
<span class="nc" id="L143"> h ^= 5;</span>
<span class="nc" id="L144"> h = Integer.rotateLeft(h, maximumRight) + maximumRight;</span>
<span class="nc" id="L145"> 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="L153"> MtasSpanQuery newQuery = query.rewrite(reader);</span>
<span class="pc bpc" id="L154" title="1 of 4 branches missed."> if (maximumLeft == 0 && maximumRight == 0) {</span>
<span class="fc" id="L155"> return newQuery;</span>
<span class="pc bpc" id="L156" title="1 of 2 branches missed."> } else if (!query.equals(newQuery)) {</span>
<span class="nc" id="L157"> return new MtasSpanMaximumExpandQuery(newQuery, minimumLeft, maximumLeft,</span>
minimumRight, maximumRight);
} else {
<span class="fc" id="L160"> return super.rewrite(reader);</span>
}
}
/**
* The Class MtasMaximumExpandWeight.
*/
private class MtasMaximumExpandWeight extends SpanWeight {
/** The sub weight. */
SpanWeight subWeight;
/**
* Instantiates a new mtas maximum expand weight.
*
* @param subWeight the sub weight
* @param searcher the searcher
* @param needsScores the needs scores
* @throws IOException Signals that an I/O exception has occurred.
*/
public MtasMaximumExpandWeight(SpanWeight subWeight, IndexSearcher searcher,
<span class="fc" id="L181"> boolean needsScores) throws IOException {</span>
<span class="pc bpc" id="L182" title="1 of 2 branches missed."> super(MtasSpanMaximumExpandQuery.this, searcher,</span>
<span class="pc" id="L183"> needsScores ? getTermContexts(subWeight) : null);</span>
<span class="fc" id="L184"> this.subWeight = subWeight;</span>
<span class="fc" id="L185"> }</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="L192"> subWeight.extractTermContexts(contexts);</span>
<span class="nc" id="L193"> }</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 ctx, Postings requiredPostings)
throws IOException {
<span class="fc" id="L201"> Spans spans = subWeight.getSpans(ctx, requiredPostings);</span>
<span class="pc bpc" id="L202" title="3 of 4 branches missed."> if (maximumLeft == 0 && maximumRight == 0) {</span>
<span class="nc" id="L203"> return spans;</span>
} else {
try {
// get leafreader
<span class="fc" id="L207"> LeafReader r = ctx.reader();</span>
// get delegate
<span class="fc" id="L209"> Boolean hasMethod = true;</span>
<span class="fc bfc" id="L210" title="All 2 branches covered."> while (hasMethod) {</span>
<span class="fc" id="L211"> hasMethod = false;</span>
<span class="fc" id="L212"> Method[] methods = r.getClass().getMethods();</span>
<span class="fc bfc" id="L213" title="All 2 branches covered."> for (Method m : methods) {</span>
<span class="pc bpc" id="L214" title="1 of 2 branches missed."> if (m.getName().equals("getDelegate")) {</span>
<span class="nc" id="L215"> hasMethod = true;</span>
<span class="nc" id="L216"> r = (LeafReader) m.invoke(r, (Object[]) null);</span>
<span class="nc" id="L217"> break;</span>
}
}
<span class="fc" id="L220"> } // get fieldsproducer</span>
<span class="fc" id="L221"> Method fpm = r.getClass().getMethod("getPostingsReader",</span>
(Class<?>[]) null);
<span class="fc" id="L223"> FieldsProducer fp = (FieldsProducer) fpm.invoke(r, (Object[]) null);</span>
// get MtasFieldsProducer using terms
<span class="fc" id="L225"> Terms t = fp.terms(field);</span>
<span class="pc bpc" id="L226" title="1 of 2 branches missed."> if (t == null) {</span>
<span class="nc" id="L227"> return new MtasSpanMatchNoneSpans(field);</span>
} else {
<span class="fc" id="L229"> CodecInfo mtasCodecInfo = CodecInfo.getCodecInfoFromTerms(t);</span>
<span class="fc" id="L230"> return new MtasMaximumExpandSpans(mtasCodecInfo, query.getField(),</span>
spans);
}
<span class="nc" id="L233"> } catch (Exception e) {</span>
<span class="nc" id="L234"> throw new IOException("Can't get reader", e);</span>
}
}
}
/* (non-Javadoc)
* @see org.apache.lucene.search.Weight#extractTerms(java.util.Set)
*/
@Override
public void extractTerms(Set<Term> terms) {
<span class="nc" id="L245"> subWeight.extractTerms(terms);</span>
<span class="nc" id="L246"> }</span>
}
/**
* The Class MtasMaximumExpandSpans.
*/
private class MtasMaximumExpandSpans extends Spans {
/** The sub spans. */
Spans subSpans;
/** The min position. */
int minPosition;
/** The max position. */
int maxPosition;
/** The field. */
String field;
/** The mtas codec info. */
CodecInfo mtasCodecInfo;
/** The start position. */
int startPosition;
/** The end position. */
int endPosition;
/**
* Instantiates a new mtas maximum expand spans.
*
* @param mtasCodecInfo the mtas codec info
* @param field the field
* @param subSpans the sub spans
*/
public MtasMaximumExpandSpans(CodecInfo mtasCodecInfo, String field,
<span class="fc" id="L284"> Spans subSpans) {</span>
<span class="fc" id="L285"> super();</span>
<span class="fc" id="L286"> this.subSpans = subSpans;</span>
<span class="fc" id="L287"> this.field = field;</span>
<span class="fc" id="L288"> this.mtasCodecInfo = mtasCodecInfo;</span>
<span class="fc" id="L289"> this.minPosition = 0;</span>
<span class="fc" id="L290"> this.maxPosition = 0;</span>
<span class="fc" id="L291"> this.startPosition = -1;</span>
<span class="fc" id="L292"> this.endPosition = -1;</span>
<span class="fc" id="L293"> }</span>
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.Spans#nextStartPosition()
*/
@Override
public int nextStartPosition() throws IOException {
int basicStartPosition;
int basicEndPosition;
<span class="fc" id="L302"> while ((basicStartPosition = subSpans</span>
<span class="fc bfc" id="L303" title="All 2 branches covered."> .nextStartPosition()) != NO_MORE_POSITIONS) {</span>
<span class="fc" id="L304"> basicEndPosition = subSpans.endPosition();</span>
<span class="fc" id="L305"> startPosition = Math.max(minPosition,</span>
(basicStartPosition - maximumLeft));
<span class="fc" id="L307"> endPosition = Math.min(maxPosition + 1,</span>
(basicEndPosition + maximumRight));
<span class="pc bpc" id="L309" title="2 of 4 branches missed."> if (startPosition <= (basicStartPosition - minimumLeft)</span>
&& endPosition >= (basicEndPosition + minimumRight)) {
<span class="fc" id="L311"> return this.startPosition;</span>
}
}
<span class="fc" id="L314"> startPosition = NO_MORE_POSITIONS;</span>
<span class="fc" id="L315"> endPosition = NO_MORE_POSITIONS;</span>
<span class="fc" id="L316"> return NO_MORE_POSITIONS;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.Spans#startPosition()
*/
@Override
public int startPosition() {
<span class="fc" id="L324"> return startPosition;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.Spans#endPosition()
*/
@Override
public int endPosition() {
<span class="fc" id="L332"> return endPosition;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.Spans#width()
*/
@Override
public int width() {
<span class="nc" id="L340"> return endPosition-startPosition;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.Spans#collect(org.apache.lucene.search.spans.SpanCollector)
*/
@Override
public void collect(SpanCollector collector) throws IOException {
<span class="nc" id="L348"> subSpans.collect(collector);</span>
<span class="nc" id="L349"> }</span>
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.Spans#asTwoPhaseIterator()
*/
@Override
public final TwoPhaseIterator asTwoPhaseIterator() {
// return subSpans.asTwoPhaseIterator();
<span class="fc" id="L357"> return null;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.Spans#positionsCost()
*/
@Override
public float positionsCost() {
//return subSpans.positionsCost();
<span class="nc" id="L366"> return 0;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.DocIdSetIterator#docID()
*/
@Override
public int docID() {
<span class="fc" id="L374"> return subSpans.docID();</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
*/
@Override
public int nextDoc() throws IOException {
<span class="fc" id="L382"> int docId = subSpans.nextDoc();</span>
<span class="fc" id="L383"> startPosition = -1;</span>
<span class="fc" id="L384"> endPosition = -1;</span>
<span class="fc bfc" id="L385" title="All 2 branches covered."> if (docId != NO_MORE_DOCS) {</span>
<span class="fc" id="L386"> IndexDoc doc = mtasCodecInfo.getDoc(field, docId);</span>
<span class="pc bpc" id="L387" title="1 of 2 branches missed."> if (doc != null) {</span>
<span class="fc" id="L388"> minPosition = doc.minPosition;</span>
<span class="fc" id="L389"> maxPosition = doc.maxPosition;</span>
} else {
<span class="nc" id="L391"> minPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L392"> maxPosition = NO_MORE_POSITIONS;</span>
}
<span class="fc" id="L394"> } else {</span>
<span class="fc" id="L395"> minPosition = NO_MORE_POSITIONS;</span>
<span class="fc" id="L396"> maxPosition = NO_MORE_POSITIONS;</span>
}
<span class="fc" id="L398"> return docId;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.DocIdSetIterator#advance(int)
*/
@Override
public int advance(int target) throws IOException {
<span class="nc" id="L406"> int docId = subSpans.advance(target);</span>
<span class="nc" id="L407"> startPosition = -1;</span>
<span class="nc" id="L408"> endPosition = -1;</span>
<span class="nc bnc" id="L409" title="All 2 branches missed."> if (docId != NO_MORE_DOCS) {</span>
<span class="nc" id="L410"> IndexDoc doc = mtasCodecInfo.getDoc(field, docId);</span>
<span class="nc bnc" id="L411" title="All 2 branches missed."> if (doc != null) {</span>
<span class="nc" id="L412"> minPosition = doc.minPosition;</span>
<span class="nc" id="L413"> maxPosition = doc.maxPosition;</span>
} else {
<span class="nc" id="L415"> minPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L416"> maxPosition = NO_MORE_POSITIONS;</span>
}
<span class="nc" id="L418"> } else {</span>
<span class="nc" id="L419"> minPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L420"> maxPosition = NO_MORE_POSITIONS;</span>
}
<span class="nc" id="L422"> return docId;</span>
}
/* (non-Javadoc)
* @see org.apache.lucene.search.DocIdSetIterator#cost()
*/
@Override
public long cost() {
<span class="pc bpc" id="L430" title="1 of 2 branches missed."> return subSpans!=null?subSpans.cost():0;</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>