forked from xinyual/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelevanceFunctionIT.java
More file actions
182 lines (166 loc) · 6.67 KB
/
Copy pathRelevanceFunctionIT.java
File metadata and controls
182 lines (166 loc) · 6.67 KB
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.ppl;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BEER;
import java.io.IOException;
import org.junit.Test;
public class RelevanceFunctionIT extends PPLIntegTestCase {
@Override
public void init() throws Exception {
super.init();
loadIndex(Index.BEER);
}
@Test
public void test_wildcard_simple_query_string() throws IOException {
String query1 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE simple_query_string(['Tags'], 'taste') | fields Id";
var result1 = executeQuery(query1);
String query2 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE simple_query_string(['T*'], 'taste') | fields Id";
var result2 = executeQuery(query2);
assertNotEquals(result2.getInt("total"), result1.getInt("total"));
}
/*
Dash/minus ('-') character is interpreted as NOT flag if it is activated by NOT or ALL `flags` value
`query1` searches for entries with '-free' in `Body`, `query2` - for entries without 'free' in `Body`
*/
@Test
public void verify_flags_in_simple_query_string() throws IOException {
String query1 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE simple_query_string(['Body'], '-free', flags='NONE|PREFIX|ESCAPE')";
var result1 = executeQuery(query1);
String query2 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE simple_query_string([Body], '-free', flags='NOT|AND|OR')";
var result2 = executeQuery(query2);
assertNotEquals(result2.getInt("total"), result1.getInt("total"));
String query = "SOURCE=" + TEST_INDEX_BEER;
var result = executeQuery(query);
assertEquals(result2.getInt("total") + result1.getInt("total"), result.getInt("total"));
}
/*
`escape` parameter switches regex-specific character escaping.
`query1` searches for entries with "\\?" in `Title`, `query2` - for "?"
Ref: QueryParserBase::escape in lucene code.
*/
@Test
public void verify_escape_in_query_string() throws IOException {
String query1 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE query_string([Title], '?', escape=true);";
var result1 = executeQuery(query1);
String query2 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE query_string([Title], '?', escape=false);";
var result2 = executeQuery(query2);
assertEquals(0, result1.getInt("total"));
assertEquals(8, result2.getInt("total"));
}
/*
`default_operator`/`operator` in relevance search functions defines whether to search for all or for any words given.
`query1` returns matches with 'beer' and matches with 'taste',
`query2` returns matches with 'beer' and with 'taste' together.
*/
@Test
public void verify_default_operator_in_query_string() throws IOException {
String query1 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE query_string([Title], 'beer taste', default_operator='OR')";
var result1 = executeQuery(query1);
String query2 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE query_string([Title], 'beer taste', default_operator='AND')";
var result2 = executeQuery(query2);
assertEquals(16, result1.getInt("total"));
assertEquals(4, result2.getInt("total"));
}
@Test
public void verify_default_operator_in_simple_query_string() throws IOException {
String query1 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE simple_query_string([Title], 'beer taste', default_operator='OR')";
var result1 = executeQuery(query1);
String query2 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE simple_query_string([Title], 'beer taste', default_operator='AND')";
var result2 = executeQuery(query2);
assertEquals(16, result1.getInt("total"));
assertEquals(4, result2.getInt("total"));
}
@Test
public void verify_default_operator_in_multi_match() throws IOException {
String query1 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE multi_match([Title], 'beer taste', operator='OR')";
var result1 = executeQuery(query1);
String query2 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE multi_match([Title], 'beer taste', operator='AND')";
var result2 = executeQuery(query2);
assertEquals(16, result1.getInt("total"));
assertEquals(4, result2.getInt("total"));
}
@Test
public void verify_operator_in_match() throws IOException {
String query1 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE match(Title, 'beer taste', operator='OR')";
var result1 = executeQuery(query1);
String query2 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE match(Title, 'beer taste', operator='AND')";
var result2 = executeQuery(query2);
assertEquals(16, result1.getInt("total"));
assertEquals(4, result2.getInt("total"));
}
@Test
public void test_mixed_relevance_function_and_normal_filter() throws IOException {
String query1 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE simple_query_string(['Tags'], 'taste') and AcceptedAnswerId > 200 | fields"
+ " Id";
var result1 = executeQuery(query1);
String query2 =
"SOURCE=" + TEST_INDEX_BEER + " | WHERE simple_query_string(['Tags'], 'taste') | fields Id";
var result2 = executeQuery(query2);
assertEquals(5, result1.getInt("total"));
assertEquals(8, result2.getInt("total"));
}
@Test
public void test_single_field_relevance_function_and_implicit_timestamp_filter()
throws IOException {
String query1 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE match('Tags', 'brewing taste', operator='AND') and CreationDate >"
+ " '2014-01-20 00:00:00.000' and CreationDate < '2018-01-20 00:00:00.000' | fields"
+ " Tags";
var result1 = executeQuery(query1);
assertEquals(2, result1.getInt("total"));
}
@Test
public void test_multi_fields_relevance_function_and_implicit_timestamp_filter()
throws IOException {
String query1 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | WHERE query_string(['Tags'], 'brewing AND taste') and CreationDate > '2014-01-20"
+ " 00:00:00.000' and CreationDate < '2018-01-20 00:00:00.000' | fields Tags";
var result1 = executeQuery(query1);
assertEquals(2, result1.getInt("total"));
}
@Test
public void not_pushdown_throws_exception() throws IOException {
String query1 =
"SOURCE="
+ TEST_INDEX_BEER
+ " | STATS count(AcceptedAnswerId) as count"
+ " | EVAL dateStr = makedate(2025, count)"
+ " | WHERE simple_query_string(['dateStr'], 'taste')";
assertThrows(Exception.class, () -> executeQuery(query1));
}
}