use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class TermsAggregatorTests method testMixLongAndDouble.
public void testMixLongAndDouble() throws Exception {
for (TermsAggregatorFactory.ExecutionMode executionMode : TermsAggregatorFactory.ExecutionMode.values()) {
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name", ValueType.LONG).executionHint(executionMode.toString()).field("number").order(Terms.Order.term(true));
List<InternalAggregation> aggs = new ArrayList<>();
int numLongs = randomIntBetween(1, 3);
for (int i = 0; i < numLongs; i++) {
final Directory dir;
try (IndexReader reader = createIndexWithLongs()) {
dir = ((DirectoryReader) reader).directory();
IndexSearcher searcher = new IndexSearcher(reader);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number");
fieldType.setHasDocValues(true);
aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher));
}
dir.close();
}
int numDoubles = randomIntBetween(1, 3);
for (int i = 0; i < numDoubles; i++) {
final Directory dir;
try (IndexReader reader = createIndexWithDoubles()) {
dir = ((DirectoryReader) reader).directory();
IndexSearcher searcher = new IndexSearcher(reader);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
fieldType.setName("number");
fieldType.setHasDocValues(true);
aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher));
}
dir.close();
}
InternalAggregation.ReduceContext ctx = new InternalAggregation.ReduceContext(new MockBigArrays(Settings.EMPTY, new NoneCircuitBreakerService()), null, true);
for (InternalAggregation internalAgg : aggs) {
InternalAggregation mergedAggs = internalAgg.doReduce(aggs, ctx);
assertTrue(mergedAggs instanceof DoubleTerms);
long expected = numLongs + numDoubles;
List<Terms.Bucket> buckets = ((DoubleTerms) mergedAggs).getBuckets();
assertEquals(4, buckets.size());
assertEquals("1.0", buckets.get(0).getKeyAsString());
assertEquals(expected, buckets.get(0).getDocCount());
assertEquals("10.0", buckets.get(1).getKeyAsString());
assertEquals(expected * 2, buckets.get(1).getDocCount());
assertEquals("100.0", buckets.get(2).getKeyAsString());
assertEquals(expected * 2, buckets.get(2).getDocCount());
assertEquals("1000.0", buckets.get(3).getKeyAsString());
assertEquals(expected, buckets.get(3).getDocCount());
}
}
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class TermsAggregatorTests method createIndexWithLongs.
private IndexReader createIndexWithLongs() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
Document document = new Document();
document.add(new SortedNumericDocValuesField("number", 10));
document.add(new SortedNumericDocValuesField("number", 100));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedNumericDocValuesField("number", 1));
document.add(new SortedNumericDocValuesField("number", 100));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedNumericDocValuesField("number", 10));
document.add(new SortedNumericDocValuesField("number", 1000));
indexWriter.addDocument(document);
indexWriter.close();
return DirectoryReader.open(directory);
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class TermsAggregatorTests method createIndexWithDoubles.
private IndexReader createIndexWithDoubles() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
Document document = new Document();
document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(10.0d)));
document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(100.0d)));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(1.0d)));
document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(100.0d)));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(10.0d)));
document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(1000.0d)));
indexWriter.addDocument(document);
indexWriter.close();
return DirectoryReader.open(directory);
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class MaxAggregatorTests method testCase.
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalMax> verify) throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number");
try (MaxAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
verify.accept((InternalMax) aggregator.buildAggregation(0L));
}
indexReader.close();
directory.close();
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class StatsAggregatorTests method testCase.
public void testCase(MappedFieldType ft, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalStats> verify) throws IOException {
try (Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
buildIndex.accept(indexWriter);
try (IndexReader reader = indexWriter.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
StatsAggregationBuilder aggBuilder = new StatsAggregationBuilder("my_agg").field("field");
InternalStats stats = search(searcher, new MatchAllDocsQuery(), aggBuilder, ft);
verify.accept(stats);
}
}
}
Aggregations