Search in sources :

Example 91 with Directory

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());
        }
    }
}
Also used : MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) HashMap(java.util.HashMap) Map(java.util.Map) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 92 with Directory

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());
        }
    }
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 93 with Directory

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 94 with Directory

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));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 95 with Directory

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));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LatLonDocValuesField(org.apache.lucene.document.LatLonDocValuesField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

Directory (org.apache.lucene.store.Directory)2194 Document (org.apache.lucene.document.Document)1374 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)816 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)669 IndexReader (org.apache.lucene.index.IndexReader)590 IndexSearcher (org.apache.lucene.search.IndexSearcher)383 BytesRef (org.apache.lucene.util.BytesRef)376 RAMDirectory (org.apache.lucene.store.RAMDirectory)360 Term (org.apache.lucene.index.Term)325 StringField (org.apache.lucene.document.StringField)313 IndexWriter (org.apache.lucene.index.IndexWriter)312 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)274 TextField (org.apache.lucene.document.TextField)259 Field (org.apache.lucene.document.Field)257 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)257 Test (org.junit.Test)237 FSDirectory (org.apache.lucene.store.FSDirectory)221 Analyzer (org.apache.lucene.analysis.Analyzer)193 DirectoryReader (org.apache.lucene.index.DirectoryReader)193 TopDocs (org.apache.lucene.search.TopDocs)174