@Override
  protected MultiGetShardResponse shardOperation(MultiGetShardRequest request, int shardId)
      throws ElasticSearchException {
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(shardId);

    if (request.refresh() && !request.realtime()) {
      indexShard.refresh(
          new Engine.Refresh("refresh_flag_mget").force(TransportGetAction.REFRESH_FORCE));
    }

    MultiGetShardResponse response = new MultiGetShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
      String type = request.types.get(i);
      String id = request.ids.get(i);
      String[] fields = request.fields.get(i);

      long version = request.versions.get(i);
      VersionType versionType = request.versionTypes.get(i);
      if (versionType == null) {
        versionType = VersionType.INTERNAL;
      }

      FetchSourceContext fetchSourceContext = request.fetchSourceContexts.get(i);
      try {
        GetResult getResult =
            indexShard
                .getService()
                .get(
                    type, id, fields, request.realtime(), version, versionType, fetchSourceContext);
        response.add(request.locations.get(i), new GetResponse(getResult));
      } catch (Throwable t) {
        if (TransportActions.isShardNotAvailableException(t)) {
          throw (ElasticSearchException) t;
        } else {
          logger.debug(
              "[{}][{}] failed to execute multi_get for [{}]/[{}]",
              t,
              request.index(),
              shardId,
              type,
              id);
          response.add(
              request.locations.get(i),
              new MultiGetResponse.Failure(
                  request.index(), type, id, ExceptionsHelper.detailedMessage(t)));
        }
      }
    }

    return response;
  }
 @Override
 protected ShardIterator shards(ClusterState state, MultiGetShardRequest request) {
   return clusterService
       .operationRouting()
       .getShards(
           clusterService.state(), request.index(), request.shardId(), request.preference());
 }
 @Override
 protected ClusterBlockException checkRequestBlock(
     ClusterState state, MultiGetShardRequest request) {
   return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index());
 }
  public void testSerialization() throws IOException {
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    if (randomBoolean()) {
      multiGetRequest.preference(randomAsciiOfLength(randomIntBetween(1, 10)));
    }
    if (randomBoolean()) {
      multiGetRequest.realtime(false);
    }
    if (randomBoolean()) {
      multiGetRequest.refresh(true);
    }
    multiGetRequest.ignoreErrorsOnGeneratedFields(randomBoolean());

    MultiGetShardRequest multiGetShardRequest =
        new MultiGetShardRequest(multiGetRequest, "index", 0);
    int numItems = iterations(10, 30);
    for (int i = 0; i < numItems; i++) {
      MultiGetRequest.Item item =
          new MultiGetRequest.Item(
              "alias-" + randomAsciiOfLength(randomIntBetween(1, 10)), "type", "id-" + i);
      if (randomBoolean()) {
        int numFields = randomIntBetween(1, 5);
        String[] fields = new String[numFields];
        for (int j = 0; j < fields.length; j++) {
          fields[j] = randomAsciiOfLength(randomIntBetween(1, 10));
        }
        item.fields(fields);
      }
      if (randomBoolean()) {
        item.version(randomIntBetween(1, Integer.MAX_VALUE));
        item.versionType(randomFrom(VersionType.values()));
      }
      if (randomBoolean()) {
        item.fetchSourceContext(new FetchSourceContext(randomBoolean()));
      }
      multiGetShardRequest.add(0, item);
    }

    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(randomVersion(random()));
    multiGetShardRequest.writeTo(out);

    StreamInput in = StreamInput.wrap(out.bytes());
    in.setVersion(out.getVersion());
    MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest();
    multiGetShardRequest2.readFrom(in);

    assertThat(multiGetShardRequest2.index(), equalTo(multiGetShardRequest.index()));
    assertThat(multiGetShardRequest2.preference(), equalTo(multiGetShardRequest.preference()));
    assertThat(multiGetShardRequest2.realtime(), equalTo(multiGetShardRequest.realtime()));
    assertThat(multiGetShardRequest2.refresh(), equalTo(multiGetShardRequest.refresh()));
    assertThat(
        multiGetShardRequest2.ignoreErrorsOnGeneratedFields(),
        equalTo(multiGetShardRequest.ignoreErrorsOnGeneratedFields()));
    assertThat(multiGetShardRequest2.items.size(), equalTo(multiGetShardRequest.items.size()));
    for (int i = 0; i < multiGetShardRequest2.items.size(); i++) {
      MultiGetRequest.Item item = multiGetShardRequest.items.get(i);
      MultiGetRequest.Item item2 = multiGetShardRequest2.items.get(i);
      assertThat(item2.index(), equalTo(item.index()));
      assertThat(item2.type(), equalTo(item.type()));
      assertThat(item2.id(), equalTo(item.id()));
      assertThat(item2.fields(), equalTo(item.fields()));
      assertThat(item2.version(), equalTo(item.version()));
      assertThat(item2.versionType(), equalTo(item.versionType()));
      assertThat(item2.fetchSourceContext(), equalTo(item.fetchSourceContext()));
    }
    assertThat(multiGetShardRequest2.indices(), equalTo(multiGetShardRequest.indices()));
    assertThat(
        multiGetShardRequest2.indicesOptions(), equalTo(multiGetShardRequest.indicesOptions()));
  }