use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class ScriptedMetricAggregatorTests method testScriptedMetricWithoutCombine.
/**
* without combine script, the "_aggs" map should contain a list of the size of the number of documents matched
*/
@SuppressWarnings("unchecked")
public void testScriptedMetricWithoutCombine() throws IOException {
try (Directory directory = newDirectory()) {
int numDocs = randomInt(100);
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < numDocs; i++) {
indexWriter.addDocument(singleton(new SortedNumericDocValuesField("number", i)));
}
}
try (IndexReader indexReader = DirectoryReader.open(directory)) {
ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME);
aggregationBuilder.initScript(INIT_SCRIPT).mapScript(MAP_SCRIPT);
ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder);
assertEquals(AGG_NAME, scriptedMetric.getName());
assertNotNull(scriptedMetric.aggregation());
Map<String, Object> agg = (Map<String, Object>) scriptedMetric.aggregation();
assertEquals(numDocs, ((List<Integer>) agg.get("collector")).size());
}
}
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class ScriptedMetricAggregatorTests method testNoDocs.
@SuppressWarnings("unchecked")
public void testNoDocs() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
// intentionally not writing any docs
}
try (IndexReader indexReader = DirectoryReader.open(directory)) {
ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME);
// map script is mandatory, even if its not used in this case
aggregationBuilder.mapScript(MAP_SCRIPT);
ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder);
assertEquals(AGG_NAME, scriptedMetric.getName());
assertNotNull(scriptedMetric.aggregation());
assertEquals(0, ((HashMap<Object, String>) scriptedMetric.aggregation()).size());
}
}
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class AvgAggregatorTests method testCase.
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalAvg> 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);
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number");
try (AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
verify.accept((InternalAvg) aggregator.buildAggregation(0L));
}
indexReader.close();
directory.close();
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class GeoBoundsAggregatorTests method testEmpty.
public void testEmpty() throws Exception {
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field").wrapLongitude(false);
MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
fieldType.setHasDocValues(true);
fieldType.setName("field");
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
assertTrue(Double.isInfinite(bounds.top));
assertTrue(Double.isInfinite(bounds.bottom));
assertTrue(Double.isInfinite(bounds.posLeft));
assertTrue(Double.isInfinite(bounds.posRight));
assertTrue(Double.isInfinite(bounds.negLeft));
assertTrue(Double.isInfinite(bounds.negRight));
}
}
}
use of org.apache.lucene.store.Directory in project elasticsearch by elastic.
the class GeoBoundsAggregatorTests method testRandom.
public void testRandom() throws Exception {
double top = Double.NEGATIVE_INFINITY;
double bottom = Double.POSITIVE_INFINITY;
double posLeft = Double.POSITIVE_INFINITY;
double posRight = Double.NEGATIVE_INFINITY;
double negLeft = Double.POSITIVE_INFINITY;
double negRight = Double.NEGATIVE_INFINITY;
int numDocs = randomIntBetween(50, 100);
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
int numValues = randomIntBetween(1, 5);
for (int j = 0; j < numValues; j++) {
GeoPoint point = RandomGeoGenerator.randomPoint(random());
if (point.getLat() > top) {
top = point.getLat();
}
if (point.getLat() < bottom) {
bottom = point.getLat();
}
if (point.getLon() >= 0 && point.getLon() < posLeft) {
posLeft = point.getLon();
}
if (point.getLon() >= 0 && point.getLon() > posRight) {
posRight = point.getLon();
}
if (point.getLon() < 0 && point.getLon() < negLeft) {
negLeft = point.getLon();
}
if (point.getLon() < 0 && point.getLon() > negRight) {
negRight = point.getLon();
}
doc.add(new LatLonDocValuesField("field", point.getLat(), point.getLon()));
}
w.addDocument(doc);
}
GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field").wrapLongitude(false);
MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
fieldType.setHasDocValues(true);
fieldType.setName("field");
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
assertThat(bounds.top, closeTo(top, GEOHASH_TOLERANCE));
assertThat(bounds.bottom, closeTo(bottom, GEOHASH_TOLERANCE));
assertThat(bounds.posLeft, closeTo(posLeft, GEOHASH_TOLERANCE));
assertThat(bounds.posRight, closeTo(posRight, GEOHASH_TOLERANCE));
assertThat(bounds.negRight, closeTo(negRight, GEOHASH_TOLERANCE));
assertThat(bounds.negLeft, closeTo(negLeft, GEOHASH_TOLERANCE));
}
}
}
Aggregations