Skip to content

Commit bd226c2

Browse files
authored
support rangeQuery and regexpQuery in constant_keyword field type (opensearch-project#14711)
--------- Signed-off-by: kkewwei <kkewwei@163.com>
1 parent f829a9f commit bd226c2

File tree

5 files changed

+394
-11
lines changed

5 files changed

+394
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1212
- Add basic aggregation support for derived fields ([#14618](https://github.com/opensearch-project/OpenSearch/pull/14618))
1313
- Add ThreadContextPermission for markAsSystemContext and allow core to perform the method ([#15016](https://github.com/opensearch-project/OpenSearch/pull/15016))
1414
- Add ThreadContextPermission for stashAndMergeHeaders and stashWithOrigin ([#15039](https://github.com/opensearch-project/OpenSearch/pull/15039))
15+
- Add `rangeQuery` and `regexpQuery` for `constant_keyword` field type ([#14711](https://github.com/opensearch-project/OpenSearch/pull/14711))
1516

1617
### Dependencies
1718
- Bump `netty` from 4.1.111.Final to 4.1.112.Final ([#15081](https://github.com/opensearch-project/OpenSearch/pull/15081))

rest-api-spec/src/main/resources/rest-api-spec/test/index/110_constant_keyword.yml

+272-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1+
# The test setup includes two parts:
2+
# part1: test mapping and indexing
3+
# part2: test query
14
---
2-
# The test setup includes:
3-
# - Create index with constant_keyword field type
4-
# - Check mapping
5-
# - Index two example documents
6-
# - Search
7-
# - Delete Index when connection is teardown
8-
9-
"Mappings and Supported queries":
5+
"Mappings and Indexing":
106
- skip:
117
version: " - 2.15.99"
128
reason: "fixed in 2.16.0"
139

14-
# Create index with constant_keyword field type
10+
# Create indices with constant_keyword field type
1511
- do:
1612
indices.create:
1713
index: test
@@ -22,7 +18,7 @@
2218
type: "constant_keyword"
2319
value: "1"
2420

25-
# Index document
21+
# Index documents to test integer and string are both ok.
2622
- do:
2723
index:
2824
index: test
@@ -39,6 +35,7 @@
3935
"genre": 1
4036
}
4137

38+
# Refresh
4239
- do:
4340
indices.refresh:
4441
index: test
@@ -54,6 +51,7 @@
5451
# Verify Document Count
5552
- do:
5653
search:
54+
index: test
5755
body: {
5856
query: {
5957
match_all: {}
@@ -68,3 +66,267 @@
6866
- do:
6967
indices.delete:
7068
index: test
69+
70+
---
71+
"Queries":
72+
- skip:
73+
version: " - 2.99.99"
74+
reason: "rangeQuery and regexpQuery are supported in 3.0.0 in main branch"
75+
76+
- do:
77+
indices.create:
78+
index: test1
79+
body:
80+
mappings:
81+
properties:
82+
genre:
83+
type: "constant_keyword"
84+
value: "d3efault"
85+
86+
# Index documents to test query.
87+
- do:
88+
index:
89+
index: test1
90+
id: 1
91+
body: {
92+
"genre": "d3efault"
93+
}
94+
95+
# Refresh
96+
- do:
97+
indices.refresh:
98+
index: test1
99+
100+
# Test rangeQuery
101+
- do:
102+
search:
103+
index: test1
104+
body: {
105+
query: {
106+
range: {
107+
genre: {
108+
gte: "d3efault"
109+
}
110+
}
111+
}
112+
}
113+
114+
- length: { hits.hits: 1 }
115+
116+
- do:
117+
search:
118+
index: test1
119+
body: {
120+
query: {
121+
range: {
122+
genre: {
123+
from: "d3efault",
124+
"include_lower": "false"
125+
}
126+
}
127+
}
128+
}
129+
130+
- length: { hits.hits: 0 }
131+
132+
- do:
133+
search:
134+
index: test1
135+
body: {
136+
query: {
137+
range: {
138+
genre: {
139+
lte: "d3efault"
140+
}
141+
}
142+
}
143+
}
144+
145+
- length: { hits.hits: 1 }
146+
147+
- do:
148+
search:
149+
index: test1
150+
body: {
151+
query: {
152+
range: {
153+
genre: {
154+
to: "d3efault",
155+
include_upper: "false"
156+
}
157+
}
158+
}
159+
}
160+
161+
- length: { hits.hits: 0 }
162+
163+
- do:
164+
search:
165+
index: test1
166+
body: {
167+
query: {
168+
range: {
169+
genre: {
170+
from: "d3efault",
171+
to: "d3efault",
172+
include_lower: "false",
173+
include_upper: "true"
174+
}
175+
}
176+
}
177+
}
178+
179+
- length: { hits.hits: 0 }
180+
181+
- do:
182+
search:
183+
index: test1
184+
body: {
185+
query: {
186+
range: {
187+
genre: {
188+
from: "d3efault",
189+
to: "d3efault",
190+
include_lower: "true",
191+
include_upper: "false"
192+
}
193+
}
194+
}
195+
}
196+
197+
- length: { hits.hits: 0 }
198+
199+
- do:
200+
search:
201+
index: test1
202+
body: {
203+
query: {
204+
range: {
205+
genre: {
206+
from: null,
207+
to: null
208+
}
209+
}
210+
}
211+
}
212+
213+
- length: { hits.hits: 1 }
214+
215+
- do:
216+
search:
217+
index: test1
218+
body: {
219+
query: {
220+
range: {
221+
genre: {
222+
from: "d3efault",
223+
to: "d3efault",
224+
include_lower: "true",
225+
include_upper: "true"
226+
}
227+
}
228+
}
229+
}
230+
231+
- length: { hits.hits: 1 }
232+
233+
- do:
234+
search:
235+
index: test1
236+
body: {
237+
query: {
238+
range: {
239+
genre: {
240+
from: "d3efaul",
241+
to: "d3efault1",
242+
include_lower: "true",
243+
include_upper: "true"
244+
}
245+
}
246+
}
247+
}
248+
249+
- length: { hits.hits: 1 }
250+
251+
# Test regexpQuery
252+
- do:
253+
search:
254+
index: test1
255+
body: {
256+
query: {
257+
regexp: {
258+
"genre":"d.*"
259+
}
260+
}
261+
}
262+
263+
- length: { hits.hits: 1 }
264+
265+
- do:
266+
search:
267+
index: test1
268+
body: {
269+
query: {
270+
regexp: {
271+
"genre":"d\\defau[a-z]?t"
272+
}
273+
}
274+
}
275+
276+
- length: { hits.hits: 1 }
277+
278+
- do:
279+
search:
280+
index: test1
281+
body: {
282+
query: {
283+
regexp: {
284+
"genre":"d\\defa[a-z]?t"
285+
}
286+
}
287+
}
288+
289+
- length: { hits.hits: 0 }
290+
291+
- do:
292+
search:
293+
index: test1
294+
body: {
295+
query: {
296+
regexp: {
297+
"genre":"d3efa[a-z]{3,3}"
298+
}
299+
}
300+
}
301+
302+
- length: { hits.hits: 1 }
303+
304+
- do:
305+
search:
306+
index: test1
307+
body: {
308+
query: {
309+
regexp: {
310+
"genre":"d3efa[a-z]{4,4}"
311+
}
312+
}
313+
}
314+
315+
- length: { hits.hits: 0 }
316+
317+
- do:
318+
search:
319+
index: test1
320+
body: {
321+
query: {
322+
match_all: {}
323+
}
324+
}
325+
326+
- length: { hits.hits: 1 }
327+
- match: { hits.hits.0._source.genre: "d3efault" }
328+
329+
# Delete Index when connection is teardown
330+
- do:
331+
indices.delete:
332+
index: test1

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public final boolean isAggregatable() {
7676
*/
7777
protected abstract boolean matches(String pattern, boolean caseInsensitive, QueryShardContext context);
7878

79-
private static String valueToString(Object value) {
79+
static String valueToString(Object value) {
8080
return value instanceof BytesRef ? ((BytesRef) value).utf8ToString() : value.toString();
8181
}
8282

0 commit comments

Comments
 (0)