forked from FoundationDB/fdb-record-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuceneQueryFieldComparisonClause.java
388 lines (362 loc) · 17.8 KB
/
LuceneQueryFieldComparisonClause.java
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
/*
* LuceneQueryFieldComparisonClause.java
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021-2022 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.apple.foundationdb.record.lucene;
import com.apple.foundationdb.annotation.API;
import com.apple.foundationdb.record.EvaluationContext;
import com.apple.foundationdb.record.PlanHashable;
import com.apple.foundationdb.record.RecordCoreArgumentException;
import com.apple.foundationdb.record.RecordCoreException;
import com.apple.foundationdb.record.metadata.Index;
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase;
import com.apple.foundationdb.record.query.expressions.Comparisons;
import com.apple.foundationdb.record.query.plan.cascades.explain.Attribute;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import javax.annotation.Nonnull;
import java.util.List;
/**
* Query clause using a {@link Comparisons.Comparison} against a document field.
*/
@API(API.Status.UNSTABLE)
public abstract class LuceneQueryFieldComparisonClause extends LuceneQueryClause {
@Nonnull
protected final String field;
@Nonnull
protected final LuceneIndexExpressions.DocumentFieldType fieldType;
@Nonnull
protected final Comparisons.Comparison comparison;
protected LuceneQueryFieldComparisonClause(@Nonnull String field, @Nonnull LuceneIndexExpressions.DocumentFieldType fieldType, @Nonnull Comparisons.Comparison comparison) {
this.field = field;
this.fieldType = fieldType;
this.comparison = comparison;
}
@Nonnull
public String getField() {
return field;
}
@Nonnull
public LuceneIndexExpressions.DocumentFieldType getFieldType() {
return fieldType;
}
@Nonnull
public Comparisons.Comparison getComparison() {
return comparison;
}
@Override
public void getPlannerGraphDetails(@Nonnull final ImmutableList.Builder<String> detailsBuilder, @Nonnull final ImmutableMap.Builder<String, Attribute> attributeMapBuilder) {
detailsBuilder.add("field: {{field}}");
attributeMapBuilder.put("field", Attribute.gml(field));
detailsBuilder.add("fieldType: {{fieldType}}");
attributeMapBuilder.put("fieldType", Attribute.gml(fieldType));
detailsBuilder.add("comparisonType: {{comparisonType}}");
attributeMapBuilder.put("comparisonType", Attribute.gml(comparison.getType()));
detailsBuilder.add("comparison: {{comparison}}");
attributeMapBuilder.put("comparison", Attribute.gml(comparison.typelessString()));
}
@Override
public int planHash(@Nonnull final PlanHashKind hashKind) {
return PlanHashable.objectsPlanHash(hashKind, field, fieldType, comparison);
}
@Override
public String toString() {
return field + ":" + fieldType + " " + comparison;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final LuceneQueryFieldComparisonClause that = (LuceneQueryFieldComparisonClause)o;
if (!field.equals(that.field)) {
return false;
}
if (fieldType != that.fieldType) {
return false;
}
return comparison.equals(that.comparison);
}
@Override
public int hashCode() {
int result = field.hashCode();
result = 31 * result + fieldType.hashCode();
result = 31 * result + comparison.hashCode();
return result;
}
@Nonnull
@SuppressWarnings("fallthrough")
public static LuceneQueryFieldComparisonClause create(@Nonnull String field, @Nonnull LuceneIndexExpressions.DocumentFieldType fieldType, @Nonnull Comparisons.Comparison comparison) {
switch (comparison.getType()) {
case NOT_NULL:
case IS_NULL:
return new NullQuery(field, fieldType, comparison);
case EQUALS:
case NOT_EQUALS:
case LESS_THAN:
case LESS_THAN_OR_EQUALS:
case GREATER_THAN:
case GREATER_THAN_OR_EQUALS:
case IN:
break;
case STARTS_WITH:
if (fieldType == LuceneIndexExpressions.DocumentFieldType.STRING) {
break;
}
/* else falls through */
case TEXT_CONTAINS_ALL:
case TEXT_CONTAINS_ANY:
case TEXT_CONTAINS_PHRASE:
case TEXT_CONTAINS_PREFIX:
case TEXT_CONTAINS_ALL_PREFIXES:
case TEXT_CONTAINS_ANY_PREFIX:
if (fieldType == LuceneIndexExpressions.DocumentFieldType.TEXT) {
break;
}
/* else falls through */
default:
throw new RecordCoreException("comparison type not supported for Lucene: " + comparison.getType());
}
switch (fieldType) {
case STRING:
case TEXT:
return new StringQuery(field, fieldType, comparison);
case INT:
return new IntQuery(field, fieldType, comparison);
case LONG:
return new LongQuery(field, fieldType, comparison);
case DOUBLE:
return new DoubleQuery(field, fieldType, comparison);
default:
throw new RecordCoreException("unsupported Lucene index field type: " + fieldType);
}
}
static class NullQuery extends LuceneQueryFieldComparisonClause {
public NullQuery(@Nonnull String field, @Nonnull LuceneIndexExpressions.DocumentFieldType fieldType, @Nonnull Comparisons.Comparison comparison) {
super(field, fieldType, comparison);
}
@Override
public Query bind(@Nonnull FDBRecordStoreBase<?> store, @Nonnull Index index, @Nonnull EvaluationContext context) {
Query allValues = new TermRangeQuery(field, null, null, true, true);
if (comparison.getType() == Comparisons.Type.NOT_NULL) {
return allValues;
} else {
// *:* -f[* TO *]
return LuceneNotQuery.negate(allValues);
}
}
}
static class StringQuery extends LuceneQueryFieldComparisonClause {
public StringQuery(@Nonnull String field, @Nonnull LuceneIndexExpressions.DocumentFieldType fieldType, @Nonnull Comparisons.Comparison comparison) {
super(field, fieldType, comparison);
}
@Override
@SuppressWarnings("unchecked")
public Query bind(@Nonnull FDBRecordStoreBase<?> store, @Nonnull Index index, @Nonnull EvaluationContext context) {
Object comparand = comparison.getComparand(store, context);
if (comparand == null) {
return new MatchNoDocsQuery();
}
switch (comparison.getType()) {
case EQUALS:
return new TermQuery(new Term(field, (String)comparand));
case NOT_EQUALS: {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(TermRangeQuery.newStringRange(field, null, (String)comparand, true, false), BooleanClause.Occur.SHOULD);
builder.add(TermRangeQuery.newStringRange(field, (String)comparand, null, false, true), BooleanClause.Occur.SHOULD);
return builder.build();
}
case LESS_THAN:
return TermRangeQuery.newStringRange(field, null, (String)comparand, true, false);
case LESS_THAN_OR_EQUALS:
return TermRangeQuery.newStringRange(field, null, (String)comparand, true, true);
case GREATER_THAN:
return TermRangeQuery.newStringRange(field, (String)comparand, null, false, true);
case GREATER_THAN_OR_EQUALS:
return TermRangeQuery.newStringRange(field, (String)comparand, null, true, true);
case STARTS_WITH:
case TEXT_CONTAINS_PREFIX:
return new PrefixQuery(new Term(field, (String)comparand));
case TEXT_CONTAINS_PHRASE:
// PhraseQuery will require tokenizing, so may as well just use parser.
try {
final Pair<AnalyzerChooser, AnalyzerChooser> analyzerChooserPair = LuceneAnalyzerRegistryImpl.instance().getLuceneAnalyzerChooserPair(index, LuceneAnalyzerType.FULL_TEXT);
final QueryParser parser = new QueryParser(field, analyzerChooserPair.getRight().chooseAnalyzer((String) comparand).getAnalyzer());
return parser.parse("\"" + comparand + "\"");
} catch (Exception ex) {
throw new RecordCoreArgumentException("Unable to parse phrase for query", ex);
}
case IN:
case TEXT_CONTAINS_ANY: {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (String value : ((List<String>)comparand)) {
builder.add(new TermQuery(new Term(field, value)), BooleanClause.Occur.SHOULD);
}
return builder.build();
}
case TEXT_CONTAINS_ALL: {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (String value : ((List<String>)comparand)) {
builder.add(new TermQuery(new Term(field, value)), BooleanClause.Occur.MUST);
}
return builder.build();
}
case TEXT_CONTAINS_ALL_PREFIXES: {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (String value : ((List<String>)comparand)) {
builder.add(new PrefixQuery(new Term(field, value)), BooleanClause.Occur.MUST);
}
return builder.build();
}
case TEXT_CONTAINS_ANY_PREFIX: {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (String value : ((List<String>)comparand)) {
builder.add(new PrefixQuery(new Term(field, value)), BooleanClause.Occur.SHOULD);
}
return builder.build();
}
default:
throw new RecordCoreException("comparison type not supported for String: " + comparison.getType());
}
}
}
static class IntQuery extends LuceneQueryFieldComparisonClause {
public IntQuery(@Nonnull String field, @Nonnull LuceneIndexExpressions.DocumentFieldType fieldType, @Nonnull Comparisons.Comparison comparison) {
super(field, fieldType, comparison);
}
@Override
@SuppressWarnings("unchecked")
public Query bind(@Nonnull FDBRecordStoreBase<?> store, @Nonnull Index index, @Nonnull EvaluationContext context) {
Object comparand = comparison.getComparand(store, context);
if (comparand == null) {
return new MatchNoDocsQuery();
}
switch (comparison.getType()) {
case EQUALS:
return IntPoint.newExactQuery(field, (Integer)comparand);
case NOT_EQUALS: {
int value = (Integer)comparand;
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(IntPoint.newRangeQuery(field, Integer.MIN_VALUE, value - 1), BooleanClause.Occur.SHOULD);
builder.add(IntPoint.newRangeQuery(field, value + 1, Integer.MAX_VALUE), BooleanClause.Occur.SHOULD);
return builder.build();
}
case LESS_THAN:
return IntPoint.newRangeQuery(field, Integer.MIN_VALUE, (Integer)comparand - 1);
case LESS_THAN_OR_EQUALS:
return IntPoint.newRangeQuery(field, Integer.MIN_VALUE, (Integer)comparand);
case GREATER_THAN:
return IntPoint.newRangeQuery(field, (Integer)comparand + 1, Integer.MAX_VALUE);
case GREATER_THAN_OR_EQUALS:
return IntPoint.newRangeQuery(field, (Integer)comparand, Integer.MAX_VALUE);
case IN:
return IntPoint.newSetQuery(field, ((List<Integer>)comparand));
default:
throw new RecordCoreException("comparison type not supported for Integer: " + comparison.getType());
}
}
}
static class LongQuery extends LuceneQueryFieldComparisonClause {
public LongQuery(@Nonnull String field, @Nonnull LuceneIndexExpressions.DocumentFieldType fieldType, @Nonnull Comparisons.Comparison comparison) {
super(field, fieldType, comparison);
}
@Override
@SuppressWarnings("unchecked")
public Query bind(@Nonnull FDBRecordStoreBase<?> store, @Nonnull Index index, @Nonnull EvaluationContext context) {
Object comparand = comparison.getComparand(store, context);
if (comparand == null) {
return new MatchNoDocsQuery();
}
switch (comparison.getType()) {
case EQUALS:
return LongPoint.newExactQuery(field, (Long)comparand);
case NOT_EQUALS: {
long value = (Long)comparand;
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(LongPoint.newRangeQuery(field, Long.MIN_VALUE, value - 1), BooleanClause.Occur.SHOULD);
builder.add(LongPoint.newRangeQuery(field, value + 1, Long.MAX_VALUE), BooleanClause.Occur.SHOULD);
return builder.build();
}
case LESS_THAN:
return LongPoint.newRangeQuery(field, Long.MIN_VALUE, (Long)comparand - 1);
case LESS_THAN_OR_EQUALS:
return LongPoint.newRangeQuery(field, Long.MIN_VALUE, (Long)comparand);
case GREATER_THAN:
return LongPoint.newRangeQuery(field, (Long)comparand + 1, Long.MAX_VALUE);
case GREATER_THAN_OR_EQUALS:
return LongPoint.newRangeQuery(field, (Long)comparand, Long.MAX_VALUE);
case IN:
return LongPoint.newSetQuery(field, ((List<Long>)comparand));
default:
throw new RecordCoreException("comparison type not supported for Long: " + comparison.getType());
}
}
}
static class DoubleQuery extends LuceneQueryFieldComparisonClause {
public DoubleQuery(@Nonnull String field, @Nonnull LuceneIndexExpressions.DocumentFieldType fieldType, @Nonnull Comparisons.Comparison comparison) {
super(field, fieldType, comparison);
}
@Override
@SuppressWarnings("unchecked")
public Query bind(@Nonnull FDBRecordStoreBase<?> store, @Nonnull Index index, @Nonnull EvaluationContext context) {
Object comparand = comparison.getComparand(store, context);
if (comparand == null) {
return new MatchNoDocsQuery();
}
switch (comparison.getType()) {
case EQUALS:
return DoublePoint.newExactQuery(field, (Double)comparand);
case NOT_EQUALS: {
double value = (Double)comparand;
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(DoublePoint.newRangeQuery(field, Double.MIN_VALUE, value - 1), BooleanClause.Occur.SHOULD);
builder.add(DoublePoint.newRangeQuery(field, value + 1, Double.MAX_VALUE), BooleanClause.Occur.SHOULD);
return builder.build();
}
case LESS_THAN:
return DoublePoint.newRangeQuery(field, Double.MIN_VALUE, (Double)comparand - 1);
case LESS_THAN_OR_EQUALS:
return DoublePoint.newRangeQuery(field, Double.MIN_VALUE, (Double)comparand);
case GREATER_THAN:
return DoublePoint.newRangeQuery(field, (Double)comparand + 1, Double.MAX_VALUE);
case GREATER_THAN_OR_EQUALS:
return DoublePoint.newRangeQuery(field, (Double)comparand, Double.MAX_VALUE);
case IN:
return DoublePoint.newSetQuery(field, ((List<Double>)comparand));
default:
throw new RecordCoreException("comparison type not supported for Double: " + comparison.getType());
}
}
}
}