Skip to content

Search dv only IP masks #16628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Support retrieving doc values of unsigned long field ([#16543](https://github.com/opensearch-project/OpenSearch/pull/16543))
- Fix rollover alias supports restored searchable snapshot index([#16483](https://github.com/opensearch-project/OpenSearch/pull/16483))
- Fix permissions error on scripted query against remote snapshot ([#16544](https://github.com/opensearch-project/OpenSearch/pull/16544))
- Fix `doc_values` only (`index:false`) IP field searching for masks ([#16628](https://github.com/opensearch-project/OpenSearch/pull/16628))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,28 @@

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
ip_field: "192.168.0.1/24"

- match: { hits.total: 3 }

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
ip_field: "192.168.0.1/31"

- match: { hits.total: 1 }

- do:
search:
rest_total_hits_as_int: true
Expand Down Expand Up @@ -987,6 +1009,28 @@

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-index
body:
query:
term:
ip_field: "192.168.0.1/24"

- match: { hits.total: 3 }

- do:
search:
rest_total_hits_as_int: true
index: test-index
body:
query:
term:
ip_field: "192.168.0.1/31"

- match: { hits.total: 1 }

- do:
search:
rest_total_hits_as_int: true
Expand Down Expand Up @@ -1077,8 +1121,8 @@
"search on fields with only doc_values enabled":
- skip:
features: [ "headers" ]
version: " - 2.99.99"
reason: "searching with only doc_values was added in 3.0.0"
version: " - 2.18.99"
reason: "searching with only doc_values was finally added in 2.19.0"
- do:
indices.create:
index: test-doc-values
Expand Down Expand Up @@ -1372,6 +1416,28 @@

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-doc-values
body:
query:
terms:
ip_field: ["192.168.0.1", "192.168.0.2"]

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-doc-values
body:
query:
terms:
ip_field: ["192.168.0.1/31", "192.168.0.3"]

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
Expand Down Expand Up @@ -1516,6 +1582,28 @@

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-doc-values
body:
query:
term:
ip_field: "192.168.0.1/31"

- match: { hits.total: 1 }

- do:
search:
rest_total_hits_as_int: true
index: test-doc-values
body:
query:
term:
ip_field: "192.168.0.1/24"

- match: { hits.total: 3 }

- do:
search:
rest_total_hits_as_int: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
* A {@link FieldMapper} for ip addresses.
Expand Down Expand Up @@ -225,42 +226,49 @@
@Override
public Query termQuery(Object value, @Nullable QueryShardContext context) {
failIfNotIndexedAndNoDocValues();
Query query;
final PointRangeQuery pointQuery;
if (value instanceof InetAddress) {
query = InetAddressPoint.newExactQuery(name(), (InetAddress) value);
pointQuery = (PointRangeQuery) InetAddressPoint.newExactQuery(name(), (InetAddress) value);

Check warning on line 231 in server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java#L231

Added line #L231 was not covered by tests
} else {
if (value instanceof BytesRef) {
value = ((BytesRef) value).utf8ToString();
}
String term = value.toString();
if (term.contains("/")) {
final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term);
query = InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
pointQuery = (PointRangeQuery) InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
} else {
InetAddress address = InetAddresses.forString(term);
query = InetAddressPoint.newExactQuery(name(), address);
pointQuery = (PointRangeQuery) InetAddressPoint.newExactQuery(name(), address);
}
}
if (isSearchable() && hasDocValues()) {
String term = value.toString();
if (term.contains("/")) {
final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term);
return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
}
return new IndexOrDocValuesQuery(
query,
SortedSetDocValuesField.newSlowExactQuery(name(), new BytesRef(((PointRangeQuery) query).getLowerPoint()))
Query dvQuery = null;
if (hasDocValues()) {
dvQuery = SortedSetDocValuesField.newSlowRangeQuery(
name(),
new BytesRef(pointQuery.getLowerPoint()),
new BytesRef(pointQuery.getUpperPoint()),
true,
true
);
}
if (hasDocValues()) {
String term = value.toString();
if (term.contains("/")) {
final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term);
return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
return indexOrDvQuery(pointQuery, dvQuery);
}

private Query indexOrDvQuery(Query pointQuery, Query dvQuery) {
if (isSearchable() && hasDocValues()) {
assert pointQuery != null;
assert dvQuery != null;
return new IndexOrDocValuesQuery(pointQuery, dvQuery);
} else {
if (isSearchable()) {
assert pointQuery != null;
return pointQuery;
} else {
assert dvQuery != null;
return dvQuery;
}
return SortedSetDocValuesField.newSlowExactQuery(name(), new BytesRef(((PointRangeQuery) query).getLowerPoint()));
}
return query;
}

@Override
Expand All @@ -285,36 +293,38 @@
}
addresses[i++] = address;
}
return InetAddressPoint.newSetQuery(name(), addresses);
Query dvQuery = null;
if (hasDocValues()) {
List<BytesRef> bytesRefs = Arrays.stream(addresses)
.distinct()
.map(InetAddressPoint::encode)
.map(BytesRef::new)
.collect(Collectors.<BytesRef>toList());
dvQuery = SortedSetDocValuesField.newSlowSetQuery(name(), bytesRefs);
}
Query pointQuery = null;
if (isSearchable()) {
pointQuery = InetAddressPoint.newSetQuery(name(), addresses);
}
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);
if (isSearchable() && hasDocValues()) {
return new IndexOrDocValuesQuery(
query,
SortedSetDocValuesField.newSlowRangeQuery(
((PointRangeQuery) query).getField(),
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
true,
true
)
);
}
PointRangeQuery pointQuery = (PointRangeQuery) InetAddressPoint.newRangeQuery(name(), lower, upper);
Query dvQuery = null;
if (hasDocValues()) {
return SortedSetDocValuesField.newSlowRangeQuery(
((PointRangeQuery) query).getField(),
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
dvQuery = SortedSetDocValuesField.newSlowRangeQuery(
pointQuery.getField(),
new BytesRef(pointQuery.getLowerPoint()),
new BytesRef(pointQuery.getUpperPoint()),
true,
true
);
}
return query;
return indexOrDvQuery(pointQuery, dvQuery);
});
}

Expand Down
Loading
Loading