64
64
import java .util .Map ;
65
65
import java .util .function .BiFunction ;
66
66
import java .util .function .Supplier ;
67
+ import java .util .stream .Collectors ;
67
68
68
69
/**
69
70
* A {@link FieldMapper} for ip addresses.
@@ -225,42 +226,37 @@ protected Object parseSourceValue(Object value) {
225
226
@ Override
226
227
public Query termQuery (Object value , @ Nullable QueryShardContext context ) {
227
228
failIfNotIndexedAndNoDocValues ();
228
- Query query ;
229
+ final PointRangeQuery pointQuery ;
229
230
if (value instanceof InetAddress ) {
230
- query = InetAddressPoint .newExactQuery (name (), (InetAddress ) value );
231
+ pointQuery = ( PointRangeQuery ) InetAddressPoint .newExactQuery (name (), (InetAddress ) value );
231
232
} else {
232
233
if (value instanceof BytesRef ) {
233
234
value = ((BytesRef ) value ).utf8ToString ();
234
235
}
235
236
String term = value .toString ();
236
237
if (term .contains ("/" )) {
237
238
final Tuple <InetAddress , Integer > cidr = InetAddresses .parseCidr (term );
238
- query = InetAddressPoint .newPrefixQuery (name (), cidr .v1 (), cidr .v2 ());
239
+ pointQuery = ( PointRangeQuery ) InetAddressPoint .newPrefixQuery (name (), cidr .v1 (), cidr .v2 ());
239
240
} else {
240
241
InetAddress address = InetAddresses .forString (term );
241
- query = InetAddressPoint .newExactQuery (name (), address );
242
+ pointQuery = ( PointRangeQuery ) InetAddressPoint .newExactQuery (name (), address );
242
243
}
243
244
}
244
- if (isSearchable () && hasDocValues ()) {
245
- String term = value .toString ();
246
- if (term .contains ("/" )) {
247
- final Tuple <InetAddress , Integer > cidr = InetAddresses .parseCidr (term );
248
- return InetAddressPoint .newPrefixQuery (name (), cidr .v1 (), cidr .v2 ());
249
- }
250
- return new IndexOrDocValuesQuery (
251
- query ,
252
- SortedSetDocValuesField .newSlowExactQuery (name (), new BytesRef (((PointRangeQuery ) query ).getLowerPoint ()))
245
+ Query dvQuery = null ;
246
+ if (hasDocValues ()) {
247
+ dvQuery = SortedSetDocValuesField .newSlowRangeQuery (
248
+ name (),
249
+ new BytesRef (pointQuery .getLowerPoint ()),
250
+ new BytesRef (pointQuery .getUpperPoint ()),
251
+ true ,
252
+ true
253
253
);
254
254
}
255
- if (hasDocValues ()) {
256
- String term = value .toString ();
257
- if (term .contains ("/" )) {
258
- final Tuple <InetAddress , Integer > cidr = InetAddresses .parseCidr (term );
259
- return InetAddressPoint .newPrefixQuery (name (), cidr .v1 (), cidr .v2 ());
260
- }
261
- return SortedSetDocValuesField .newSlowExactQuery (name (), new BytesRef (((PointRangeQuery ) query ).getLowerPoint ()));
255
+ if (isSearchable () && hasDocValues ()) {
256
+ return new IndexOrDocValuesQuery (pointQuery , dvQuery );
257
+ } else {
258
+ return isSearchable () ? pointQuery : dvQuery ;
262
259
}
263
- return query ;
264
260
}
265
261
266
262
@ Override
@@ -285,36 +281,46 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
285
281
}
286
282
addresses [i ++] = address ;
287
283
}
288
- return InetAddressPoint .newSetQuery (name (), addresses );
284
+ Query dvQuery = null ;
285
+ if (hasDocValues ()) {
286
+ List <BytesRef > bytesRefs = Arrays .stream (addresses )
287
+ .distinct ()
288
+ .map (InetAddressPoint ::encode )
289
+ .map (BytesRef ::new )
290
+ .collect (Collectors .<BytesRef >toList ());
291
+ dvQuery = SortedSetDocValuesField .newSlowSetQuery (name (), bytesRefs );
292
+ }
293
+ Query pointQuery = null ;
294
+ if (isSearchable ()) {
295
+ pointQuery = InetAddressPoint .newSetQuery (name (), addresses );
296
+ }
297
+ if (isSearchable () && hasDocValues ()) {
298
+ return new IndexOrDocValuesQuery (pointQuery , dvQuery );
299
+ } else {
300
+ return isSearchable () ? pointQuery : dvQuery ;
301
+ }
289
302
}
290
303
291
304
@ Override
292
305
public Query rangeQuery (Object lowerTerm , Object upperTerm , boolean includeLower , boolean includeUpper , QueryShardContext context ) {
293
306
failIfNotIndexedAndNoDocValues ();
294
307
return rangeQuery (lowerTerm , upperTerm , includeLower , includeUpper , (lower , upper ) -> {
295
- Query query = InetAddressPoint .newRangeQuery (name (), lower , upper );
296
- if (isSearchable () && hasDocValues ()) {
297
- return new IndexOrDocValuesQuery (
298
- query ,
299
- SortedSetDocValuesField .newSlowRangeQuery (
300
- ((PointRangeQuery ) query ).getField (),
301
- new BytesRef (((PointRangeQuery ) query ).getLowerPoint ()),
302
- new BytesRef (((PointRangeQuery ) query ).getUpperPoint ()),
303
- true ,
304
- true
305
- )
306
- );
307
- }
308
+ PointRangeQuery pointQuery = (PointRangeQuery ) InetAddressPoint .newRangeQuery (name (), lower , upper );
309
+ Query dvQuery = null ;
308
310
if (hasDocValues ()) {
309
- return SortedSetDocValuesField .newSlowRangeQuery (
310
- (( PointRangeQuery ) query ) .getField (),
311
- new BytesRef ((( PointRangeQuery ) query ) .getLowerPoint ()),
312
- new BytesRef ((( PointRangeQuery ) query ) .getUpperPoint ()),
311
+ dvQuery = SortedSetDocValuesField .newSlowRangeQuery (
312
+ pointQuery .getField (),
313
+ new BytesRef (pointQuery .getLowerPoint ()),
314
+ new BytesRef (pointQuery .getUpperPoint ()),
313
315
true ,
314
316
true
315
317
);
316
318
}
317
- return query ;
319
+ if (isSearchable () && hasDocValues ()) {
320
+ return new IndexOrDocValuesQuery (pointQuery , dvQuery );
321
+ } else {
322
+ return isSearchable () ? pointQuery : dvQuery ;
323
+ }
318
324
});
319
325
}
320
326
0 commit comments