예제 #1
0
  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));
  }
예제 #2
0
  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));
  }