Search in sources :

Example 21 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 22 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 23 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)

Example 24 with Directory

use of org.apache.lucene.store.Directory in project elasticsearch by elastic.

the class MinAggregatorTests method testMinAggregator_sortedNumericDv.

public void testMinAggregator_sortedNumericDv() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    Document document = new Document();
    document.add(new SortedNumericDocValuesField("number", 9));
    document.add(new SortedNumericDocValuesField("number", 7));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number", 5));
    document.add(new SortedNumericDocValuesField("number", 3));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number", 1));
    document.add(new SortedNumericDocValuesField("number", -1));
    indexWriter.addDocument(document);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number");
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    fieldType.setName("number");
    try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
        aggregator.preCollection();
        indexSearcher.search(new MatchAllDocsQuery(), aggregator);
        aggregator.postCollection();
        InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
        assertEquals(-1.0, result.getValue(), 0);
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 25 with Directory

use of org.apache.lucene.store.Directory in project elasticsearch by elastic.

the class ParentFieldSubFetchPhaseTests method testGetParentId.

public void testGetParentId() throws Exception {
    ParentFieldMapper fieldMapper = createParentFieldMapper();
    Directory directory = newDirectory();
    IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig());
    Document document = new Document();
    document.add(new SortedDocValuesField(fieldMapper.fieldType().name(), new BytesRef("1")));
    indexWriter.addDocument(document);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0);
    assertEquals("1", id);
    indexReader.close();
    directory.close();
}
Also used : ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) IndexWriter(org.apache.lucene.index.IndexWriter) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

Directory (org.apache.lucene.store.Directory)2188 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)381 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)309 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)271 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)236 FSDirectory (org.apache.lucene.store.FSDirectory)215 DirectoryReader (org.apache.lucene.index.DirectoryReader)193 Analyzer (org.apache.lucene.analysis.Analyzer)192 FieldType (org.apache.lucene.document.FieldType)173