Skip to content

Commit f15a2ae

Browse files
committed
Updating Ip fields to use doc_values to search
Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
1 parent 69cc2a1 commit f15a2ae

File tree

3 files changed

+192
-12
lines changed

3 files changed

+192
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
157157
- Made leader/follower check timeout setting dynamic ([#10528](https://github.com/opensearch-project/OpenSearch/pull/10528))
158158
- Improve boolean parsing performance ([#11308](https://github.com/opensearch-project/OpenSearch/pull/11308))
159159
- Change error message when per shard document limit is breached ([#11312](https://github.com/opensearch-project/OpenSearch/pull/11312))
160+
- Updates IpField to be searchable when only `doc_values` are enabled ([#11508](https://github.com/opensearch-project/OpenSearch/pull/11508))
160161

161162
### Deprecated
162163

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
setup:
2+
- skip:
3+
features: [ "headers" ]
4+
version: " - 2.11.99"
5+
reason: "searching with only doc_values was added in 2.12.0"
6+
7+
---
8+
"search on ip fields with doc_values enabled":
9+
- do:
10+
indices.create:
11+
index: test-iodvq
12+
body:
13+
mappings:
14+
dynamic: false
15+
properties:
16+
ip_index:
17+
type: ip
18+
doc_values: false
19+
ip_doc_values:
20+
type: ip
21+
index: false
22+
ip_both:
23+
type: ip
24+
index: true
25+
doc_values: true
26+
27+
28+
- do:
29+
headers:
30+
Content-Type: application/json
31+
index:
32+
index: "test-iodvq"
33+
id: 1
34+
body:
35+
ip_index: "192.168.0.1"
36+
ip_doc_values: "192.168.0.2"
37+
ip_both: "192.168.0.3"
38+
39+
- do:
40+
headers:
41+
Content-Type: application/json
42+
index:
43+
index: "test-iodvq"
44+
id: 2
45+
body:
46+
ip_index: "192.168.1.1"
47+
ip_doc_values: "192.168.1.2"
48+
ip_both: "192.168.1.3"
49+
50+
51+
- do:
52+
headers:
53+
Content-Type: application/json
54+
index:
55+
index: "test-iodvq"
56+
id: 3
57+
body:
58+
ip_index: "192.168.2.1"
59+
ip_doc_values: "192.168.2.2"
60+
ip_both: "192.168.2.3"
61+
62+
63+
- do:
64+
indices.refresh: {}
65+
66+
- do:
67+
search:
68+
rest_total_hits_as_int: true
69+
index: test-iodvq
70+
body:
71+
query:
72+
term:
73+
ip_index: "192.168.0.1"
74+
75+
- match: {hits.total: 1}
76+
77+
- do:
78+
search:
79+
rest_total_hits_as_int: true
80+
index: test-iodvq
81+
body:
82+
query:
83+
term:
84+
ip_doc_values: "192.168.0.2"
85+
86+
- match: { hits.total: 1 }
87+
88+
- do:
89+
search:
90+
rest_total_hits_as_int: true
91+
index: test-iodvq
92+
body:
93+
query:
94+
term:
95+
ip_both: "192.168.0.3"
96+
97+
- match: { hits.total: 1 }
98+
99+
- do:
100+
search:
101+
rest_total_hits_as_int: true
102+
index: test-iodvq
103+
body:
104+
query:
105+
terms:
106+
ip_index: ["192.168.0.1", "192.168.1.1"]
107+
108+
- match: { hits.total: 2 }
109+
110+
- do:
111+
search:
112+
rest_total_hits_as_int: true
113+
index: test-iodvq
114+
body:
115+
query:
116+
range:
117+
ip_doc_values:
118+
gte: "192.168.0.2"
119+
120+
- match: { hits.total: 3 }
121+
122+
123+
- do:
124+
search:
125+
rest_total_hits_as_int: true
126+
index: test-iodvq
127+
body:
128+
query:
129+
range:
130+
ip_both:
131+
gte: "192.168.0.2"
132+
lte: "192.168.2.2"
133+
134+
- match: { hits.total: 2 }

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

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import org.apache.lucene.document.SortedSetDocValuesField;
3737
import org.apache.lucene.document.StoredField;
3838
import org.apache.lucene.index.SortedSetDocValues;
39+
import org.apache.lucene.search.IndexOrDocValuesQuery;
3940
import org.apache.lucene.search.MatchNoDocsQuery;
41+
import org.apache.lucene.search.PointRangeQuery;
4042
import org.apache.lucene.search.Query;
4143
import org.apache.lucene.util.ArrayUtil;
4244
import org.apache.lucene.util.BytesRef;
@@ -222,25 +224,50 @@ protected Object parseSourceValue(Object value) {
222224

223225
@Override
224226
public Query termQuery(Object value, @Nullable QueryShardContext context) {
225-
failIfNotIndexed();
227+
failIfNotIndexedAndNoDocValues();
228+
Query query;
226229
if (value instanceof InetAddress) {
227-
return InetAddressPoint.newExactQuery(name(), (InetAddress) value);
230+
query = InetAddressPoint.newExactQuery(name(), (InetAddress) value);
228231
} else {
229232
if (value instanceof BytesRef) {
230233
value = ((BytesRef) value).utf8ToString();
231234
}
232235
String term = value.toString();
233236
if (term.contains("/")) {
234237
final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term);
235-
return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
238+
query = InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
236239
}
237240
InetAddress address = InetAddresses.forString(term);
238-
return InetAddressPoint.newExactQuery(name(), address);
241+
query = InetAddressPoint.newExactQuery(name(), address);
239242
}
243+
if (isSearchable() && hasDocValues()) {
244+
return new IndexOrDocValuesQuery(
245+
query,
246+
SortedSetDocValuesField.newSlowRangeQuery(
247+
((PointRangeQuery) query).getField(),
248+
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
249+
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
250+
true,
251+
true
252+
)
253+
);
254+
}
255+
if (hasDocValues()) {
256+
// InetAddressPoint uses a rangeQuery internally for terms
257+
return SortedSetDocValuesField.newSlowRangeQuery(
258+
((PointRangeQuery) query).getField(),
259+
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
260+
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
261+
true,
262+
true
263+
);
264+
}
265+
return query;
240266
}
241267

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

266293
@Override
267294
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
268-
failIfNotIndexed();
269-
return rangeQuery(
270-
lowerTerm,
271-
upperTerm,
272-
includeLower,
273-
includeUpper,
274-
(lower, upper) -> InetAddressPoint.newRangeQuery(name(), lower, upper)
275-
);
295+
failIfNotIndexedAndNoDocValues();
296+
return rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, (lower, upper) -> {
297+
Query query = InetAddressPoint.newRangeQuery(name(), lower, upper);
298+
if (isSearchable() && hasDocValues()) {
299+
return new IndexOrDocValuesQuery(
300+
query,
301+
SortedSetDocValuesField.newSlowRangeQuery(
302+
((PointRangeQuery) query).getField(),
303+
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
304+
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
305+
true,
306+
true
307+
)
308+
);
309+
}
310+
if (hasDocValues()) {
311+
return SortedSetDocValuesField.newSlowRangeQuery(
312+
((PointRangeQuery) query).getField(),
313+
new BytesRef(((PointRangeQuery) query).getLowerPoint()),
314+
new BytesRef(((PointRangeQuery) query).getUpperPoint()),
315+
true,
316+
true
317+
);
318+
}
319+
return query;
320+
});
276321
}
277322

278323
/**

0 commit comments

Comments
 (0)