Search in sources :

Example 1 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project elasticsearch by elastic.

the class SharedSignificantTermsTestMethods method checkSignificantTermsAggregationCorrect.

private static void checkSignificantTermsAggregationCorrect(ESIntegTestCase testCase) {
    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");
    Assert.assertThat(classes.getBuckets().size(), equalTo(2));
    for (Terms.Bucket classBucket : classes.getBuckets()) {
        Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
        Assert.assertTrue(aggs.containsKey("sig_terms"));
        SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
        Assert.assertThat(agg.getBuckets().size(), equalTo(1));
        SignificantTerms.Bucket sigBucket = agg.iterator().next();
        String term = sigBucket.getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        Assert.assertTrue(term.equals(classTerm));
    }
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) AggregationBuilders.significantTerms(org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project sonarqube by SonarSource.

the class ActiveRuleIndex method processAggregations.

private static Multimap<String, FacetValue> processAggregations(@Nullable Aggregations aggregations) {
    Multimap<String, FacetValue> stats = ArrayListMultimap.create();
    if (aggregations == null) {
        return stats;
    }
    for (Aggregation aggregation : aggregations.asList()) {
        if (aggregation instanceof StringTerms) {
            for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
                FacetValue facetValue = new FacetValue(value.getKeyAsString(), value.getDocCount());
                stats.put(aggregation.getName(), facetValue);
            }
        } else if (aggregation instanceof InternalValueCount) {
            InternalValueCount count = (InternalValueCount) aggregation;
            FacetValue facetValue = new FacetValue(count.getName(), count.getValue());
            stats.put(count.getName(), facetValue);
        }
    }
    return stats;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalValueCount(org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount) FacetValue(org.sonar.server.search.FacetValue) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms)

Example 3 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project sonarqube by SonarSource.

the class ActiveRuleIndex method getStatsByProfileKeys.

public Map<String, Multimap<String, FacetValue>> getStatsByProfileKeys(List<String> keys) {
    SearchRequestBuilder request = getClient().prepareSearch(INDEX_TYPE_RULE.getIndex()).setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(FIELD_ACTIVE_RULE_PROFILE_KEY, keys)).filter(QueryBuilders.boolQuery().mustNot(QueryBuilders.hasParentQuery(INDEX_TYPE_RULE.getType(), QueryBuilders.termQuery(FIELD_RULE_STATUS, RuleStatus.REMOVED.name()))))).addAggregation(AggregationBuilders.terms(FIELD_ACTIVE_RULE_PROFILE_KEY).field(RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY).size(0).subAggregation(AggregationBuilders.terms(FIELD_ACTIVE_RULE_INHERITANCE).field(RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE)).subAggregation(AggregationBuilders.terms(FIELD_ACTIVE_RULE_SEVERITY).field(RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY)).subAggregation(AggregationBuilders.count(COUNT_ACTIVE_RULES))).setSize(0);
    SearchResponse response = request.get();
    Map<String, Multimap<String, FacetValue>> stats = new HashMap<>();
    Aggregation aggregation = response.getAggregations().get(FIELD_ACTIVE_RULE_PROFILE_KEY);
    for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
        stats.put(value.getKeyAsString(), processAggregations(value.getAggregations()));
    }
    return stats;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Multimap(com.google.common.collect.Multimap) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project elasticsearch by elastic.

the class NaNSortingIT method assertCorrectlySorted.

private void assertCorrectlySorted(Terms terms, boolean asc, SubAggregation agg) {
    assertThat(terms, notNullValue());
    double previousValue = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        Aggregation sub = bucket.getAggregations().get(agg.name);
        double value = agg.getValue(sub);
        assertTrue(Comparators.compareDiscardNaN(previousValue, value, asc) <= 0);
        previousValue = value;
    }
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms)

Example 5 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project elasticsearch by elastic.

the class SignificantTermsSignificanceScoreIT method testPlugin.

public void testPlugin() 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).significanceHeuristic(new SimpleHeuristic()).minDocCount(1))).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(2));
        Iterator<SignificantTerms.Bucket> bucketIterator = agg.iterator();
        SignificantTerms.Bucket sigBucket = bucketIterator.next();
        String term = sigBucket.getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        assertTrue(term.equals(classTerm));
        assertThat(sigBucket.getSignificanceScore(), closeTo(2.0, 1.e-8));
        sigBucket = bucketIterator.next();
        assertThat(sigBucket.getSignificanceScore(), closeTo(1.0, 1.e-8));
    }
    // we run the same test again but this time we do not call assertSearchResponse() before the assertions
    // the reason is that this would trigger toXContent and we would like to check that this has no potential side effects
    response = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation((significantTerms("sig_terms")).field(TEXT_FIELD).significanceHeuristic(new SimpleHeuristic()).minDocCount(1))).execute().actionGet();
    classes = (StringTerms) 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(2));
        Iterator<SignificantTerms.Bucket> bucketIterator = agg.iterator();
        SignificantTerms.Bucket sigBucket = bucketIterator.next();
        String term = sigBucket.getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        assertTrue(term.equals(classTerm));
        assertThat(sigBucket.getSignificanceScore(), closeTo(2.0, 1.e-8));
        sigBucket = bucketIterator.next();
        assertThat(sigBucket.getSignificanceScore(), closeTo(1.0, 1.e-8));
    }
}
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) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)53 HashMap (java.util.HashMap)24 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)19 List (java.util.List)18 ArrayList (java.util.ArrayList)17 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)17 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)16 Script (org.elasticsearch.script.Script)13 ScriptedMetric (org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric)13 Map (java.util.Map)10 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 Aggregation (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation)5 VertexiumException (org.vertexium.VertexiumException)5 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)4 IOException (java.io.IOException)4 SearchHit (org.elasticsearch.search.SearchHit)4 SearchHits (org.elasticsearch.search.SearchHits)4 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)4 BucketSpec (org.graylog.plugins.views.search.searchtypes.pivot.BucketSpec)4