|
19 | 19 | package org.elasticsearch.search.aggregations.bucket; |
20 | 20 |
|
21 | 21 | import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; |
| 22 | +import org.elasticsearch.action.index.IndexRequestBuilder; |
22 | 23 | import org.elasticsearch.action.search.SearchResponse; |
23 | 24 | import org.elasticsearch.action.search.SearchType; |
24 | 25 | import org.elasticsearch.common.settings.ImmutableSettings; |
25 | 26 | import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 29 | +import org.elasticsearch.common.xcontent.XContentParser; |
26 | 30 | import org.elasticsearch.index.query.FilterBuilders; |
27 | 31 | import org.elasticsearch.index.query.TermQueryBuilder; |
| 32 | +import org.elasticsearch.search.aggregations.Aggregation; |
| 33 | +import org.elasticsearch.search.aggregations.bucket.significant.SignificantStringTerms; |
28 | 34 | import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms; |
29 | 35 | import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket; |
30 | 36 | import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregatorFactory.ExecutionMode; |
31 | 37 | import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsBuilder; |
| 38 | +import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; |
32 | 39 | import org.elasticsearch.search.aggregations.bucket.terms.Terms; |
33 | 40 | import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; |
34 | 41 | import org.elasticsearch.test.ElasticsearchIntegrationTest; |
35 | 42 | import org.junit.Test; |
36 | 43 |
|
37 | | -import java.util.HashMap; |
38 | | -import java.util.HashSet; |
39 | | -import java.util.Set; |
| 44 | +import java.util.*; |
| 45 | +import java.util.concurrent.ExecutionException; |
40 | 46 |
|
41 | 47 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; |
42 | 48 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; |
@@ -300,4 +306,63 @@ private void checkExpectedStringTermsFound(SignificantTerms topTerms) { |
300 | 306 | assertEquals(4, kellyTerm.getSupersetDf()); |
301 | 307 | } |
302 | 308 |
|
| 309 | + @Test |
| 310 | + public void testXContentResponse() throws Exception { |
| 311 | + String indexName = "10index"; |
| 312 | + String docType = "doc"; |
| 313 | + String classField = "class"; |
| 314 | + String textField = "text"; |
| 315 | + cluster().wipeIndices(indexName); |
| 316 | + String type = randomBoolean() ? "string" : "long"; |
| 317 | + index01Docs(indexName, docType, classField, textField, type); |
| 318 | + SearchResponse response = client().prepareSearch(indexName).setTypes(docType) |
| 319 | + .addAggregation(new TermsBuilder("class").field(classField).subAggregation(new SignificantTermsBuilder("sig_terms").field(textField))) |
| 320 | + .execute() |
| 321 | + .actionGet(); |
| 322 | + assertSearchResponse(response); |
| 323 | + StringTerms classes = (StringTerms) response.getAggregations().get("class"); |
| 324 | + assertThat(classes.getBuckets().size(), equalTo(2)); |
| 325 | + for (Terms.Bucket classBucket : classes.getBuckets()) { |
| 326 | + Map<String, Aggregation> aggs = classBucket.getAggregations().asMap(); |
| 327 | + assertTrue(aggs.containsKey("sig_terms")); |
| 328 | + SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms"); |
| 329 | + assertThat(agg.getBuckets().size(), equalTo(1)); |
| 330 | + String term = agg.iterator().next().getKey(); |
| 331 | + String classTerm = classBucket.getKey(); |
| 332 | + assertTrue(term.equals(classTerm)); |
| 333 | + } |
| 334 | + |
| 335 | + XContentBuilder responseBuilder = XContentFactory.jsonBuilder(); |
| 336 | + classes.toXContent(responseBuilder, null); |
| 337 | + String result = null; |
| 338 | + if (type.equals("long")) { |
| 339 | + result = "\"class\"{\"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}]}}]}"; |
| 340 | + } else { |
| 341 | + result = "\"class\"{\"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}]}}]}"; |
| 342 | + } |
| 343 | + assertThat(responseBuilder.string(), equalTo(result)); |
| 344 | + |
| 345 | + } |
| 346 | + |
| 347 | + private void index01Docs(String indexName, String docType, String classField, String textField, String type) throws ExecutionException, InterruptedException { |
| 348 | + String mappings = "{\"doc\": {\"properties\":{\"text\": {\"type\":\"" + type + "\"}}}}"; |
| 349 | + assertAcked(prepareCreate(indexName).setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0).addMapping("doc", mappings)); |
| 350 | + String[] gb = {"0", "1"}; |
| 351 | + List<IndexRequestBuilder> indexRequestBuilderList = new ArrayList<>(); |
| 352 | + indexRequestBuilderList.add(client().prepareIndex(indexName, docType, "1") |
| 353 | + .setSource(textField, "1", classField, "1")); |
| 354 | + indexRequestBuilderList.add(client().prepareIndex(indexName, docType, "2") |
| 355 | + .setSource(textField, "1", classField, "1")); |
| 356 | + indexRequestBuilderList.add(client().prepareIndex(indexName, docType, "3") |
| 357 | + .setSource(textField, "0", classField, "0")); |
| 358 | + indexRequestBuilderList.add(client().prepareIndex(indexName, docType, "4") |
| 359 | + .setSource(textField, "0", classField, "0")); |
| 360 | + indexRequestBuilderList.add(client().prepareIndex(indexName, docType, "5") |
| 361 | + .setSource(textField, gb, classField, "1")); |
| 362 | + indexRequestBuilderList.add(client().prepareIndex(indexName, docType, "6") |
| 363 | + .setSource(textField, gb, classField, "0")); |
| 364 | + indexRequestBuilderList.add(client().prepareIndex(indexName, docType, "7") |
| 365 | + .setSource(textField, "0", classField, "0")); |
| 366 | + indexRandom(true, indexRequestBuilderList); |
| 367 | + } |
303 | 368 | } |
0 commit comments