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 testBasics() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type", "field1", "type=string"));
    ensureGreen();

    logger.info("--> register a queries");
    client()
        .prepareIndex("test", PercolatorService.TYPE_NAME, "1")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("query", matchQuery("field1", "b"))
                .field("a", "b")
                .endObject())
        .execute()
        .actionGet();
    client()
        .prepareIndex("test", PercolatorService.TYPE_NAME, "2")
        .setSource(
            jsonBuilder().startObject().field("query", matchQuery("field1", "c")).endObject())
        .execute()
        .actionGet();
    client()
        .prepareIndex("test", PercolatorService.TYPE_NAME, "3")
        .setSource(
            jsonBuilder()
                .startObject()
                .field(
                    "query",
                    boolQuery().must(matchQuery("field1", "b")).must(matchQuery("field1", "c")))
                .endObject())
        .execute()
        .actionGet();
    client()
        .prepareIndex("test", PercolatorService.TYPE_NAME, "4")
        .setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
        .execute()
        .actionGet();

    MultiPercolateResponse response =
        client()
            .prepareMultiPercolate()
            .add(
                client()
                    .preparePercolate()
                    .setIndices("test")
                    .setDocumentType("type")
                    .setPercolateDoc(
                        docBuilder()
                            .setDoc(jsonBuilder().startObject().field("field1", "b").endObject())))
            .add(
                client()
                    .preparePercolate()
                    .setIndices("test")
                    .setDocumentType("type")
                    .setPercolateDoc(
                        docBuilder()
                            .setDoc(yamlBuilder().startObject().field("field1", "c").endObject())))
            .add(
                client()
                    .preparePercolate()
                    .setIndices("test")
                    .setDocumentType("type")
                    .setPercolateDoc(
                        docBuilder()
                            .setDoc(
                                smileBuilder().startObject().field("field1", "b c").endObject())))
            .add(
                client()
                    .preparePercolate()
                    .setIndices("test")
                    .setDocumentType("type")
                    .setPercolateDoc(
                        docBuilder()
                            .setDoc(jsonBuilder().startObject().field("field1", "d").endObject())))
            .add(
                client()
                    .preparePercolate() // non existing doc, so error element
                    .setIndices("test")
                    .setDocumentType("type")
                    .setGetRequest(Requests.getRequest("test").type("type").id("5")))
            .execute()
            .actionGet();

    MultiPercolateResponse.Item item = response.getItems()[0];
    assertMatchCount(item.getResponse(), 2l);
    assertThat(item.getResponse().getMatches(), arrayWithSize(2));
    assertThat(item.getErrorMessage(), nullValue());
    assertThat(
        convertFromTextArray(item.getResponse().getMatches(), "test"),
        arrayContainingInAnyOrder("1", "4"));

    item = response.getItems()[1];
    assertThat(item.getErrorMessage(), nullValue());

    assertMatchCount(item.getResponse(), 2l);
    assertThat(item.getResponse().getMatches(), arrayWithSize(2));
    assertThat(
        convertFromTextArray(item.getResponse().getMatches(), "test"),
        arrayContainingInAnyOrder("2", "4"));

    item = response.getItems()[2];
    assertThat(item.getErrorMessage(), nullValue());
    assertMatchCount(item.getResponse(), 4l);
    assertThat(
        convertFromTextArray(item.getResponse().getMatches(), "test"),
        arrayContainingInAnyOrder("1", "2", "3", "4"));

    item = response.getItems()[3];
    assertThat(item.getErrorMessage(), nullValue());
    assertMatchCount(item.getResponse(), 1l);
    assertThat(item.getResponse().getMatches(), arrayWithSize(1));
    assertThat(convertFromTextArray(item.getResponse().getMatches(), "test"), arrayContaining("4"));

    item = response.getItems()[4];
    assertThat(item.getResponse(), nullValue());
    assertThat(item.getErrorMessage(), notNullValue());
    assertThat(item.getErrorMessage(), containsString("document missing"));
  }
  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));
  }