예제 #1
0
  @Test
  // Just test the integration with facets and aggregations, not the facet and aggregation
  // functionality!
  public void testFacetsAndAggregations() throws Exception {
    client().admin().indices().prepareCreate("test").execute().actionGet();
    ensureGreen();

    int numQueries = atLeast(250);
    int numUniqueQueries = between(1, numQueries / 2);
    String[] values = new String[numUniqueQueries];
    for (int i = 0; i < values.length; i++) {
      values[i] = "value" + i;
    }
    int[] expectedCount = new int[numUniqueQueries];

    logger.info("--> registering {} queries", numQueries);
    for (int i = 0; i < numQueries; i++) {
      String value = values[i % numUniqueQueries];
      expectedCount[i % numUniqueQueries]++;
      QueryBuilder queryBuilder = matchQuery("field1", value);
      client()
          .prepareIndex("test", PercolatorService.TYPE_NAME, Integer.toString(i))
          .setSource(
              jsonBuilder()
                  .startObject()
                  .field("query", queryBuilder)
                  .field("field2", "b")
                  .endObject())
          .execute()
          .actionGet();
    }
    client().admin().indices().prepareRefresh("test").execute().actionGet();

    for (int i = 0; i < numQueries; i++) {
      String value = values[i % numUniqueQueries];
      PercolateRequestBuilder percolateRequestBuilder =
          client()
              .preparePercolate()
              .setIndices("test")
              .setDocumentType("type")
              .setPercolateDoc(
                  docBuilder()
                      .setDoc(jsonBuilder().startObject().field("field1", value).endObject()));

      boolean useAggs = randomBoolean();
      if (useAggs) {
        percolateRequestBuilder.addAggregation(AggregationBuilders.terms("a").field("field2"));
      } else {
        percolateRequestBuilder.addFacet(FacetBuilders.termsFacet("a").field("field2"));
      }

      if (randomBoolean()) {
        percolateRequestBuilder.setPercolateQuery(matchAllQuery());
      }
      if (randomBoolean()) {
        percolateRequestBuilder.setScore(true);
      } else {
        percolateRequestBuilder.setSortByScore(true).setSize(numQueries);
      }

      boolean countOnly = randomBoolean();
      if (countOnly) {
        percolateRequestBuilder.setOnlyCount(countOnly);
      }

      PercolateResponse response = percolateRequestBuilder.execute().actionGet();
      assertMatchCount(response, expectedCount[i % numUniqueQueries]);
      if (!countOnly) {
        assertThat(response.getMatches(), arrayWithSize(expectedCount[i % numUniqueQueries]));
      }

      if (useAggs) {
        List<Aggregation> aggregations = response.getAggregations().asList();
        assertThat(aggregations.size(), equalTo(1));
        assertThat(aggregations.get(0).getName(), equalTo("a"));
        List<Terms.Bucket> buckets =
            new ArrayList<Terms.Bucket>(((Terms) aggregations.get(0)).buckets());
        assertThat(buckets.size(), equalTo(1));
        assertThat(buckets.get(0).getKey().string(), equalTo("b"));
        assertThat(buckets.get(0).getDocCount(), equalTo((long) expectedCount[i % values.length]));
      } else {
        assertThat(response.getFacets().facets().size(), equalTo(1));
        assertThat(response.getFacets().facets().get(0).getName(), equalTo("a"));
        assertThat(
            ((TermsFacet) response.getFacets().facets().get(0)).getEntries().size(), equalTo(1));
        assertThat(
            ((TermsFacet) response.getFacets().facets().get(0)).getEntries().get(0).getCount(),
            equalTo(expectedCount[i % values.length]));
        assertThat(
            ((TermsFacet) response.getFacets().facets().get(0))
                .getEntries()
                .get(0)
                .getTerm()
                .string(),
            equalTo("b"));
      }
    }
  }
  public void testOldPercolatorIndex() throws Exception {
    setupNode();

    // verify cluster state:
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    assertThat(state.metaData().indices().size(), equalTo(1));
    assertThat(state.metaData().indices().get(INDEX_NAME), notNullValue());
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getCreationVersion(), equalTo(Version.V_2_0_0));
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getUpgradedVersion(), equalTo(Version.CURRENT));
    assertThat(state.metaData().indices().get(INDEX_NAME).getMappings().size(), equalTo(2));
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getMappings().get(".percolator"),
        notNullValue());
    // important: verify that the query field in the .percolator mapping is of type object (from
    // 3.0.0 this is of type percolator)
    MappingMetaData mappingMetaData =
        state.metaData().indices().get(INDEX_NAME).getMappings().get(".percolator");
    assertThat(
        XContentMapValues.extractValue("properties.query.type", mappingMetaData.sourceAsMap()),
        equalTo("object"));
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getMappings().get("message"), notNullValue());

    // verify existing percolator queries:
    SearchResponse searchResponse =
        client()
            .prepareSearch(INDEX_NAME)
            .setTypes(".percolator")
            .addSort("_id", SortOrder.ASC)
            .get();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
    assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).id(), equalTo("2"));
    assertThat(searchResponse.getHits().getAt(2).id(), equalTo("3"));

    // verify percolate response
    PercolateResponse percolateResponse =
        client()
            .preparePercolate()
            .setIndices(INDEX_NAME)
            .setDocumentType("message")
            .setPercolateDoc(
                new PercolateSourceBuilder.DocBuilder()
                    .setDoc("message", "the quick brown fox jumps over the lazy dog"))
            .get();

    assertThat(percolateResponse.getCount(), equalTo(2L));
    assertThat(percolateResponse.getMatches().length, equalTo(2));
    assertThat(percolateResponse.getMatches()[0].getId().string(), equalTo("1"));
    assertThat(percolateResponse.getMatches()[1].getId().string(), equalTo("2"));

    // add an extra query and verify the results
    client()
        .prepareIndex(INDEX_NAME, ".percolator", "4")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("query", matchQuery("message", "fox jumps"))
                .endObject())
        .get();
    refresh();

    percolateResponse =
        client()
            .preparePercolate()
            .setIndices(INDEX_NAME)
            .setDocumentType("message")
            .setPercolateDoc(
                new PercolateSourceBuilder.DocBuilder()
                    .setDoc("message", "the quick brown fox jumps over the lazy dog"))
            .get();

    assertThat(percolateResponse.getCount(), equalTo(3L));
    assertThat(percolateResponse.getMatches().length, equalTo(3));
    assertThat(percolateResponse.getMatches()[0].getId().string(), equalTo("1"));
    assertThat(percolateResponse.getMatches()[1].getId().string(), equalTo("2"));
    assertThat(percolateResponse.getMatches()[2].getId().string(), equalTo("4"));
  }