|
11 | 11 | import org.apache.lucene.util.Constants; |
12 | 12 | import org.elasticsearch.action.NoShardAvailableActionException; |
13 | 13 | import org.elasticsearch.action.RoutingMissingException; |
| 14 | +import org.elasticsearch.action.search.ReduceSearchPhaseException; |
14 | 15 | import org.elasticsearch.action.search.SearchPhaseExecutionException; |
15 | 16 | import org.elasticsearch.action.search.ShardSearchFailure; |
16 | 17 | import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException; |
@@ -202,6 +203,104 @@ public void testGuessRootCause() { |
202 | 203 | } |
203 | 204 | } |
204 | 205 |
|
| 206 | + public void testReduceSearchPhaseExceptionWithNoShardFailuresAndNoCause() throws IOException { |
| 207 | + final ReduceSearchPhaseException ex = new ReduceSearchPhaseException( |
| 208 | + "search", |
| 209 | + "no shard failure", |
| 210 | + null, |
| 211 | + ShardSearchFailure.EMPTY_ARRAY |
| 212 | + ); |
| 213 | + |
| 214 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 215 | + builder.startObject(); |
| 216 | + ex.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 217 | + builder.endObject(); |
| 218 | + String expected = """ |
| 219 | + { |
| 220 | + "type": "reduce_search_phase_exception", |
| 221 | + "reason": "[reduce] no shard failure", |
| 222 | + "phase": "search", |
| 223 | + "grouped": true, |
| 224 | + "failed_shards": [] |
| 225 | + }"""; |
| 226 | + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); |
| 227 | + assertEquals(RestStatus.INTERNAL_SERVER_ERROR.getStatus(), ex.status().getStatus()); |
| 228 | + } |
| 229 | + |
| 230 | + public void testReduceSearchPhaseExceptionWithNoShardFailuresAndCause() throws IOException { |
| 231 | + final ReduceSearchPhaseException ex = new ReduceSearchPhaseException( |
| 232 | + "search", |
| 233 | + "no shard failure", |
| 234 | + new IllegalArgumentException("illegal argument"), |
| 235 | + ShardSearchFailure.EMPTY_ARRAY |
| 236 | + ); |
| 237 | + |
| 238 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 239 | + builder.startObject(); |
| 240 | + ex.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 241 | + builder.endObject(); |
| 242 | + String expected = """ |
| 243 | + { |
| 244 | + "type": "reduce_search_phase_exception", |
| 245 | + "reason": "[reduce] no shard failure", |
| 246 | + "phase": "search", |
| 247 | + "grouped": true, |
| 248 | + "failed_shards": [], |
| 249 | + "caused_by":{"type":"illegal_argument_exception","reason":"illegal argument"} |
| 250 | + }"""; |
| 251 | + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); |
| 252 | + assertEquals(RestStatus.BAD_REQUEST.getStatus(), ex.status().getStatus()); |
| 253 | + } |
| 254 | + |
| 255 | + public void testSearchPhaseExecutionExceptionWithNoShardFailuresAndNoCause() throws IOException { |
| 256 | + final SearchPhaseExecutionException ex = new SearchPhaseExecutionException( |
| 257 | + "search", |
| 258 | + "no shard failure", |
| 259 | + null, |
| 260 | + ShardSearchFailure.EMPTY_ARRAY |
| 261 | + ); |
| 262 | + |
| 263 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 264 | + builder.startObject(); |
| 265 | + ex.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 266 | + builder.endObject(); |
| 267 | + String expected = """ |
| 268 | + { |
| 269 | + "type": "search_phase_execution_exception", |
| 270 | + "reason": "no shard failure", |
| 271 | + "phase": "search", |
| 272 | + "grouped": true, |
| 273 | + "failed_shards": [] |
| 274 | + }"""; |
| 275 | + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); |
| 276 | + assertEquals(RestStatus.SERVICE_UNAVAILABLE.getStatus(), ex.status().getStatus()); |
| 277 | + } |
| 278 | + |
| 279 | + public void testReduceSearchPhaseExecutionExceptionWithNoShardFailuresAndCause() throws IOException { |
| 280 | + final SearchPhaseExecutionException ex = new SearchPhaseExecutionException( |
| 281 | + "search", |
| 282 | + "no shard failure", |
| 283 | + new IllegalArgumentException("illegal argument"), |
| 284 | + ShardSearchFailure.EMPTY_ARRAY |
| 285 | + ); |
| 286 | + |
| 287 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 288 | + builder.startObject(); |
| 289 | + ex.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 290 | + builder.endObject(); |
| 291 | + String expected = """ |
| 292 | + { |
| 293 | + "type": "search_phase_execution_exception", |
| 294 | + "reason": "no shard failure", |
| 295 | + "phase": "search", |
| 296 | + "grouped": true, |
| 297 | + "failed_shards": [], |
| 298 | + "caused_by":{"type":"illegal_argument_exception","reason":"illegal argument"} |
| 299 | + }"""; |
| 300 | + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); |
| 301 | + assertEquals(RestStatus.BAD_REQUEST.getStatus(), ex.status().getStatus()); |
| 302 | + } |
| 303 | + |
205 | 304 | public void testDeduplicate() throws IOException { |
206 | 305 | { |
207 | 306 | ShardSearchFailure failure = new ShardSearchFailure( |
|
0 commit comments