|
9 | 9 |
|
10 | 10 | import org.elasticsearch.common.Strings; |
11 | 11 | import org.elasticsearch.common.settings.Settings; |
| 12 | +import org.elasticsearch.index.query.MatchAllQueryBuilder; |
| 13 | +import org.elasticsearch.index.query.QueryBuilder; |
12 | 14 | import org.elasticsearch.search.SearchModule; |
13 | 15 | import org.elasticsearch.search.builder.SearchSourceBuilder; |
14 | 16 | import org.elasticsearch.search.retriever.RetrieverBuilder; |
@@ -169,42 +171,47 @@ public void testValidation() { |
169 | 171 | } |
170 | 172 |
|
171 | 173 | public void testValidateSort() { |
| 174 | + |
172 | 175 | PinnedRetrieverBuilder builder = new PinnedRetrieverBuilder( |
173 | 176 | List.of("id1"), |
174 | 177 | List.of(), |
175 | 178 | new TestRetrieverBuilder("test"), |
176 | 179 | DEFAULT_RANK_WINDOW_SIZE |
177 | 180 | ); |
178 | 181 |
|
| 182 | + QueryBuilder dummyQuery = new MatchAllQueryBuilder(); |
| 183 | + |
179 | 184 | SearchSourceBuilder emptySource = new SearchSourceBuilder(); |
| 185 | + emptySource.query(dummyQuery); |
180 | 186 | builder.finalizeSourceBuilder(emptySource); |
181 | 187 | assertThat(emptySource.sorts(), equalTo(null)); |
182 | 188 |
|
183 | 189 | SearchSourceBuilder scoreSource = new SearchSourceBuilder(); |
| 190 | + scoreSource.query(dummyQuery); |
184 | 191 | scoreSource.sort("_score"); |
185 | 192 | builder.finalizeSourceBuilder(scoreSource); |
186 | 193 | assertThat(scoreSource.sorts().size(), equalTo(1)); |
187 | 194 |
|
188 | 195 | SearchSourceBuilder customSortSource = new SearchSourceBuilder(); |
| 196 | + customSortSource.query(dummyQuery); |
189 | 197 | customSortSource.sort("field1"); |
190 | 198 | IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.finalizeSourceBuilder(customSortSource)); |
191 | 199 | assertThat( |
192 | 200 | e.getMessage(), |
193 | 201 | equalTo( |
194 | | - "[pinned] retriever only supports sorting by score, " |
195 | | - + "invalid sort criterion: {\n \"field1\" : {\n \"order\" : \"asc\"\n }\n}" |
| 202 | + "[" + PinnedRetrieverBuilder.NAME + "] retriever only supports sorting by score, invalid sort criterion: {\n \"field1\" : {\n \"order\" : \"asc\"\n }\n}" |
196 | 203 | ) |
197 | 204 | ); |
198 | 205 |
|
199 | 206 | SearchSourceBuilder multipleSortsSource = new SearchSourceBuilder(); |
| 207 | + multipleSortsSource.query(dummyQuery); |
200 | 208 | multipleSortsSource.sort("_score"); |
201 | 209 | multipleSortsSource.sort("field1"); |
202 | 210 | e = expectThrows(IllegalArgumentException.class, () -> builder.finalizeSourceBuilder(multipleSortsSource)); |
203 | 211 | assertThat( |
204 | 212 | e.getMessage(), |
205 | 213 | equalTo( |
206 | | - "[pinned] retriever only supports sorting by score, " |
207 | | - + "invalid sort criterion: {\n \"field1\" : {\n \"order\" : \"asc\"\n }\n}" |
| 214 | + "[" + PinnedRetrieverBuilder.NAME + "] retriever only supports sorting by score, invalid sort criterion: {\n \"field1\" : {\n \"order\" : \"asc\"\n }\n}" |
208 | 215 | ) |
209 | 216 | ); |
210 | 217 | } |
|
0 commit comments