Skip to content

Commit

Permalink
fix tests. Spotless apply.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-khludnev committed Oct 17, 2024
1 parent fbbb0bc commit 0586f06
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
52 changes: 28 additions & 24 deletions server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PointRangeQuery;
Expand Down Expand Up @@ -271,8 +272,7 @@ public Query termQuery(Object value, @Nullable QueryShardContext context) {
public Query termsQuery(List<?> values, QueryShardContext context) {
failIfNotIndexedAndNoDocValues();
List<InetAddress> concreteIPs = new ArrayList<>();
BooleanQuery.Builder ranges = new BooleanQuery.Builder();
int i = 0;
List<Query> ranges = new ArrayList<>();
for (final Object value : values) {
if (value instanceof InetAddress) {
concreteIPs.add((InetAddress) value);
Expand All @@ -283,36 +283,40 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
// and need to fall back to a disjunction of `term` queries
Query query = termQuery(strVal, context);
// would be great to have union on ranges over bare points
ranges.add(query, BooleanClause.Occur.SHOULD);
ranges.add(query);
} else {
concreteIPs.add(InetAddresses.forString(strVal));
}
}
}
if (!concreteIPs.isEmpty()) {
Supplier<Query> pointsQuery;// TODO this trick is redundant
// if (concreteIPs.size() == 1) {
// it can't get InetAddress since it convert is to string and misguided by hostname separating slash
// pointsQuery = () -> termQuery(concreteIPs.get(0), context);
// } else {
pointsQuery = () -> InetAddressPoint.newSetQuery(name(), concreteIPs.toArray(new InetAddress[0]));
if (hasDocValues()) {
List<BytesRef> set = new ArrayList<>(concreteIPs.size());
for(final InetAddress address : concreteIPs) {
set.add(new BytesRef(InetAddressPoint.encode(address)));
}
Query dvQuery = SortedSetDocValuesField.newSlowSetQuery(name(), set);
if (!isSearchable()) {
pointsQuery = () -> dvQuery;
} else {
Supplier<Query> wrap = pointsQuery;
pointsQuery = () -> new IndexOrDocValuesQuery(wrap.get(), dvQuery);
}
Supplier<Query> pointsQuery;
pointsQuery = () -> concreteIPs.size() == 1
? InetAddressPoint.newExactQuery(name(), concreteIPs.iterator().next())
: InetAddressPoint.newSetQuery(name(), concreteIPs.toArray(new InetAddress[0]));
if (hasDocValues()) {
List<BytesRef> set = new ArrayList<>(concreteIPs.size());
for (final InetAddress address : concreteIPs) {
set.add(new BytesRef(InetAddressPoint.encode(address)));
}
Query dvQuery = SortedSetDocValuesField.newSlowSetQuery(name(), set);
if (!isSearchable()) {
pointsQuery = () -> dvQuery;
} else {
Supplier<Query> wrap = pointsQuery;
pointsQuery = () -> new IndexOrDocValuesQuery(wrap.get(), dvQuery);
}
//}
ranges.add(pointsQuery.get(), BooleanClause.Occur.SHOULD);
}
ranges.add(pointsQuery.get());
}
if (ranges.size() == 1) {
return ranges.iterator().next(); // CSQ?
}
BooleanQuery.Builder union = new BooleanQuery.Builder();
for (Query q : ranges) {
union.add(q, BooleanClause.Occur.SHOULD);
}
return ranges.build();
return new ConstantScoreQuery(union.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void testTermQuery() {
}

public void testTermsQuery() {
MappedFieldType ft = new IpFieldMapper.IpFieldType("field",true, false, false, null, Collections.emptyMap());
MappedFieldType ft = new IpFieldMapper.IpFieldType("field", true, false, false, null, Collections.emptyMap());

assertEquals(
InetAddressPoint.newSetQuery("field", InetAddresses.forString("::2"), InetAddresses.forString("::5")),
Expand All @@ -131,8 +131,8 @@ public void testTermsQuery() {
// if the list includes a prefix query we fallback to a bool query
assertEquals(
new ConstantScoreQuery(
new BooleanQuery.Builder().add(ft.termQuery("::42", null), Occur.SHOULD)
.add(ft.termQuery("::2/16", null), Occur.SHOULD)
new BooleanQuery.Builder().add(ft.termQuery("::2/16", null), Occur.SHOULD)
.add(ft.termQuery("::42", null), Occur.SHOULD)
.build()
),
ft.termsQuery(Arrays.asList("::42", "::2/16"), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.test.OpenSearchSingleNodeTestCase;
import org.hamcrest.MatcherAssert;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.hamcrest.Matchers.equalTo;

import org.hamcrest.MatcherAssert;

import static org.opensearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
import static org.hamcrest.Matchers.equalTo;

public class SearchIpFieldTermsTest extends OpenSearchSingleNodeTestCase {

Expand Down Expand Up @@ -92,15 +89,11 @@ public void testMassive() throws Exception {
}
}

bulkRequestBuilder.setRefreshPolicy(IMMEDIATE)
.get();
SearchResponse result = client().prepareSearch(defaultIndexName)
.setQuery(QueryBuilders.termsQuery("addr", toQuery))
.get();
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));
}


// Converts an IP string (either IPv4 or IPv6) to a byte array
private static byte[] ipToBytes(String ip) {
InetAddress inetAddress = InetAddresses.forString(ip);
Expand Down Expand Up @@ -138,10 +131,7 @@ private static boolean isIPInCIDR(String ip, String cidr) {

// Generate a random IPv4 address
private static String generateRandomIPv4() {
return String.format("%d.%d.%d.%d", random().nextInt(256),
random().nextInt(256),
random().nextInt(256),
random().nextInt(256));
return String.format("%d.%d.%d.%d", random().nextInt(256), random().nextInt(256), random().nextInt(256), random().nextInt(256));
}

// Generate a random IPv6 address
Expand Down

0 comments on commit 0586f06

Please sign in to comment.