forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONRequestIT.java
193 lines (180 loc) · 6.23 KB
/
JSONRequestIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.legacy;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import java.io.IOException;
import java.util.Map;
import org.json.JSONObject;
import org.junit.Test;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
public class JSONRequestIT extends SQLIntegTestCase {
@Override
protected void init() throws Exception {
loadIndex(Index.ACCOUNT);
loadIndex(Index.NESTED);
}
@Test
public void search() throws IOException {
int ageToCompare = 25;
SearchHits response = query(String.format("{\"query\":\"" +
"SELECT * " +
"FROM %s " +
"WHERE age > %s " +
"LIMIT 1000\"}", TestsConstants.TEST_INDEX_ACCOUNT, ageToCompare));
SearchHit[] hits = response.getHits();
for (SearchHit hit : hits) {
int age = (int) hit.getSourceAsMap().get("age");
assertThat(age, greaterThan(ageToCompare));
}
}
@Test
public void searchWithFilterAndNoWhere() throws IOException {
/*
* Human readable format of the request defined below:
* {
* "query": "SELECT * FROM accounts LIMIT 1000",
* "filter": {
* "range": {
* "age": {
* "gt": 25
* }
* }
* }
* }
*/
int ageToCompare = 25;
SearchHits response = query(String.format("{\"query\":\"" +
"SELECT * " +
"FROM %s " +
"LIMIT 1000\",\"filter\":{\"range\":{\"age\":{\"gt\":%s}}}}",
TestsConstants.TEST_INDEX_ACCOUNT, ageToCompare));
SearchHit[] hits = response.getHits();
for (SearchHit hit : hits) {
int age = (int) hit.getSourceAsMap().get("age");
assertThat(age, greaterThan(ageToCompare));
}
}
@Test
public void searchWithRangeFilter() throws IOException {
/*
* Human readable format of the request defined below:
* {
* "query": "SELECT * FROM accounts WHERE age > 25 LIMIT 1000",
* "filter": {
* "range": {
* "balance": {
* "lt": 35000
* }
* }
* }
* }
*/
int ageToCompare = 25;
int balanceToCompare = 35000;
SearchHits response = query(String.format("{\"query\":\"" +
"SELECT * " +
"FROM %s " +
"WHERE age > %s " +
"LIMIT 1000\",\"filter\":{\"range\":{\"balance\":{\"lt\":%s}}}}",
TestsConstants.TEST_INDEX_ACCOUNT, ageToCompare, balanceToCompare));
SearchHit[] hits = response.getHits();
for (SearchHit hit : hits) {
int age = (int) hit.getSourceAsMap().get("age");
int balance = (int) hit.getSourceAsMap().get("balance");
assertThat(age, greaterThan(ageToCompare));
assertThat(balance, lessThan(balanceToCompare));
}
}
@Test
/**
* Using TEST_INDEX_NESTED_TYPE here since term filter does not work properly on analyzed fields like text.
* The field 'someField' in TEST_INDEX_NESTED_TYPE is of type keyword.
*/
public void searchWithTermFilter() throws IOException {
/*
* Human readable format of the request defined below:
* {
* "query": "SELECT * FROM nested_objects WHERE nested(comment.likes) < 3",
* "filter": {
* "term": {
* "someField": "a"
* }
* }
* }
*/
int likesToCompare = 3;
String fieldToCompare = "a";
SearchHits response = query(String.format("{\"query\":\"" +
"SELECT * " +
"FROM %s " +
"WHERE nested(comment.likes) < %s\"," +
"\"filter\":{\"term\":{\"someField\":\"%s\"}}}",
TestsConstants.TEST_INDEX_NESTED_TYPE, likesToCompare, fieldToCompare));
SearchHit[] hits = response.getHits();
for (SearchHit hit : hits) {
int likes = (int) ((Map) hit.getSourceAsMap().get("comment")).get("likes");
String someField = hit.getSourceAsMap().get("someField").toString();
assertThat(likes, lessThan(likesToCompare));
assertThat(someField, equalTo(fieldToCompare));
}
}
@Test
public void searchWithNestedFilter() throws IOException {
/*
* Human readable format of the request defined below:
* {
* "query": "SELECT * FROM nested_objects WHERE nested(comment.likes) > 1",
* "filter": {
* "nested": {
* "path": "comment",
* "query": {
* "bool": {
* "must": {
* "term": {
* "comment.data": "aa"
* }
* }
* }
* }
* }
* }
* }
*/
int likesToCompare = 1;
String dataToCompare = "aa";
SearchHits response = query(String.format("{\"query\":\"" +
"SELECT * " +
"FROM %s " +
"WHERE nested(comment.likes) > %s\"," +
"\"filter\":{\"nested\":{\"path\":\"comment\"," +
"\"query\":{\"bool\":{\"must\":{\"term\":{\"comment.data\":\"%s\"}}}}}}}",
TestsConstants.TEST_INDEX_NESTED_TYPE, likesToCompare, dataToCompare));
SearchHit[] hits = response.getHits();
for (SearchHit hit : hits) {
int likes = (int) ((Map) hit.getSourceAsMap().get("comment")).get("likes");
String data = ((Map) hit.getSourceAsMap().get("comment")).get("data").toString();
assertThat(likes, greaterThan(likesToCompare));
assertThat(data, anyOf(equalTo(dataToCompare), equalTo("[aa, bb]")));
}
}
private SearchHits query(String request) throws IOException {
final JSONObject jsonObject = executeRequest(request);
final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
jsonObject.toString());
return SearchResponse.fromXContent(parser).getHits();
}
}