3636import org .apache .lucene .document .SortedSetDocValuesField ;
3737import org .apache .lucene .document .StoredField ;
3838import org .apache .lucene .index .SortedSetDocValues ;
39+ import org .apache .lucene .search .IndexOrDocValuesQuery ;
3940import org .apache .lucene .search .MatchNoDocsQuery ;
41+ import org .apache .lucene .search .PointRangeQuery ;
4042import org .apache .lucene .search .Query ;
4143import org .apache .lucene .util .ArrayUtil ;
4244import org .apache .lucene .util .BytesRef ;
@@ -222,25 +224,50 @@ protected Object parseSourceValue(Object value) {
222224
223225 @ Override
224226 public Query termQuery (Object value , @ Nullable QueryShardContext context ) {
225- failIfNotIndexed ();
227+ failIfNotIndexedAndNoDocValues ();
228+ Query query ;
226229 if (value instanceof InetAddress ) {
227- return InetAddressPoint .newExactQuery (name (), (InetAddress ) value );
230+ query = InetAddressPoint .newExactQuery (name (), (InetAddress ) value );
228231 } else {
229232 if (value instanceof BytesRef ) {
230233 value = ((BytesRef ) value ).utf8ToString ();
231234 }
232235 String term = value .toString ();
233236 if (term .contains ("/" )) {
234237 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 ());
236239 }
237240 InetAddress address = InetAddresses .forString (term );
238- return InetAddressPoint .newExactQuery (name (), address );
241+ query = InetAddressPoint .newExactQuery (name (), address );
239242 }
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 ;
240266 }
241267
242268 @ Override
243269 public Query termsQuery (List <?> values , QueryShardContext context ) {
270+ failIfNotIndexedAndNoDocValues ();
244271 InetAddress [] addresses = new InetAddress [values .size ()];
245272 int i = 0 ;
246273 for (Object value : values ) {
@@ -265,14 +292,32 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
265292
266293 @ Override
267294 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+ });
276321 }
277322
278323 /**
0 commit comments