public void testNestedMultiPercolation() throws IOException {
   initNestedIndexAndPercolation();
   MultiPercolateRequestBuilder mpercolate = client().prepareMultiPercolate();
   mpercolate.add(
       client()
           .preparePercolate()
           .setPercolateDoc(
               new PercolateSourceBuilder.DocBuilder().setDoc(getNotMatchingNestedDoc()))
           .setIndices("nestedindex")
           .setDocumentType("company"));
   mpercolate.add(
       client()
           .preparePercolate()
           .setPercolateDoc(new PercolateSourceBuilder.DocBuilder().setDoc(getMatchingNestedDoc()))
           .setIndices("nestedindex")
           .setDocumentType("company"));
   MultiPercolateResponse response = mpercolate.get();
   assertEquals(response.getItems()[0].getResponse().getMatches().length, 0);
   assertEquals(response.getItems()[1].getResponse().getMatches().length, 1);
   assertEquals(response.getItems()[1].getResponse().getMatches()[0].getId().string(), "Q");
 }
  public void testWithDocsOnly() throws Exception {
    createIndex("test");
    ensureGreen();

    NumShards test = getNumShards("test");

    int numQueries = randomIntBetween(50, 100);
    logger.info("--> register a queries");
    for (int i = 0; i < numQueries; i++) {
      client()
          .prepareIndex("test", PercolatorService.TYPE_NAME, Integer.toString(i))
          .setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
          .execute()
          .actionGet();
    }

    MultiPercolateRequestBuilder builder = client().prepareMultiPercolate();
    int numPercolateRequest = randomIntBetween(50, 100);
    for (int i = 0; i < numPercolateRequest; i++) {
      builder.add(
          client()
              .preparePercolate()
              .setIndices("test")
              .setDocumentType("type")
              .setPercolateDoc(
                  docBuilder()
                      .setDoc(jsonBuilder().startObject().field("field", "a").endObject())));
    }

    MultiPercolateResponse response = builder.execute().actionGet();
    assertThat(response.items().length, equalTo(numPercolateRequest));
    for (MultiPercolateResponse.Item item : response) {
      assertThat(item.isFailure(), equalTo(false));
      assertMatchCount(item.getResponse(), numQueries);
      assertThat(item.getResponse().getMatches().length, equalTo(numQueries));
    }

    // All illegal json
    builder = client().prepareMultiPercolate();
    for (int i = 0; i < numPercolateRequest; i++) {
      builder.add(
          client()
              .preparePercolate()
              .setIndices("test")
              .setDocumentType("type")
              .setSource("illegal json"));
    }

    response = builder.execute().actionGet();
    assertThat(response.items().length, equalTo(numPercolateRequest));
    for (MultiPercolateResponse.Item item : response) {
      assertThat(item.isFailure(), equalTo(false));
      assertThat(item.getResponse().getSuccessfulShards(), equalTo(0));
      assertThat(item.getResponse().getShardFailures().length, equalTo(test.numPrimaries));
      for (ShardOperationFailedException shardFailure : item.getResponse().getShardFailures()) {
        assertThat(shardFailure.reason(), containsString("Failed to derive xcontent"));
        assertThat(shardFailure.status().getStatus(), equalTo(400));
      }
    }

    // one valid request
    builder = client().prepareMultiPercolate();
    for (int i = 0; i < numPercolateRequest; i++) {
      builder.add(
          client()
              .preparePercolate()
              .setIndices("test")
              .setDocumentType("type")
              .setSource("illegal json"));
    }
    builder.add(
        client()
            .preparePercolate()
            .setIndices("test")
            .setDocumentType("type")
            .setPercolateDoc(
                docBuilder().setDoc(jsonBuilder().startObject().field("field", "a").endObject())));

    response = builder.execute().actionGet();
    assertThat(response.items().length, equalTo(numPercolateRequest + 1));
    assertThat(response.items()[numPercolateRequest].isFailure(), equalTo(false));
    assertMatchCount(response.items()[numPercolateRequest].getResponse(), numQueries);
    assertThat(
        response.items()[numPercolateRequest].getResponse().getMatches().length,
        equalTo(numQueries));
  }
  public void testExistingDocsOnly() throws Exception {
    createIndex("test");

    int numQueries = randomIntBetween(50, 100);
    logger.info("--> register a queries");
    for (int i = 0; i < numQueries; i++) {
      client()
          .prepareIndex("test", PercolatorService.TYPE_NAME, Integer.toString(i))
          .setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
          .execute()
          .actionGet();
    }

    client()
        .prepareIndex("test", "type", "1")
        .setSource(jsonBuilder().startObject().field("field", "a"))
        .execute()
        .actionGet();

    MultiPercolateRequestBuilder builder = client().prepareMultiPercolate();
    int numPercolateRequest = randomIntBetween(50, 100);
    for (int i = 0; i < numPercolateRequest; i++) {
      builder.add(
          client()
              .preparePercolate()
              .setGetRequest(Requests.getRequest("test").type("type").id("1"))
              .setIndices("test")
              .setDocumentType("type"));
    }

    MultiPercolateResponse response = builder.execute().actionGet();
    assertThat(response.items().length, equalTo(numPercolateRequest));
    for (MultiPercolateResponse.Item item : response) {
      assertThat(item.isFailure(), equalTo(false));
      assertMatchCount(item.getResponse(), numQueries);
      assertThat(item.getResponse().getMatches().length, equalTo(numQueries));
    }

    // Non existing doc
    builder = client().prepareMultiPercolate();
    for (int i = 0; i < numPercolateRequest; i++) {
      builder.add(
          client()
              .preparePercolate()
              .setGetRequest(Requests.getRequest("test").type("type").id("2"))
              .setIndices("test")
              .setDocumentType("type"));
    }

    response = builder.execute().actionGet();
    assertThat(response.items().length, equalTo(numPercolateRequest));
    for (MultiPercolateResponse.Item item : response) {
      assertThat(item.isFailure(), equalTo(true));
      assertThat(item.getErrorMessage(), containsString("document missing"));
      assertThat(item.getResponse(), nullValue());
    }

    // One existing doc
    builder = client().prepareMultiPercolate();
    for (int i = 0; i < numPercolateRequest; i++) {
      builder.add(
          client()
              .preparePercolate()
              .setGetRequest(Requests.getRequest("test").type("type").id("2"))
              .setIndices("test")
              .setDocumentType("type"));
    }
    builder.add(
        client()
            .preparePercolate()
            .setGetRequest(Requests.getRequest("test").type("type").id("1"))
            .setIndices("test")
            .setDocumentType("type"));

    response = builder.execute().actionGet();
    assertThat(response.items().length, equalTo(numPercolateRequest + 1));
    assertThat(response.items()[numPercolateRequest].isFailure(), equalTo(false));
    assertMatchCount(response.items()[numPercolateRequest].getResponse(), numQueries);
    assertThat(
        response.items()[numPercolateRequest].getResponse().getMatches().length,
        equalTo(numQueries));
  }