Skip to content

Commit 1cbeaf6

Browse files
committed
significant terms: fix json response
Commit fbd7c9a introduced a regression that caused the min_doc_count to be equal to the number of documents in the background set. As a result no buckets were built when the response for significant terms was created. This only affected the final XContent response. closes elastic#6535
1 parent ed62f90 commit 1cbeaf6

File tree

4 files changed

+144
-5
lines changed

4 files changed

+144
-5
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
"Default index":
3+
- do:
4+
indices.create:
5+
index: goodbad
6+
body:
7+
settings:
8+
number_of_shards: "1"
9+
10+
- do:
11+
index:
12+
index: goodbad
13+
type: doc
14+
id: 1
15+
body: { text: "good", class: "good" }
16+
- do:
17+
index:
18+
index: goodbad
19+
type: doc
20+
id: 2
21+
body: { text: "good", class: "good" }
22+
- do:
23+
index:
24+
index: goodbad
25+
type: doc
26+
id: 3
27+
body: { text: "bad", class: "bad" }
28+
- do:
29+
index:
30+
index: goodbad
31+
type: doc
32+
id: 4
33+
body: { text: "bad", class: "bad" }
34+
- do:
35+
index:
36+
index: goodbad
37+
type: doc
38+
id: 5
39+
body: { text: "good bad", class: "good" }
40+
- do:
41+
index:
42+
index: goodbad
43+
type: doc
44+
id: 6
45+
body: { text: "good bad", class: "bad" }
46+
- do:
47+
index:
48+
index: goodbad
49+
type: doc
50+
id: 7
51+
body: { text: "bad", class: "bad" }
52+
53+
54+
55+
- do:
56+
indices.refresh:
57+
index: [goodbad]
58+
59+
- do:
60+
search:
61+
index: goodbad
62+
type: doc
63+
64+
- match: {hits.total: 7}
65+
66+
- do:
67+
search:
68+
index: goodbad
69+
type: doc
70+
body: {"aggs": {"class": {"terms": {"field": "class"},"aggs": {"sig_terms": {"significant_terms": {"field": "text"}}}}}}
71+
72+
- match: {aggregations.class.buckets.0.sig_terms.buckets.0.key: "bad"}
73+
- match: {aggregations.class.buckets.1.sig_terms.buckets.0.key: "good"}
74+

src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTerms.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public Type type() {
109109
@Override
110110
InternalSignificantTerms newAggregation(long subsetSize, long supersetSize,
111111
List<InternalSignificantTerms.Bucket> buckets) {
112-
return new SignificantLongTerms(subsetSize, supersetSize, getName(), formatter, requiredSize, supersetSize, buckets);
112+
return new SignificantLongTerms(subsetSize, supersetSize, getName(), formatter, requiredSize, minDocCount, buckets);
113113
}
114114

115115
@Override

src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantStringTerms.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Type type() {
106106
@Override
107107
InternalSignificantTerms newAggregation(long subsetSize, long supersetSize,
108108
List<InternalSignificantTerms.Bucket> buckets) {
109-
return new SignificantStringTerms(subsetSize, supersetSize, getName(), requiredSize, supersetSize, buckets);
109+
return new SignificantStringTerms(subsetSize, supersetSize, getName(), requiredSize, minDocCount, buckets);
110110
}
111111

112112
@Override

src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsTests.java

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,30 @@
1919
package org.elasticsearch.search.aggregations.bucket;
2020

2121
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
22+
import org.elasticsearch.action.index.IndexRequestBuilder;
2223
import org.elasticsearch.action.search.SearchResponse;
2324
import org.elasticsearch.action.search.SearchType;
2425
import org.elasticsearch.common.settings.ImmutableSettings;
2526
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;
2630
import org.elasticsearch.index.query.FilterBuilders;
2731
import org.elasticsearch.index.query.TermQueryBuilder;
32+
import org.elasticsearch.search.aggregations.Aggregation;
33+
import org.elasticsearch.search.aggregations.bucket.significant.SignificantStringTerms;
2834
import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms;
2935
import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket;
3036
import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregatorFactory.ExecutionMode;
3137
import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsBuilder;
38+
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
3239
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
3340
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
3441
import org.elasticsearch.test.ElasticsearchIntegrationTest;
3542
import org.junit.Test;
3643

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;
4046

4147
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
4248
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
@@ -300,4 +306,63 @@ private void checkExpectedStringTermsFound(SignificantTerms topTerms) {
300306
assertEquals(4, kellyTerm.getSupersetDf());
301307
}
302308

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+
}
303368
}

0 commit comments

Comments
 (0)