36
36
import org .apache .lucene .document .SortedSetDocValuesField ;
37
37
import org .apache .lucene .document .StoredField ;
38
38
import org .apache .lucene .index .SortedSetDocValues ;
39
+ import org .apache .lucene .search .IndexOrDocValuesQuery ;
39
40
import org .apache .lucene .search .MatchNoDocsQuery ;
41
+ import org .apache .lucene .search .PointRangeQuery ;
40
42
import org .apache .lucene .search .Query ;
41
43
import org .apache .lucene .util .ArrayUtil ;
42
44
import org .apache .lucene .util .BytesRef ;
@@ -222,25 +224,50 @@ protected Object parseSourceValue(Object value) {
222
224
223
225
@ Override
224
226
public Query termQuery (Object value , @ Nullable QueryShardContext context ) {
225
- failIfNotIndexed ();
227
+ failIfNotIndexedAndNoDocValues ();
228
+ Query query ;
226
229
if (value instanceof InetAddress ) {
227
- return InetAddressPoint .newExactQuery (name (), (InetAddress ) value );
230
+ query = InetAddressPoint .newExactQuery (name (), (InetAddress ) value );
228
231
} else {
229
232
if (value instanceof BytesRef ) {
230
233
value = ((BytesRef ) value ).utf8ToString ();
231
234
}
232
235
String term = value .toString ();
233
236
if (term .contains ("/" )) {
234
237
final Tuple <InetAddress , Integer > cidr = InetAddresses .parseCidr (term );
235
- return InetAddressPoint .newPrefixQuery (name (), cidr .v1 (), cidr .v2 ());
238
+ query = InetAddressPoint .newPrefixQuery (name (), cidr .v1 (), cidr .v2 ());
236
239
}
237
240
InetAddress address = InetAddresses .forString (term );
238
- return InetAddressPoint .newExactQuery (name (), address );
241
+ query = InetAddressPoint .newExactQuery (name (), address );
239
242
}
243
+ if (isSearchable () && hasDocValues ()) {
244
+ return new IndexOrDocValuesQuery (
245
+ query ,
246
+ SortedSetDocValuesField .newSlowRangeQuery (
247
+ ((PointRangeQuery ) query ).getField (),
248
+ new BytesRef (((PointRangeQuery ) query ).getLowerPoint ()),
249
+ new BytesRef (((PointRangeQuery ) query ).getUpperPoint ()),
250
+ true ,
251
+ true
252
+ )
253
+ );
254
+ }
255
+ if (hasDocValues ()) {
256
+ // InetAddressPoint uses a rangeQuery internally for terms
257
+ return SortedSetDocValuesField .newSlowRangeQuery (
258
+ ((PointRangeQuery ) query ).getField (),
259
+ new BytesRef (((PointRangeQuery ) query ).getLowerPoint ()),
260
+ new BytesRef (((PointRangeQuery ) query ).getUpperPoint ()),
261
+ true ,
262
+ true
263
+ );
264
+ }
265
+ return query ;
240
266
}
241
267
242
268
@ Override
243
269
public Query termsQuery (List <?> values , QueryShardContext context ) {
270
+ failIfNotIndexedAndNoDocValues ();
244
271
InetAddress [] addresses = new InetAddress [values .size ()];
245
272
int i = 0 ;
246
273
for (Object value : values ) {
@@ -265,14 +292,32 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
265
292
266
293
@ Override
267
294
public Query rangeQuery (Object lowerTerm , Object upperTerm , boolean includeLower , boolean includeUpper , QueryShardContext context ) {
268
- failIfNotIndexed ();
269
- return rangeQuery (
270
- lowerTerm ,
271
- upperTerm ,
272
- includeLower ,
273
- includeUpper ,
274
- (lower , upper ) -> InetAddressPoint .newRangeQuery (name (), lower , upper )
275
- );
295
+ failIfNotIndexedAndNoDocValues ();
296
+ return rangeQuery (lowerTerm , upperTerm , includeLower , includeUpper , (lower , upper ) -> {
297
+ Query query = InetAddressPoint .newRangeQuery (name (), lower , upper );
298
+ if (isSearchable () && hasDocValues ()) {
299
+ return new IndexOrDocValuesQuery (
300
+ query ,
301
+ SortedSetDocValuesField .newSlowRangeQuery (
302
+ ((PointRangeQuery ) query ).getField (),
303
+ new BytesRef (((PointRangeQuery ) query ).getLowerPoint ()),
304
+ new BytesRef (((PointRangeQuery ) query ).getUpperPoint ()),
305
+ true ,
306
+ true
307
+ )
308
+ );
309
+ }
310
+ if (hasDocValues ()) {
311
+ return SortedSetDocValuesField .newSlowRangeQuery (
312
+ ((PointRangeQuery ) query ).getField (),
313
+ new BytesRef (((PointRangeQuery ) query ).getLowerPoint ()),
314
+ new BytesRef (((PointRangeQuery ) query ).getUpperPoint ()),
315
+ true ,
316
+ true
317
+ );
318
+ }
319
+ return query ;
320
+ });
276
321
}
277
322
278
323
/**
0 commit comments