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 c9b6f07e84e40..c1db6b0415519 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java @@ -243,15 +243,20 @@ public Query termQuery(Object value, @Nullable QueryShardContext context) { } if (isSearchable() && hasDocValues()) { String term = value.toString(); - if (value instanceof String) - value = new BytesRef(((String) value).getBytes()); + if (value instanceof String) value = new BytesRef(((String) value)); if (term.contains("/")) { final Tuple cidr = InetAddresses.parseCidr(term); return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2()); } - return new IndexOrDocValuesQuery(query, SortedSetDocValuesField.newSlowExactQuery(name(), (BytesRef)(value))); + return new IndexOrDocValuesQuery(query, SortedSetDocValuesField.newSlowExactQuery(name(), (BytesRef) (value))); } if (hasDocValues()) { + String term = value.toString(); + if (value instanceof String) value = new BytesRef(((String) value)); + if (term.contains("/")) { + final Tuple cidr = InetAddresses.parseCidr(term); + return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2()); + } return SortedSetDocValuesField.newSlowExactQuery(name(), ((BytesRef) value)); } return query; diff --git a/server/src/test/java/org/opensearch/index/mapper/IpFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/IpFieldTypeTests.java index ddf3fc45c11ef..e848700850800 100644 --- a/server/src/test/java/org/opensearch/index/mapper/IpFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/IpFieldTypeTests.java @@ -83,20 +83,14 @@ public void testTermQuery() { Query query = InetAddressPoint.newExactQuery("field", InetAddresses.forString(ip)); assertEquals( - new IndexOrDocValuesQuery( - query, - SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef(ip)) - ), + new IndexOrDocValuesQuery(query, SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef(ip))), ft.termQuery(ip, null) ); ip = "192.168.1.7"; query = InetAddressPoint.newExactQuery("field", InetAddresses.forString(ip)); assertEquals( - new IndexOrDocValuesQuery( - query, - SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef(ip)) - ), + new IndexOrDocValuesQuery(query, SortedSetDocValuesField.newSlowExactQuery("field", new BytesRef(ip))), ft.termQuery(ip, null) ); @@ -104,18 +98,12 @@ public void testTermQuery() { String prefix = ip + "/64"; query = InetAddressPoint.newPrefixQuery("field", InetAddresses.forString(ip), 64); - assertEquals( - query, - ft.termQuery(prefix, null) - ); + assertEquals(query, ft.termQuery(prefix, null)); ip = "192.168.1.7"; prefix = ip + "/16"; query = InetAddressPoint.newPrefixQuery("field", InetAddresses.forString(ip), 16); - assertEquals( - query, - ft.termQuery(prefix, null) - ); + assertEquals(query, ft.termQuery(prefix, null)); MappedFieldType unsearchable = new IpFieldMapper.IpFieldType("field", false, false, false, null, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termQuery("::1", null));