Skip to content

Commit 2e67647

Browse files
committed
more rest tests
Signed-off-by: bowenlan-amzn <[email protected]>
1 parent 577e6d0 commit 2e67647

File tree

3 files changed

+186
-67
lines changed

3 files changed

+186
-67
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search/175_terms_lookup.yml

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
setup:
3+
- do:
4+
indices.create:
5+
index: students
6+
body:
7+
settings:
8+
number_of_shards: 1
9+
number_of_replicas: 0
10+
mappings:
11+
properties:
12+
student_id:
13+
type: integer
14+
- do:
15+
bulk:
16+
refresh: true
17+
body:
18+
- { "index": { "_index": "students", "_id": "1" } }
19+
- { "name": "Jane Doe", "student_id": 111 }
20+
- { "index": { "_index": "students", "_id": "2" } }
21+
- { "name": "Mary Major", "student_id": 222 }
22+
- { "index": { "_index": "students", "_id": "3" } }
23+
- { "name": "John Doe", "student_id": 333 }
24+
- do:
25+
indices.create:
26+
index: classes
27+
body:
28+
settings:
29+
number_of_shards: 1
30+
number_of_replicas: 0
31+
mappings:
32+
properties:
33+
enrolled:
34+
type: binary
35+
store: true
36+
- do:
37+
bulk:
38+
refresh: true
39+
body:
40+
- { "index": { "_index": "classes", "_id": "101" } }
41+
- { "enrolled": "OjAAAAEAAAAAAAEAEAAAAG8A3gA=" } # 111,222
42+
- { "index": { "_index": "classes", "_id": "102" } }
43+
- { "enrolled": "OjAAAAEAAAAAAAAAEAAAAG8A" } # 111
44+
- { "index": { "_index": "classes", "_id": "103" } }
45+
- { "enrolled": "OjAAAAEAAAAAAAAAEAAAAE0B" } # 333
46+
- { "index": { "_index": "classes", "_id": "104" } }
47+
- { "enrolled": "OjAAAAEAAAAAAAEAEAAAAN4ATQE=" } # 222,333
48+
- do:
49+
cluster.health:
50+
wait_for_status: green
51+
52+
---
53+
"Terms lookup on a binary field with bitmap":
54+
- do:
55+
search:
56+
rest_total_hits_as_int: true
57+
index: students
58+
body: {
59+
"query": {
60+
"terms": {
61+
"student_id": {
62+
"index": "classes",
63+
"id": "101",
64+
"path": "enrolled",
65+
"store": true
66+
},
67+
"value_type": "bitmap"
68+
}
69+
}
70+
}
71+
- match: { hits.total: 2 }
72+
- match: { hits.hits.0._source.name: Jane Doe }
73+
- match: { hits.hits.0._source.student_id: 111 }
74+
- match: { hits.hits.1._source.name: Mary Major }
75+
- match: { hits.hits.1._source.student_id: 222 }
76+
---
77+
"Terms query accepting bitmap as value":
78+
- do:
79+
search:
80+
rest_total_hits_as_int: true
81+
index: students
82+
body: {
83+
"query": {
84+
"terms": {
85+
"student_id": "OjAAAAEAAAAAAAEAEAAAAG8A3gA=",
86+
"value_type": "bitmap"
87+
}
88+
}
89+
}
90+
- match: { hits.total: 2 }
91+
- match: { hits.hits.0._source.name: Jane Doe }
92+
- match: { hits.hits.0._source.student_id: 111 }
93+
- match: { hits.hits.1._source.name: Mary Major }
94+
- match: { hits.hits.1._source.student_id: 222 }
95+
---
96+
"Boolean must bitmap filtering":
97+
- do:
98+
search:
99+
rest_total_hits_as_int: true
100+
index: students
101+
body: {
102+
"query": {
103+
"bool": {
104+
"must": [
105+
{
106+
"terms": {
107+
"student_id": {
108+
"index": "classes",
109+
"id": "101",
110+
"path": "enrolled",
111+
"store": true
112+
},
113+
"value_type": "bitmap"
114+
}
115+
}
116+
],
117+
"must_not": [
118+
{
119+
"terms": {
120+
"student_id": {
121+
"index": "classes",
122+
"id": "104",
123+
"path": "enrolled",
124+
"store": true
125+
},
126+
"value_type": "bitmap"
127+
}
128+
}
129+
]
130+
}
131+
}
132+
}
133+
- match: { hits.total: 1 }
134+
- match: { hits.hits.0._source.name: Jane Doe }
135+
- match: { hits.hits.0._source.student_id: 111 }
136+
137+
---
138+
"Boolean should bitmap filtering":
139+
- do:
140+
search:
141+
rest_total_hits_as_int: true
142+
index: students
143+
body: {
144+
"query": {
145+
"bool": {
146+
"should": [
147+
{
148+
"terms": {
149+
"student_id": {
150+
"index": "classes",
151+
"id": "101",
152+
"path": "enrolled",
153+
"store": true
154+
},
155+
"value_type": "bitmap"
156+
}
157+
},
158+
{
159+
"terms": {
160+
"student_id": {
161+
"index": "classes",
162+
"id": "104",
163+
"path": "enrolled",
164+
"store": true
165+
},
166+
"value_type": "bitmap"
167+
}
168+
}
169+
]
170+
}
171+
}
172+
}
173+
- match: { hits.total: 3 }
174+
- match: { hits.hits.0._source.name: Jane Doe }
175+
- match: { hits.hits.0._source.student_id: 111 }
176+
- match: { hits.hits.0._source.name: Mary Major }
177+
- match: { hits.hits.0._source.student_id: 222 }
178+
- match: { hits.hits.0._source.name: John Doe }
179+
- match: { hits.hits.0._source.student_id: 333 }

server/src/main/java/org/opensearch/index/query/TermsQueryBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import java.util.AbstractList;
6565
import java.util.ArrayList;
6666
import java.util.Arrays;
67+
import java.util.Base64;
6768
import java.util.Collections;
6869
import java.util.HashSet;
6970
import java.util.List;
@@ -462,6 +463,12 @@ public static TermsQueryBuilder fromXContent(XContentParser parser) throws IOExc
462463
);
463464
}
464465

466+
// if value_type is not empty, and values is a BytesRef,
467+
// we parse the bytes to the corresponding structure here
468+
if (valueType != null && values != null && values.size() == 1 && values.get(0) instanceof BytesRef) {
469+
values.set(0, new BytesArray(Base64.getDecoder().decode(((BytesRef) values.get(0)).utf8ToString())));
470+
}
471+
465472
return new TermsQueryBuilder(fieldName, values, termsLookup).boost(boost).queryName(queryName).valueType(valueType);
466473
}
467474

0 commit comments

Comments
 (0)