diff --git a/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java index a6118736d615a..8336b09bdb818 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java @@ -252,7 +252,10 @@ public Query termQuery(Object value, @Nullable QueryShardContext context) { true ); } + return indexOrDvQuery(pointQuery, dvQuery); + } + private Query indexOrDvQuery(Query pointQuery, Query dvQuery) { if (isSearchable() && hasDocValues()) { return new IndexOrDocValuesQuery(pointQuery, dvQuery); } else { @@ -299,39 +302,25 @@ public Query termsQuery(List values, QueryShardContext context) { if (isSearchable()) { pointQuery = InetAddressPoint.newSetQuery(name(), addresses); } - if (isSearchable() && hasDocValues()) { - return new IndexOrDocValuesQuery(pointQuery, dvQuery); - } else { - if (isSearchable()) { - return pointQuery; - } else { - return dvQuery; - } - } + return indexOrDvQuery(pointQuery, dvQuery); } @Override public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) { failIfNotIndexedAndNoDocValues(); return rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, (lower, upper) -> { - Query query = InetAddressPoint.newRangeQuery(name(), lower, upper); + PointRangeQuery pointQuery = (PointRangeQuery) InetAddressPoint.newRangeQuery(name(), lower, upper); Query dvQuery = null; if (hasDocValues()) { dvQuery = SortedSetDocValuesField.newSlowRangeQuery( - ((PointRangeQuery) query).getField(), - new BytesRef(((PointRangeQuery) query).getLowerPoint()), - new BytesRef(((PointRangeQuery) query).getUpperPoint()), + pointQuery.getField(), + new BytesRef(pointQuery.getLowerPoint()), + new BytesRef(pointQuery.getUpperPoint()), true, true ); } - if (isSearchable() && hasDocValues()) { - return new IndexOrDocValuesQuery(query, dvQuery); - } - if (hasDocValues()) { - return dvQuery; - } - return query; + return indexOrDvQuery(pointQuery, dvQuery); }); }