Skip to content

Commit a9ea742

Browse files
authored
Tests fix - Significant terms/text aggs (#25499)
The significance aggs return Lucene index-level statistics that when merged are assumed to be from different shards. The Aggregator unit tests assume segments can be treated as shards and thus break the significance stats and introduce double-counting of background doc frequencies. This change addresses this problem by ensuring test indexes have only one shard. Closes #25429
1 parent 99fef24 commit a9ea742

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregatorTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ public void testParsedAsFilter() throws IOException {
9595
/**
9696
* Uses the significant terms aggregation to find the keywords in text fields
9797
*/
98-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/25429")
9998
public void testSignificance() throws IOException {
10099
TextFieldType textFieldType = new TextFieldType();
101100
textFieldType.setName("text");
102101
textFieldType.setFielddata(true);
103102
textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
104103

105104
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
105+
indexWriterConfig.setMaxBufferedDocs(100);
106+
indexWriterConfig.setRAMBufferSizeMB(100); // flush on open to have a single segment
107+
106108
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
107109
addMixedTextDocs(textFieldType, w);
108110

@@ -117,6 +119,7 @@ public void testSignificance() throws IOException {
117119
sigNumAgg.executionHint(randomExecutionHint());
118120

119121
try (IndexReader reader = DirectoryReader.open(w)) {
122+
assertEquals("test expects a single segment", 1, reader.leaves().size());
120123
IndexSearcher searcher = new IndexSearcher(reader);
121124

122125
// Search "odd"
@@ -183,6 +186,8 @@ public void testNumericSignificance() throws IOException {
183186
textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
184187

185188
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
189+
indexWriterConfig.setMaxBufferedDocs(100);
190+
indexWriterConfig.setRAMBufferSizeMB(100); // flush on open to have a single segment
186191
final long ODD_VALUE = 3;
187192
final long EVEN_VALUE = 6;
188193
final long COMMON_VALUE = 2;
@@ -206,6 +211,7 @@ public void testNumericSignificance() throws IOException {
206211
sigNumAgg.executionHint(randomExecutionHint());
207212

208213
try (IndexReader reader = DirectoryReader.open(w)) {
214+
assertEquals("test expects a single segment", 1, reader.leaves().size());
209215
IndexSearcher searcher = new IndexSearcher(reader);
210216

211217
// Search "odd"
@@ -237,6 +243,8 @@ public void testUnmapped() throws IOException {
237243
textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
238244

239245
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
246+
indexWriterConfig.setMaxBufferedDocs(100);
247+
indexWriterConfig.setRAMBufferSizeMB(100); // flush on open to have a single segment
240248
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
241249
addMixedTextDocs(textFieldType, w);
242250

@@ -245,6 +253,7 @@ public void testUnmapped() throws IOException {
245253
sigAgg.executionHint(randomExecutionHint());
246254

247255
try (IndexReader reader = DirectoryReader.open(w)) {
256+
assertEquals("test expects a single segment", 1, reader.leaves().size());
248257
IndexSearcher searcher = new IndexSearcher(reader);
249258

250259
// Search "odd"

core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTextAggregatorTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public void testSignificance() throws IOException {
6565
textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
6666

6767
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
68+
indexWriterConfig.setMaxBufferedDocs(100);
69+
indexWriterConfig.setRAMBufferSizeMB(100); // flush on open to have a single segment
6870
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
6971
for (int i = 0; i < 10; i++) {
7072
Document doc = new Document();
@@ -91,6 +93,7 @@ public void testSignificance() throws IOException {
9193
.subAggregation(sigAgg);
9294

9395
try (IndexReader reader = DirectoryReader.open(w)) {
96+
assertEquals("test expects a single segment", 1, reader.leaves().size());
9497
IndexSearcher searcher = new IndexSearcher(reader);
9598

9699
// Search "odd" which should have no duplication
@@ -128,6 +131,8 @@ public void testSignificanceOnTextArrays() throws IOException {
128131
textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
129132

130133
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
134+
indexWriterConfig.setMaxBufferedDocs(100);
135+
indexWriterConfig.setRAMBufferSizeMB(100); // flush on open to have a single segment
131136
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
132137
for (int i = 0; i < 10; i++) {
133138
Document doc = new Document();
@@ -140,6 +145,7 @@ public void testSignificanceOnTextArrays() throws IOException {
140145
SignificantTextAggregationBuilder sigAgg = new SignificantTextAggregationBuilder("sig_text", "text");
141146
sigAgg.sourceFieldNames(Arrays.asList(new String [] {"title", "text"}));
142147
try (IndexReader reader = DirectoryReader.open(w)) {
148+
assertEquals("test expects a single segment", 1, reader.leaves().size());
143149
IndexSearcher searcher = new IndexSearcher(reader);
144150
searchAndReduce(searcher, new TermQuery(new Term("text", "foo")), sigAgg, textFieldType);
145151
// No significant results to be found in this test - only checking we don't end up

0 commit comments

Comments
 (0)