@Override
  public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    if (scrollId != null) {
      builder.field(Fields._SCROLL_ID, scrollId);
    }
    builder.field(Fields.TOOK, tookInMillis);
    builder.field(Fields.TIMED_OUT, isTimedOut());
    if (isTerminatedEarly() != null) {
      builder.field(Fields.TERMINATED_EARLY, isTerminatedEarly());
    }
    builder.startObject(Fields._SHARDS);
    builder.field(Fields.TOTAL, getTotalShards());
    builder.field(Fields.SUCCESSFUL, getSuccessfulShards());
    builder.field(Fields.FAILED, getFailedShards());

    if (shardFailures.length > 0) {
      builder.startArray(Fields.FAILURES);
      for (ShardSearchFailure shardFailure : shardFailures) {
        builder.startObject();
        if (shardFailure.shard() != null) {
          builder.field(Fields.INDEX, shardFailure.shard().index());
          builder.field(Fields.SHARD, shardFailure.shard().shardId());
        }
        builder.field(Fields.STATUS, shardFailure.status().getStatus());
        builder.field(Fields.REASON, shardFailure.reason());
        builder.endObject();
      }
      builder.endArray();
    }

    builder.endObject();
    internalResponse.toXContent(builder, params);
    return builder;
  }
 private void checkExceptions(String script) {
   try {
     SearchResponse sr =
         client()
             .prepareSearch("test")
             .setQuery(QueryBuilders.matchAllQuery())
             .addScriptField("tvtest", script)
             .execute()
             .actionGet();
     assertThat(sr.getHits().hits().length, equalTo(0));
     ShardSearchFailure[] shardFails = sr.getShardFailures();
     for (ShardSearchFailure fail : shardFails) {
       assertThat(
           fail.reason()
               .indexOf(
                   "Cannot iterate twice! If you want to iterate more that once, add _CACHE explicitly."),
           Matchers.greaterThan(-1));
     }
   } catch (SearchPhaseExecutionException ex) {
     assertThat(
         "got " + ex.getDetailedMessage(),
         ex.getDetailedMessage()
             .indexOf(
                 "Cannot iterate twice! If you want to iterate more that once, add _CACHE explicitly."),
         Matchers.greaterThan(-1));
   }
 }
  @Override
  public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    internalResponse.writeTo(out);
    out.writeVInt(totalShards);
    out.writeVInt(successfulShards);

    out.writeVInt(shardFailures.length);
    for (ShardSearchFailure shardSearchFailure : shardFailures) {
      shardSearchFailure.writeTo(out);
    }

    out.writeOptionalString(scrollId);
    out.writeVLong(tookInMillis);
  }
Example #4
0
 private void assertShardExecutionState(SearchResponse response, int expectedFailures)
     throws Exception {
   ShardSearchFailure[] failures = response.getShardFailures();
   if (failures.length != expectedFailures) {
     for (ShardSearchFailure failure : failures) {
       logger.error(
           (Supplier<?>) () -> new ParameterizedMessage("Shard Failure: {}", failure),
           failure.getCause());
     }
     fail("Unexpected shard failures!");
   }
   assertThat(
       "Not all shards are initialized",
       response.getSuccessfulShards(),
       equalTo(response.getTotalShards()));
 }
 private Response wrap(SearchResponse response) {
   List<SearchFailure> failures;
   if (response.getShardFailures() == null) {
     failures = emptyList();
   } else {
     failures = new ArrayList<>(response.getShardFailures().length);
     for (ShardSearchFailure failure : response.getShardFailures()) {
       String nodeId = failure.shard() == null ? null : failure.shard().nodeId();
       failures.add(
           new SearchFailure(failure.getCause(), failure.index(), failure.shardId(), nodeId));
     }
   }
   List<Hit> hits;
   if (response.getHits().getHits() == null || response.getHits().getHits().length == 0) {
     hits = emptyList();
   } else {
     hits = new ArrayList<>(response.getHits().getHits().length);
     for (SearchHit hit : response.getHits().getHits()) {
       hits.add(new ClientHit(hit));
     }
     hits = unmodifiableList(hits);
   }
   return new Response(
       response.isTimedOut(),
       failures,
       response.getHits().getTotalHits(),
       hits,
       response.getScrollId());
 }