Skip to content

Commit

Permalink
fix dv only range search
Browse files Browse the repository at this point in the history
add filter

Signed-off-by: mikhail-khludnev <[email protected]>
  • Loading branch information
mikhail-khludnev committed Nov 13, 2024
1 parent 26ff736 commit 01db875
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,14 @@ public Query termQuery(Object value, @Nullable QueryShardContext context) {
String term = value.toString();
if (term.contains("/")) {
final Tuple<InetAddress, Integer> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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
Expand Down Expand Up @@ -169,6 +182,9 @@ private XContentBuilder createMapping() throws IOException {
.endObject()
.endObject()
.endObject()
.startObject("dummy_filter")
.field("type", "keyword")
.endObject()
.endObject()
.endObject();
}
Expand Down

0 comments on commit 01db875

Please sign in to comment.