@Test public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() { TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); SearchResponse response = client .prepareSearch("blog") .setTypes("article") .addAggregation(aggregation) .execute() .actionGet(); Map<String, Aggregation> results = response.getAggregations().asMap(); StringTerms topTags = (StringTerms) results.get("top_tags"); List<String> keys = topTags.getBuckets().stream().map(b -> b.getKey()).collect(toList()); Collections.sort(keys); assertEquals( asList( "about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); }
/* * 找到所有职员中最大的共同点(兴趣爱好)是什么 */ @Test public void aggregationTest() { Client client = ClientTemplate.getClient(); SearchResponse response = client .prepareSearch("fccs") .setTypes("employee") .addAggregation(AggregationBuilders.terms("by_interests").field("interests")) .get(); StringTerms terms = response.getAggregations().get("by_interests"); List<Bucket> buckets = terms.getBuckets(); for (Bucket bucket : buckets) { System.out.println("key:" + bucket.getKey() + " doc_count:" + bucket.getDocCount()); } client.close(); }
@Test public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { TermsBuilder aggregation = AggregationBuilders.terms("top_tags") .field("tags") .order(Terms.Order.aggregation("_count", false)); SearchResponse response = client .prepareSearch("blog") .setTypes("article") .addAggregation(aggregation) .execute() .actionGet(); Map<String, Aggregation> results = response.getAggregations().asMap(); StringTerms topTags = (StringTerms) results.get("top_tags"); List<String> keys = topTags.getBuckets().stream().map(b -> b.getKey()).collect(toList()); assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); }
public void testScoresEqualForPositiveAndNegative(SignificanceHeuristicBuilder heuristic) throws Exception { // check that results for both classes are the same with exclude negatives = false and classes // are routing ids SearchResponse response = client() .prepareSearch("test") .addAggregation( new TermsBuilder("class") .field("class") .subAggregation( new SignificantTermsBuilder("mySignificantTerms") .field("text") .executionHint(randomExecutionHint()) .significanceHeuristic(heuristic) .minDocCount(1) .shardSize(1000) .size(1000))) .execute() .actionGet(); assertSearchResponse(response); StringTerms classes = (StringTerms) response.getAggregations().get("class"); assertThat(classes.getBuckets().size(), equalTo(2)); Iterator<Terms.Bucket> classBuckets = classes.getBuckets().iterator(); Collection<SignificantTerms.Bucket> classA = ((SignificantTerms) classBuckets.next().getAggregations().get("mySignificantTerms")) .getBuckets(); Iterator<SignificantTerms.Bucket> classBBucketIterator = ((SignificantTerms) classBuckets.next().getAggregations().get("mySignificantTerms")) .getBuckets() .iterator(); assertThat(classA.size(), greaterThan(0)); for (SignificantTerms.Bucket classABucket : classA) { SignificantTerms.Bucket classBBucket = classBBucketIterator.next(); assertThat(classABucket.getKey(), equalTo(classBBucket.getKey())); assertThat( classABucket.getSignificanceScore(), closeTo(classBBucket.getSignificanceScore(), 1.e-5)); } }
public void testNestNestedAggs() throws Exception { SearchResponse response = client() .prepareSearch("idx_nested_nested_aggs") .addAggregation( nested("level1", "nested1") .subAggregation( terms("a") .field("nested1.a") .collectMode(aggCollectionMode) .subAggregation( nested("level2", "nested1.nested2") .subAggregation(sum("sum").field("nested1.nested2.b"))))) .get(); assertSearchResponse(response); Nested level1 = response.getAggregations().get("level1"); assertThat(level1, notNullValue()); assertThat(level1.getName(), equalTo("level1")); assertThat(level1.getDocCount(), equalTo(2L)); StringTerms a = level1.getAggregations().get("a"); Terms.Bucket bBucket = a.getBucketByKey("a"); assertThat(bBucket.getDocCount(), equalTo(1L)); Nested level2 = bBucket.getAggregations().get("level2"); assertThat(level2.getDocCount(), equalTo(1L)); Sum sum = level2.getAggregations().get("sum"); assertThat(sum.getValue(), equalTo(2d)); a = level1.getAggregations().get("a"); bBucket = a.getBucketByKey("b"); assertThat(bBucket.getDocCount(), equalTo(1L)); level2 = bBucket.getAggregations().get("level2"); assertThat(level2.getDocCount(), equalTo(1L)); sum = level2.getAggregations().get("sum"); assertThat(sum.getValue(), equalTo(2d)); }
public void testXContentResponse() throws Exception { String type = randomBoolean() ? "string" : "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( new TermsBuilder("class") .field(CLASS_FIELD) .subAggregation(new SignificantTermsBuilder("sig_terms").field(TEXT_FIELD))) .execute() .actionGet(); assertSearchResponse(response); StringTerms 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(1)); String term = agg.iterator().next().getKeyAsString(); String classTerm = classBucket.getKeyAsString(); assertTrue(term.equals(classTerm)); } XContentBuilder responseBuilder = XContentFactory.jsonBuilder(); classes.toXContent(responseBuilder, null); String result = null; if (type.equals("long")) { 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\":0,\"key_as_string\":\"0\",\"doc_count\":4,\"score\":0.39999999999999997,\"bg_count\":5}]}},{\"key\":\"1\",\"doc_count\":3,\"sig_terms\":{\"doc_count\":3,\"buckets\":[{\"key\":1,\"key_as_string\":\"1\",\"doc_count\":3,\"score\":0.75,\"bg_count\":4}]}}]}"; } else { 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\":\"0\",\"doc_count\":4,\"score\":0.39999999999999997,\"bg_count\":5}]}},{\"key\":\"1\",\"doc_count\":3,\"sig_terms\":{\"doc_count\":3,\"buckets\":[{\"key\":\"1\",\"doc_count\":3,\"score\":0.75,\"bg_count\":4}]}}]}"; } assertThat(responseBuilder.string(), equalTo(result)); }
@Override public Map<String, Integer> countTermsByField(SearchCriteria searchCriteria, String field) { ElasticSearchCriteria criteria = (ElasticSearchCriteria) searchCriteria; if (criteria == null) return null; SearchRequestBuilder srb = criteria2builder(criteria); srb.setFrom(0); srb.setSize(0); TermsBuilder tb = AggregationBuilders.terms(field); tb.field(field); srb.addAggregation(tb); try { SearchResponse response = srb.execute().get(); StringTerms aggr = response.getAggregations().get(field); Map<String, Integer> result = new LinkedHashMap<>(); for (Terms.Bucket bucket : aggr.getBuckets()) { result.put(bucket.getKey(), (int) bucket.getDocCount()); } return result; } catch (Exception e) { logger.error(e.getMessage(), e); } return Collections.emptyMap(); }
@Override public StringTerms readResult(StreamInput in) throws IOException { StringTerms buckets = new StringTerms(); buckets.readFrom(in); return buckets; }
public void testPlugin() throws Exception { String type = randomBoolean() ? "string" : "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( new TermsBuilder("class") .field(CLASS_FIELD) .subAggregation( (new SignificantTermsBuilder("sig_terms")) .field(TEXT_FIELD) .significanceHeuristic(new SimpleHeuristic.SimpleHeuristicBuilder()) .minDocCount(1))) .execute() .actionGet(); assertSearchResponse(response); StringTerms 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)); } // 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( new TermsBuilder("class") .field(CLASS_FIELD) .subAggregation( (new SignificantTermsBuilder("sig_terms")) .field(TEXT_FIELD) .significanceHeuristic(new SimpleHeuristic.SimpleHeuristicBuilder()) .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)); } }