Search in sources :

Example 21 with SignificantTerms

use of org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms in project elasticsearch by elastic.

the class SignificantTermsIT method testUnmapped.

public void testUnmapped() throws Exception {
    SearchResponse response = client().prepareSearch("idx_unmapped").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("description", "terje")).setFrom(0).setSize(60).setExplain(true).addAggregation(significantTerms("mySignificantTerms").field("fact_category").executionHint(randomExecutionHint()).minDocCount(2)).execute().actionGet();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    assertThat(topTerms.getBuckets().size(), equalTo(0));
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 22 with SignificantTerms

use of org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms in project elasticsearch by elastic.

the class SignificantTermsSignificanceScoreIT method testXContentResponse.

public void testXContentResponse() throws Exception {
    String type = randomBoolean() ? "text" : "long";
    String settings = "{\"index.number_of_shards\": 1, \"index.number_of_replicas\": 0}";
    SharedSignificantTermsTestMethods.index01Docs(type, settings, this);
    SearchResponse response = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD))).execute().actionGet();
    assertSearchResponse(response);
    StringTerms classes = response.getAggregations().get("class");
    assertThat(classes.getBuckets().size(), equalTo(2));
    for (Terms.Bucket classBucket : classes.getBuckets()) {
        Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
        assertTrue(aggs.containsKey("sig_terms"));
        SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
        assertThat(agg.getBuckets().size(), equalTo(1));
        String term = agg.iterator().next().getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        assertTrue(term.equals(classTerm));
    }
    XContentBuilder responseBuilder = XContentFactory.jsonBuilder();
    responseBuilder.startObject();
    classes.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS);
    responseBuilder.endObject();
    String result = "{\"class\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0," + "\"buckets\":[" + "{" + "\"key\":\"0\"," + "\"doc_count\":4," + "\"sig_terms\":{" + "\"doc_count\":4," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "0," : "\"0\",") + "\"doc_count\":4," + "\"score\":0.39999999999999997," + "\"bg_count\":5" + "}" + "]" + "}" + "}," + "{" + "\"key\":\"1\"," + "\"doc_count\":3," + "\"sig_terms\":{" + "\"doc_count\":3," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "1," : "\"1\",") + "\"doc_count\":3," + "\"score\":0.75," + "\"bg_count\":4" + "}]}}]}}";
    assertThat(responseBuilder.string(), equalTo(result));
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.significantTerms(org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 23 with SignificantTerms

use of org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms in project elasticsearch by elastic.

the class SignificantTermsSignificanceScoreIT method testScriptScore.

public void testScriptScore() throws ExecutionException, InterruptedException, IOException {
    indexRandomFrequencies01(randomBoolean() ? "text" : "long");
    ScriptHeuristic scriptHeuristic = getScriptSignificanceHeuristic();
    SearchResponse response = client().prepareSearch(INDEX_NAME).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("mySignificantTerms").field(TEXT_FIELD).executionHint(randomExecutionHint()).significanceHeuristic(scriptHeuristic).minDocCount(1).shardSize(2).size(2))).execute().actionGet();
    assertSearchResponse(response);
    for (Terms.Bucket classBucket : ((Terms) response.getAggregations().get("class")).getBuckets()) {
        SignificantTerms sigTerms = classBucket.getAggregations().get("mySignificantTerms");
        for (SignificantTerms.Bucket bucket : sigTerms.getBuckets()) {
            assertThat(bucket.getSignificanceScore(), is((double) bucket.getSubsetDf() + bucket.getSubsetSize() + bucket.getSupersetDf() + bucket.getSupersetSize()));
        }
    }
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) ScriptHeuristic(org.elasticsearch.search.aggregations.bucket.significant.heuristics.ScriptHeuristic) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.significantTerms(org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)23 SignificantTerms (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)23 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)14 Bucket (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket)6 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)6 AggregationBuilders.significantTerms (org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms)5 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 HashSet (java.util.HashSet)4 Aggregation (org.elasticsearch.search.aggregations.Aggregation)3 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)3 Aggregations (org.elasticsearch.search.aggregations.Aggregations)2 InternalFilter (org.elasticsearch.search.aggregations.bucket.filter.InternalFilter)2 ArrayList (java.util.ArrayList)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 ChiSquare (org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare)1 GND (org.elasticsearch.search.aggregations.bucket.significant.heuristics.GND)1 JLHScore (org.elasticsearch.search.aggregations.bucket.significant.heuristics.JLHScore)1