From 01db87572537eb23944de26eb6b6d4dde12f3337 Mon Sep 17 00:00:00 2001 From: mikhail-khludnev Date: Wed, 13 Nov 2024 11:17:25 +0300 Subject: [PATCH] fix dv only range search add filter Signed-off-by: mikhail-khludnev --- .../index/mapper/IpFieldMapper.java | 9 ++++++++- .../search/SearchIpFieldTermsTests.java | 20 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) 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 026d3846a4973..59d1e25c5712a 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java @@ -265,7 +265,14 @@ public Query termQuery(Object value, @Nullable QueryShardContext context) { String term = value.toString(); if (term.contains("/")) { final Tuple cidr = InetAddresses.parseCidr(term); - return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2()); + PointRangeQuery pointsRange = (PointRangeQuery) InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2()); + return SortedSetDocValuesField.newSlowRangeQuery( + name(), + new BytesRef(pointsRange.getLowerPoint()), + new BytesRef(pointsRange.getUpperPoint()), + true, + true + ); } return SortedSetDocValuesField.newSlowExactQuery(name(), new BytesRef(((PointRangeQuery) query).getLowerPoint())); } diff --git a/server/src/test/java/org/opensearch/search/SearchIpFieldTermsTests.java b/server/src/test/java/org/opensearch/search/SearchIpFieldTermsTests.java index 06dd2e9ba634b..88dc2fd0353cf 100644 --- a/server/src/test/java/org/opensearch/search/SearchIpFieldTermsTests.java +++ b/server/src/test/java/org/opensearch/search/SearchIpFieldTermsTests.java @@ -14,12 +14,14 @@ import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.index.query.QueryBuilders; +import org.opensearch.index.query.TermsQueryBuilder; import org.opensearch.test.OpenSearchSingleNodeTestCase; import org.hamcrest.MatcherAssert; import java.io.IOException; import java.net.InetAddress; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Objects; @@ -90,8 +92,19 @@ public void testMassive() throws Exception { } bulkRequestBuilder.setRefreshPolicy(IMMEDIATE).get(); - SearchResponse result = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.termsQuery("addr", toQuery)).get(); - MatcherAssert.assertThat(Objects.requireNonNull(result.getHits().getTotalHits()).value, equalTo((long) cidrs + ips + addMatches)); + long expectedMatches = (long) cidrs + ips + addMatches; + for (String field : List.of("addr", "addr.idx", "addr.dv")) { + assertTermsHitCount(field, toQuery, expectedMatches); + } + } + + private void assertTermsHitCount(String field, Collection toQuery, long expectedMatches) { + TermsQueryBuilder ipTerms = QueryBuilders.termsQuery(field, new ArrayList<>(toQuery)); + SearchResponse result = client().prepareSearch(defaultIndexName) + .setQuery(QueryBuilders.boolQuery().must(ipTerms).filter(QueryBuilders.termsQuery("dummy_filter", "a", "b"))) + .get(); + long hitsFound = Objects.requireNonNull(result.getHits().getTotalHits()).value; + MatcherAssert.assertThat(field, hitsFound, equalTo(expectedMatches)); } // Converts an IP string (either IPv4 or IPv6) to a byte array @@ -169,6 +182,9 @@ private XContentBuilder createMapping() throws IOException { .endObject() .endObject() .endObject() + .startObject("dummy_filter") + .field("type", "keyword") + .endObject() .endObject() .endObject(); }