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));
}
}
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;
}
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;
}
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;
}
}
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));
}
}
Aggregations