Esempio n. 1
0
  void assertAllSearchWorks(String indexName) {
    logger.info("--> testing _all search");
    SearchResponse searchRsp = client().prepareSearch(indexName).get();
    ElasticsearchAssertions.assertNoFailures(searchRsp);
    assertThat(searchRsp.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
    SearchHit bestHit = searchRsp.getHits().getAt(0);

    // Make sure there are payloads and they are taken into account for the score
    // the 'string' field has a boost of 4 in the mappings so it should get a payload boost
    String stringValue = (String) bestHit.sourceAsMap().get("string");
    assertNotNull(stringValue);
    Explanation explanation =
        client()
            .prepareExplain(indexName, bestHit.getType(), bestHit.getId())
            .setQuery(QueryBuilders.matchQuery("_all", stringValue))
            .get()
            .getExplanation();
    assertTrue(
        "Could not find payload boost in explanation\n" + explanation,
        findPayloadBoostInExplanation(explanation));

    // Make sure the query can run on the whole index
    searchRsp =
        client()
            .prepareSearch(indexName)
            .setQuery(QueryBuilders.matchQuery("_all", stringValue))
            .setExplain(true)
            .get();
    ElasticsearchAssertions.assertNoFailures(searchRsp);
    assertThat(searchRsp.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
  }
  private <T> T createSavedObjectWithHit(SearchHit hit, Query<T> query) {
    T object = createSavedObject(hit.getType(), hit.getId(), query);
    State objectState = State.getInstance(object);

    if (!objectState.isReferenceOnly()) {
      objectState.setValues(hit.getSource());
    }

    return swapObjectType(query, object);
  }
 private void indexSearchHit(
     BulkNodeClient bulkNodeClient, KnapsackPullRequest request, SearchHit hit)
     throws IOException {
   IndexRequest indexRequest =
       new IndexRequest(
           mapIndex(request, hit.getIndex()),
           mapType(request, hit.getIndex(), hit.getType()),
           hit.getId());
   for (String f : hit.getFields().keySet()) {
     switch (f) {
       case "_parent":
         indexRequest.parent(hit.getFields().get(f).getValue().toString());
         break;
       case "_routing":
         indexRequest.routing(hit.getFields().get(f).getValue().toString());
         break;
       case "_timestamp":
         indexRequest.timestamp(hit.getFields().get(f).getValue().toString());
         break;
       case "_version":
         indexRequest
             .versionType(VersionType.EXTERNAL)
             .version(Long.parseLong(hit.getFields().get(f).getValue().toString()));
         break;
       case "_source":
         indexRequest.source(hit.getSourceAsString());
         break;
       default:
         indexRequest.source(f, hit.getFields().get(f).getValue().toString());
         break;
     }
   }
   if (!hit.getFields().keySet().contains("_source")) {
     indexRequest.source(hit.getSourceAsString());
   }
   bulkNodeClient.bulkIndex(indexRequest);
 }
 @Override
 public void describeMismatchSafely(
     final SearchHit searchHit, final Description mismatchDescription) {
   mismatchDescription.appendText(" was ").appendValue(searchHit.getType());
 }
 @Override
 public boolean matchesSafely(final SearchHit searchHit) {
   return searchHit.getType().equals(type);
 }
Esempio n. 6
0
  @Test
  public void testParentWithMultipleBuckets() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(matchQuery("randomized", false))
            .addAggregation(
                terms("category")
                    .field("category")
                    .size(0)
                    .subAggregation(
                        children("to_comment")
                            .childType("comment")
                            .subAggregation(topHits("top_comments").addSort("_id", SortOrder.ASC))))
            .get();
    assertSearchResponse(searchResponse);

    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(3));

    for (Terms.Bucket bucket : categoryTerms.getBuckets()) {
      logger.info("bucket=" + bucket.getKey());
      Children childrenBucket = bucket.getAggregations().get("to_comment");
      TopHits topHits = childrenBucket.getAggregations().get("top_comments");
      logger.info("total_hits={}", topHits.getHits().getTotalHits());
      for (SearchHit searchHit : topHits.getHits()) {
        logger.info(
            "hit= {} {} {}", searchHit.sortValues()[0], searchHit.getType(), searchHit.getId());
      }
    }

    Terms.Bucket categoryBucket = categoryTerms.getBucketByKey("a");
    assertThat(categoryBucket.getKey(), equalTo("a"));
    assertThat(categoryBucket.getDocCount(), equalTo(3l));

    Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(2l));
    TopHits topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().totalHits(), equalTo(2l));
    assertThat(topHits.getHits().getAt(0).sortValues()[0].toString(), equalTo("a"));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("a"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    assertThat(topHits.getHits().getAt(1).sortValues()[0].toString(), equalTo("c"));
    assertThat(topHits.getHits().getAt(1).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(1).getType(), equalTo("comment"));

    categoryBucket = categoryTerms.getBucketByKey("b");
    assertThat(categoryBucket.getKey(), equalTo("b"));
    assertThat(categoryBucket.getDocCount(), equalTo(2l));

    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1l));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().totalHits(), equalTo(1l));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));

    categoryBucket = categoryTerms.getBucketByKey("c");
    assertThat(categoryBucket.getKey(), equalTo("c"));
    assertThat(categoryBucket.getDocCount(), equalTo(2l));

    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1l));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().totalHits(), equalTo(1l));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
  }
 @Override
 public String getType() {
   return delegate.getType();
 }
  public void testIndexTemplateWithAliases() throws Exception {

    client()
        .admin()
        .indices()
        .preparePutTemplate("template_with_aliases")
        .setTemplate("te*")
        .addMapping(
            "type1", "{\"type1\" : {\"properties\" : {\"value\" : {\"type\" : \"string\"}}}}")
        .addAlias(new Alias("simple_alias"))
        .addAlias(new Alias("templated_alias-{index}"))
        .addAlias(new Alias("filtered_alias").filter("{\"type\":{\"value\":\"type2\"}}"))
        .addAlias(
            new Alias("complex_filtered_alias")
                .filter(
                    FilterBuilders.termsFilter("_type", "typeX", "typeY", "typeZ")
                        .execution("bool")
                        .cache(true)))
        .get();

    assertAcked(
        prepareCreate("test_index")
            .addMapping("type1")
            .addMapping("type2")
            .addMapping("typeX")
            .addMapping("typeY")
            .addMapping("typeZ"));
    ensureGreen();

    client().prepareIndex("test_index", "type1", "1").setSource("field", "A value").get();
    client().prepareIndex("test_index", "type2", "2").setSource("field", "B value").get();
    client().prepareIndex("test_index", "typeX", "3").setSource("field", "C value").get();
    client().prepareIndex("test_index", "typeY", "4").setSource("field", "D value").get();
    client().prepareIndex("test_index", "typeZ", "5").setSource("field", "E value").get();

    GetAliasesResponse getAliasesResponse =
        client().admin().indices().prepareGetAliases().setIndices("test_index").get();
    assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
    assertThat(getAliasesResponse.getAliases().get("test_index").size(), equalTo(4));

    refresh();

    SearchResponse searchResponse = client().prepareSearch("test_index").get();
    assertHitCount(searchResponse, 5l);

    searchResponse = client().prepareSearch("simple_alias").get();
    assertHitCount(searchResponse, 5l);

    searchResponse = client().prepareSearch("templated_alias-test_index").get();
    assertHitCount(searchResponse, 5l);

    searchResponse = client().prepareSearch("filtered_alias").get();
    assertHitCount(searchResponse, 1l);
    assertThat(searchResponse.getHits().getAt(0).type(), equalTo("type2"));

    // Search the complex filter alias
    searchResponse = client().prepareSearch("complex_filtered_alias").get();
    assertHitCount(searchResponse, 3l);

    Set<String> types = Sets.newHashSet();
    for (SearchHit searchHit : searchResponse.getHits().getHits()) {
      types.add(searchHit.getType());
    }
    assertThat(types.size(), equalTo(3));
    assertThat(types, containsInAnyOrder("typeX", "typeY", "typeZ"));
  }