Search in sources :

Example 1 with InternalFilter

use of org.elasticsearch.search.aggregations.bucket.filter.InternalFilter in project elasticsearch by elastic.

the class SignificantTermsSignificanceScoreIT method testBackgroundVsSeparateSet.

// compute significance score by
// 1. terms agg on class and significant terms
// 2. filter buckets and set the background to the other class and set is_background false
// both should yield exact same result
public void testBackgroundVsSeparateSet(SignificanceHeuristic significanceHeuristicExpectingSuperset, SignificanceHeuristic significanceHeuristicExpectingSeparateSets) throws Exception {
    SearchResponse response1 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD).minDocCount(1).significanceHeuristic(significanceHeuristicExpectingSuperset))).execute().actionGet();
    assertSearchResponse(response1);
    SearchResponse response2 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(filter("0", QueryBuilders.termQuery(CLASS_FIELD, "0")).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD).minDocCount(1).backgroundFilter(QueryBuilders.termQuery(CLASS_FIELD, "1")).significanceHeuristic(significanceHeuristicExpectingSeparateSets))).addAggregation(filter("1", QueryBuilders.termQuery(CLASS_FIELD, "1")).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD).minDocCount(1).backgroundFilter(QueryBuilders.termQuery(CLASS_FIELD, "0")).significanceHeuristic(significanceHeuristicExpectingSeparateSets))).execute().actionGet();
    StringTerms classes = response1.getAggregations().get("class");
    SignificantTerms sigTerms0 = ((SignificantTerms) (classes.getBucketByKey("0").getAggregations().asMap().get("sig_terms")));
    assertThat(sigTerms0.getBuckets().size(), equalTo(2));
    double score00Background = sigTerms0.getBucketByKey("0").getSignificanceScore();
    double score01Background = sigTerms0.getBucketByKey("1").getSignificanceScore();
    SignificantTerms sigTerms1 = ((SignificantTerms) (classes.getBucketByKey("1").getAggregations().asMap().get("sig_terms")));
    double score10Background = sigTerms1.getBucketByKey("0").getSignificanceScore();
    double score11Background = sigTerms1.getBucketByKey("1").getSignificanceScore();
    Aggregations aggs = response2.getAggregations();
    sigTerms0 = (SignificantTerms) ((InternalFilter) aggs.get("0")).getAggregations().getAsMap().get("sig_terms");
    double score00SeparateSets = sigTerms0.getBucketByKey("0").getSignificanceScore();
    double score01SeparateSets = sigTerms0.getBucketByKey("1").getSignificanceScore();
    sigTerms1 = (SignificantTerms) ((InternalFilter) aggs.get("1")).getAggregations().getAsMap().get("sig_terms");
    double score10SeparateSets = sigTerms1.getBucketByKey("0").getSignificanceScore();
    double score11SeparateSets = sigTerms1.getBucketByKey("1").getSignificanceScore();
    assertThat(score00Background, equalTo(score00SeparateSets));
    assertThat(score01Background, equalTo(score01SeparateSets));
    assertThat(score10Background, equalTo(score10SeparateSets));
    assertThat(score11Background, equalTo(score11SeparateSets));
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) InternalFilter(org.elasticsearch.search.aggregations.bucket.filter.InternalFilter) Aggregations(org.elasticsearch.search.aggregations.Aggregations) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with InternalFilter

use of org.elasticsearch.search.aggregations.bucket.filter.InternalFilter in project elasticsearch by elastic.

the class TermsShardMinDocCountIT method testShardMinDocCountSignificantTermsTest.

// see https://github.com/elastic/elasticsearch/issues/5998
public void testShardMinDocCountSignificantTermsTest() throws Exception {
    String textMappings;
    if (randomBoolean()) {
        textMappings = "type=long";
    } else {
        textMappings = "type=text,fielddata=true";
    }
    assertAcked(prepareCreate(index).setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0).addMapping(type, "text", textMappings));
    List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
    //high score but low doc freq
    addTermsDocs("1", 1, 0, indexBuilders);
    addTermsDocs("2", 1, 0, indexBuilders);
    addTermsDocs("3", 1, 0, indexBuilders);
    addTermsDocs("4", 1, 0, indexBuilders);
    //low score but high doc freq
    addTermsDocs("5", 3, 1, indexBuilders);
    addTermsDocs("6", 3, 1, indexBuilders);
    // make sure the terms all get score > 0 except for this one
    addTermsDocs("7", 0, 3, indexBuilders);
    indexRandom(true, false, indexBuilders);
    // first, check that indeed when not setting the shardMinDocCount parameter 0 terms are returned
    SearchResponse response = client().prepareSearch(index).addAggregation((filter("inclass", QueryBuilders.termQuery("class", true))).subAggregation(significantTerms("mySignificantTerms").field("text").minDocCount(2).size(2).executionHint(randomExecutionHint()))).execute().actionGet();
    assertSearchResponse(response);
    InternalFilter filteredBucket = response.getAggregations().get("inclass");
    SignificantTerms sigterms = filteredBucket.getAggregations().get("mySignificantTerms");
    assertThat(sigterms.getBuckets().size(), equalTo(0));
    response = client().prepareSearch(index).addAggregation((filter("inclass", QueryBuilders.termQuery("class", true))).subAggregation(significantTerms("mySignificantTerms").field("text").minDocCount(2).shardMinDocCount(2).size(2).executionHint(randomExecutionHint()))).execute().actionGet();
    assertSearchResponse(response);
    filteredBucket = response.getAggregations().get("inclass");
    sigterms = filteredBucket.getAggregations().get("mySignificantTerms");
    assertThat(sigterms.getBuckets().size(), equalTo(2));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) InternalFilter(org.elasticsearch.search.aggregations.bucket.filter.InternalFilter) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)2 InternalFilter (org.elasticsearch.search.aggregations.bucket.filter.InternalFilter)2 SignificantTerms (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms)2 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)2 ArrayList (java.util.ArrayList)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 Aggregations (org.elasticsearch.search.aggregations.Aggregations)1 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)1