MtasSpanAndQuery.java.html
10.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
<?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>MtasSpanAndQuery.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">MtasSpanAndQuery.java</span></div><h1>MtasSpanAndQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;
import java.io.IOException;
import java.util.HashSet;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanWeight;
import mtas.search.spans.util.MtasExtendedSpanAndQuery;
import mtas.search.spans.util.MtasSpanQuery;
/**
* The Class MtasSpanAndQuery.
*/
public class MtasSpanAndQuery extends MtasSpanQuery {
/** The base query. */
private SpanNearQuery baseQuery;
/** The clauses. */
private HashSet<MtasSpanQuery> clauses;
/**
* Instantiates a new mtas span and query.
*
* @param initialClauses the initial clauses
*/
public MtasSpanAndQuery(MtasSpanQuery... initialClauses) {
<span class="fc" id="L30"> super(null, null);</span>
<span class="fc" id="L31"> Integer minimum = null;</span>
<span class="fc" id="L32"> Integer maximum = null;</span>
<span class="fc" id="L33"> clauses = new HashSet<>();</span>
<span class="fc bfc" id="L34" title="All 2 branches covered."> for (MtasSpanQuery item : initialClauses) {</span>
<span class="pc bpc" id="L35" title="1 of 2 branches missed."> if (!clauses.contains(item)) {</span>
<span class="fc" id="L36"> clauses.add(item);</span>
<span class="pc bpc" id="L37" title="1 of 2 branches missed."> if (item.getMinimumWidth() != null) {</span>
<span class="fc bfc" id="L38" title="All 2 branches covered."> if (minimum != null) {</span>
<span class="fc" id="L39"> minimum = Math.max(minimum, item.getMinimumWidth());</span>
} else {
<span class="fc" id="L41"> minimum = item.getMinimumWidth();</span>
}
}
<span class="pc bpc" id="L44" title="1 of 2 branches missed."> if (item.getMaximumWidth() != null) {</span>
<span class="fc bfc" id="L45" title="All 2 branches covered."> if (maximum != null) {</span>
<span class="fc" id="L46"> maximum = Math.max(maximum, item.getMaximumWidth());</span>
} else {
<span class="fc" id="L48"> maximum = item.getMaximumWidth();</span>
}
}
}
}
<span class="fc" id="L53"> setWidth(minimum, maximum);</span>
<span class="fc" id="L54"> baseQuery = new MtasExtendedSpanAndQuery(</span>
<span class="fc" id="L55"> clauses.toArray(new MtasSpanQuery[clauses.size()]));</span>
<span class="fc" id="L56"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanQuery#getField()
*/
@Override
public String getField() {
<span class="fc" id="L65"> return baseQuery.getField();</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, float boost)
throws IOException {
<span class="fc" id="L78"> return baseQuery.createWeight(searcher, needsScores, boost);</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="pc bpc" id="L89" title="1 of 2 branches missed."> if (clauses.size() > 1) {</span>
// rewrite, count MtasSpanMatchAllQuery and check for
// MtasSpanMatchNoneQuery
<span class="fc" id="L92"> MtasSpanQuery[] newClauses = new MtasSpanQuery[clauses.size()];</span>
<span class="fc" id="L93"> MtasSpanQuery[] oldClauses = clauses</span>
<span class="fc" id="L94"> .toArray(new MtasSpanQuery[clauses.size()]);</span>
<span class="fc" id="L95"> int singlePositionQueries = 0;</span>
<span class="fc" id="L96"> int matchAllSinglePositionQueries = 0;</span>
<span class="fc" id="L97"> boolean actuallyRewritten = false;</span>
<span class="fc bfc" id="L98" title="All 2 branches covered."> for (int i = 0; i < oldClauses.length; i++) {</span>
<span class="fc" id="L99"> newClauses[i] = oldClauses[i].rewrite(reader);</span>
<span class="fc bfc" id="L100" title="All 2 branches covered."> actuallyRewritten |= !oldClauses[i].equals(newClauses[i]);</span>
<span class="pc bpc" id="L101" title="1 of 2 branches missed."> if (newClauses[i] instanceof MtasSpanMatchNoneQuery) {</span>
<span class="nc" id="L102"> return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
} else {
<span class="pc bpc" id="L104" title="1 of 2 branches missed."> if (newClauses[i].isSinglePositionQuery()) {</span>
<span class="fc" id="L105"> singlePositionQueries++;</span>
<span class="pc bpc" id="L106" title="1 of 2 branches missed."> if (newClauses[i] instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L107"> matchAllSinglePositionQueries++;</span>
}
}
}
}
// filter clauses
<span class="pc bpc" id="L113" title="1 of 2 branches missed."> if (matchAllSinglePositionQueries > 0) {</span>
// compute new number of clauses
<span class="nc" id="L115"> int newNumber = newClauses.length - matchAllSinglePositionQueries;</span>
<span class="nc bnc" id="L116" title="All 2 branches missed."> if (matchAllSinglePositionQueries == singlePositionQueries) {</span>
<span class="nc" id="L117"> newNumber++;</span>
}
<span class="nc" id="L119"> MtasSpanQuery[] newFilteredClauses = new MtasSpanQuery[newNumber];</span>
<span class="nc" id="L120"> int j = 0;</span>
<span class="nc bnc" id="L121" title="All 2 branches missed."> for (int i = 0; i < newClauses.length; i++) {</span>
<span class="nc bnc" id="L122" title="All 4 branches missed."> if (!(newClauses[i].isSinglePositionQuery()</span>
&& (newClauses[i] instanceof MtasSpanMatchAllQuery))) {
<span class="nc" id="L124"> newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L125"> j++;</span>
<span class="nc bnc" id="L126" title="All 2 branches missed."> } else if (matchAllSinglePositionQueries == singlePositionQueries) {</span>
<span class="nc" id="L127"> newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L128"> j++;</span>
<span class="nc" id="L129"> singlePositionQueries++; // only match this condition once</span>
}
}
<span class="nc" id="L132"> newClauses = newFilteredClauses;</span>
}
<span class="pc bpc" id="L134" title="1 of 2 branches missed."> if (newClauses.length == 0) {</span>
<span class="nc" id="L135"> return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
<span class="pc bpc" id="L136" title="1 of 2 branches missed."> } else if (newClauses.length == 1) {</span>
<span class="nc" id="L137"> return newClauses[0].rewrite(reader);</span>
<span class="pc bpc" id="L138" title="1 of 4 branches missed."> } else if (actuallyRewritten || newClauses.length != clauses.size()) {</span>
<span class="fc" id="L139"> return new MtasSpanAndQuery(newClauses).rewrite(reader);</span>
} else {
<span class="fc" id="L141"> return super.rewrite(reader);</span>
}
<span class="nc bnc" id="L143" title="All 2 branches missed."> } else if (clauses.size() == 1) {</span>
<span class="nc" id="L144"> return clauses.iterator().next().rewrite(reader);</span>
} else {
<span class="nc" id="L146"> return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
}
}
/*
* (non-Javadoc)
*
* @see
* org.apache.lucene.search.spans.SpanNearQuery#toString(java.lang.String)
*/
@Override
public String toString(String field) {
<span class="nc" id="L158"> return baseQuery.toString(field);</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanNearQuery#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
<span class="fc bfc" id="L168" title="All 2 branches covered."> if (this == obj)</span>
<span class="fc" id="L169"> return true;</span>
<span class="pc bpc" id="L170" title="1 of 2 branches missed."> if (obj == null)</span>
<span class="nc" id="L171"> return false;</span>
<span class="pc bpc" id="L172" title="1 of 2 branches missed."> if (getClass() != obj.getClass())</span>
<span class="nc" id="L173"> return false;</span>
<span class="fc" id="L174"> final MtasSpanAndQuery that = (MtasSpanAndQuery) obj;</span>
<span class="fc" id="L175"> return baseQuery.equals(that.baseQuery);</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.SpanNearQuery#hashCode()
*/
@Override
public int hashCode() {
<span class="fc" id="L185"> int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L186"> h = (h * 7) ^ clauses.hashCode();</span>
<span class="fc" id="L187"> return h;</span>
}
/*
* (non-Javadoc)
*
* @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
*/
@Override
public void disableTwoPhaseIterator() {
<span class="nc" id="L197"> super.disableTwoPhaseIterator();</span>
<span class="nc bnc" id="L198" title="All 2 branches missed."> for (MtasSpanQuery item : clauses) {</span>
<span class="nc" id="L199"> item.disableTwoPhaseIterator();</span>
<span class="nc" id="L200"> }</span>
<span class="nc" id="L201"> }</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>