MtasSpanTermQuery.java
8.03 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
package mtas.search.spans;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import mtas.analysis.token.MtasToken;
import mtas.codec.util.CodecUtil;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext;
import org.apache.lucene.index.TermState;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.FilterSpans;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.search.spans.SpanWeight;
import org.apache.lucene.search.spans.Spans;
/**
* The Class MtasSpanTermQuery.
*/
public class MtasSpanTermQuery extends SpanTermQuery {
/** The prefix. */
private String prefix;
/** The value. */
private String value;
/** The single position. */
private boolean singlePosition;
/** The term. */
private Term term;
/**
* Instantiates a new mtas span term query.
*
* @param term the term
*/
public MtasSpanTermQuery(Term term) {
this(term, true);
}
/**
* Instantiates a new mtas span term query.
*
* @param term the term
* @param singlePosition the single position
*/
public MtasSpanTermQuery(Term term, boolean singlePosition) {
this(new SpanTermQuery(term), true);
}
/**
* Instantiates a new mtas span term query.
*
* @param query the query
* @param singlePosition the single position
*/
public MtasSpanTermQuery(SpanTermQuery query, boolean singlePosition) {
super(query.getTerm());
term = query.getTerm();
this.singlePosition = singlePosition;
int i = term.text().indexOf(MtasToken.DELIMITER);
if (i >= 0) {
prefix = term.text().substring(0, i);
value = term.text().substring((i + MtasToken.DELIMITER.length()));
value = (value.length() > 0) ? value : null;
} else {
prefix = term.text();
value = null;
}
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.SpanTermQuery#createWeight(org.apache.lucene.search.IndexSearcher, boolean)
*/
@Override
public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores)
throws IOException {
final TermContext context;
final IndexReaderContext topContext = searcher.getTopReaderContext();
if (termContext == null || termContext.topReaderContext != topContext) {
context = TermContext.build(topContext, term);
} else {
context = termContext;
}
return new SpanTermWeight(context, searcher,
needsScores ? Collections.singletonMap(term, context) : null);
}
/**
* The Class SpanTermWeight.
*/
public class SpanTermWeight extends SpanWeight {
/** The term context. */
final TermContext termContext;
/**
* Instantiates a new span term weight.
*
* @param termContext the term context
* @param searcher the searcher
* @param terms the terms
* @throws IOException Signals that an I/O exception has occurred.
*/
public SpanTermWeight(TermContext termContext, IndexSearcher searcher,
Map<Term, TermContext> terms) throws IOException {
super(MtasSpanTermQuery.this, searcher, terms);
this.termContext = termContext;
assert termContext != null : "TermContext must not be null";
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.Weight#extractTerms(java.util.Set)
*/
@Override
public void extractTerms(Set<Term> terms) {
terms.add(term);
}
/*
* (non-Javadoc)
*
* @see
* org.apache.lucene.search.spans.SpanWeight#extractTermContexts(java.util
* .Map)
*/
@Override
public void extractTermContexts(Map<Term, TermContext> contexts) {
contexts.put(term, termContext);
}
/*
* (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(final LeafReaderContext context,
Postings requiredPostings) throws IOException {
assert termContext.topReaderContext == ReaderUtil.getTopLevelContext(
context) : "The top-reader used to create Weight ("
+ termContext.topReaderContext
+ ") is not the same as the current reader's top-reader ("
+ ReaderUtil.getTopLevelContext(context);
final TermState state = termContext.get(context.ord);
if (state == null) { // term is not present in that reader
assert context.reader().docFreq(
term) == 0 : "no termstate found but term exists in reader term="
+ term;
return null;
}
final Terms terms = context.reader().terms(term.field());
if (terms == null)
return null;
if (terms.hasPositions() == false)
throw new IllegalStateException("field \"" + term.field()
+ "\" was indexed without position data; cannot run SpanTermQuery (term="
+ term.text() + ")");
final TermsEnum termsEnum = terms.iterator();
termsEnum.seekExact(term.bytes(), state);
final PostingsEnum postings;
Spans matchSpans;
try {
// get leafreader
LeafReader r = context.reader();
// get delegate
Boolean hasMethod = true;
while (hasMethod) {
hasMethod = false;
Method[] methods = r.getClass().getMethods();
for (Method m : methods) {
if (m.getName().equals("getDelegate")) {
hasMethod = true;
r = (LeafReader) m.invoke(r, (Object[]) null);
break;
}
}
}
FieldInfo fieldInfo = r.getFieldInfos().fieldInfo(field);
if (CodecUtil.isSinglePositionPrefix(fieldInfo, prefix)) {
postings = termsEnum.postings(null,
requiredPostings.getRequiredPostings());
matchSpans = new MtasTermSpans(postings, term, true);
} else {
postings = termsEnum.postings(null, requiredPostings
.atLeast(Postings.PAYLOADS).getRequiredPostings());
matchSpans = new MtasTermSpans(postings, term, false);
}
return (matchSpans == null) ? null
: singlePosition ? new FilterSpans(matchSpans) {
@Override
protected AcceptStatus accept(Spans candidate)
throws IOException {
assert candidate.startPosition() != candidate.endPosition();
AcceptStatus res = ((candidate.endPosition()
- candidate.startPosition()) == 1) ? AcceptStatus.YES
: AcceptStatus.NO;
return res;
}
} : matchSpans;
} catch (Exception e) {
// e.printStackTrace();
throw new IOException("Can't get reader: " + e.getMessage());
}
}
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.SpanTermQuery#toString(java.lang.String)
*/
@Override
public String toString(String field) {
StringBuilder buffer = new StringBuilder();
buffer.append("mtasSpanTermQuery([");
if (value == null) {
buffer.append(field + ":" + prefix);
} else {
buffer.append(field + ":" + prefix + "=" + value);
}
buffer.append("])");
return buffer.toString();
}
/* (non-Javadoc)
* @see org.apache.lucene.search.spans.SpanTermQuery#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MtasSpanTermQuery other = (MtasSpanTermQuery) obj;
return other.term.equals(term) && (other.singlePosition != singlePosition);
}
}