Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,95 @@ private void testCase(AvgAggregationBuilder aggregationBuilder, Query query,
indexReader.close();
directory.close();
}

/**
* Make sure that an aggregation not using a script does get cached.
*/
public void testCacheAggregation() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final int numDocs = 10;
for (int i = 0; i < numDocs; i++) {
indexWriter.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
}
indexWriter.close();

Directory unmappedDirectory = newDirectory();
RandomIndexWriter unmappedIndexWriter = new RandomIndexWriter(random(), unmappedDirectory);
unmappedIndexWriter.close();

IndexReader indexReader = DirectoryReader.open(directory);
IndexReader unamappedIndexReader = DirectoryReader.open(unmappedDirectory);
MultiReader multiReader = new MultiReader(indexReader, unamappedIndexReader);
IndexSearcher indexSearcher = newSearcher(multiReader, true, true);

MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
fieldType.setName("value");
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("avg")
.field("value");

AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection();

InternalAvg avg = (InternalAvg) aggregator.buildAggregation(0L);

assertEquals(5.5, avg.getValue(), 0);
assertEquals("avg", avg.getName());
assertTrue(AggregationInspectionHelper.hasValue(avg));

// Test that an aggregation not using a script does get cached
assertTrue(aggregator.context().getQueryShardContext().isCacheable());

multiReader.close();
directory.close();
unmappedDirectory.close();
}

/**
* Make sure that an aggregation using a script does not get cached.
*/
public void testDontCacheScripts() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final int numDocs = 10;
for (int i = 0; i < numDocs; i++) {
indexWriter.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
}
indexWriter.close();

Directory unmappedDirectory = newDirectory();
RandomIndexWriter unmappedIndexWriter = new RandomIndexWriter(random(), unmappedDirectory);
unmappedIndexWriter.close();

IndexReader indexReader = DirectoryReader.open(directory);
IndexReader unamappedIndexReader = DirectoryReader.open(unmappedDirectory);
MultiReader multiReader = new MultiReader(indexReader, unamappedIndexReader);
IndexSearcher indexSearcher = newSearcher(multiReader, true, true);

MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
fieldType.setName("value");
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("avg")
.field("value")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));

AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection();

InternalAvg avg = (InternalAvg) aggregator.buildAggregation(0L);

assertEquals(5.5, avg.getValue(), 0);
assertEquals("avg", avg.getName());
assertTrue(AggregationInspectionHelper.hasValue(avg));

// Test that an aggregation using a script does not get cached
assertFalse(aggregator.context().getQueryShardContext().isCacheable());

multiReader.close();
directory.close();
unmappedDirectory.close();
}
}

This file was deleted.