Skip to content

Commit

Permalink
Updating Ip fields to use doc_values to search
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
  • Loading branch information
harshavamsi committed Dec 7, 2023
1 parent 69cc2a1 commit 4b2b0c5
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
setup:
- skip:
features: [ "headers" ]
version: " - 2.11.99"
reason: "searching with only doc_values was added in 2.12.0"

---
"search on ip fields with doc_values enabled":
- do:
indices.create:
index: test-iodvq
body:
mappings:
dynamic: false
properties:
ip_index:
type: ip
doc_values: false
ip_doc_values:
type: ip
index: false
ip_both:
type: ip
index: true
doc_values: true


- do:
headers:
Content-Type: application/json
index:
index: "test-iodvq"
id: 1
body:
ip_index: "192.168.0.1"
ip_doc_values: "192.168.0.2"
ip_both: "192.168.0.3"

- do:
headers:
Content-Type: application/json
index:
index: "test-iodvq"
id: 2
body:
ip_index: "192.168.1.1"
ip_doc_values: "192.168.1.2"
ip_both: "192.168.1.3"


- do:
headers:
Content-Type: application/json
index:
index: "test-iodvq"
id: 3
body:
ip_index: "192.168.2.1"
ip_doc_values: "192.168.2.2"
ip_both: "192.168.2.3"


- do:
indices.refresh: {}

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
ip_index: "192.168.0.1"

- match: {hits.total: 1}

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
ip_doc_values: "192.168.0.2"

- match: { hits.total: 1 }

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
ip_both: "192.168.0.3"

- match: { hits.total: 1 }

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
terms:
ip_index: ["192.168.0.1", "192.168.1.1"]

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
range:
ip_doc_values:
gte: "192.168.0.2"

- match: { hits.total: 3 }


- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
range:
ip_both:
gte: "192.168.0.2"
lte: "192.168.2.2"

- match: { hits.total: 2 }
69 changes: 57 additions & 12 deletions server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.BytesRef;
Expand Down Expand Up @@ -222,25 +224,50 @@ protected Object parseSourceValue(Object value) {

@Override
public Query termQuery(Object value, @Nullable QueryShardContext context) {
failIfNotIndexed();
failIfNotIndexedAndNoDocValues();
Query query;
if (value instanceof InetAddress) {
return InetAddressPoint.newExactQuery(name(), (InetAddress) value);
query = InetAddressPoint.newExactQuery(name(), (InetAddress) value);
} else {
if (value instanceof BytesRef) {
value = ((BytesRef) value).utf8ToString();
}
String term = value.toString();
if (term.contains("/")) {
final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term);
return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
query = InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
}
InetAddress address = InetAddresses.forString(term);
return InetAddressPoint.newExactQuery(name(), address);
query = InetAddressPoint.newExactQuery(name(), address);
}
if (isSearchable() && hasDocValues()) {
return new IndexOrDocValuesQuery(
query,
SortedSetDocValuesField.newSlowRangeQuery(
((PointRangeQuery) query).getField(),
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
true,
true
)
);
}
if (hasDocValues()) {
// InetAddressPoint uses a rangeQuery internally for terms
return SortedSetDocValuesField.newSlowRangeQuery(
((PointRangeQuery) query).getField(),
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
true,
true
);
}
return query;
}

@Override
public Query termsQuery(List<?> values, QueryShardContext context) {
failIfNotIndexedAndNoDocValues();
InetAddress[] addresses = new InetAddress[values.size()];
int i = 0;
for (Object value : values) {
Expand All @@ -265,14 +292,32 @@ public Query termsQuery(List<?> values, QueryShardContext context) {

@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
failIfNotIndexed();
return rangeQuery(
lowerTerm,
upperTerm,
includeLower,
includeUpper,
(lower, upper) -> InetAddressPoint.newRangeQuery(name(), lower, upper)
);
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
)
);
}
if (hasDocValues()) {
return SortedSetDocValuesField.newSlowRangeQuery(
((PointRangeQuery) query).getField(),
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
true,
true
);
}
return query;
});
}

/**
Expand Down

0 comments on commit 4b2b0c5

Please sign in to comment.