Search in sources :

Example 16 with Document

use of org.apache.lucene.document.Document 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);
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 17 with Document

use of org.apache.lucene.document.Document in project elasticsearch by elastic.

the class StatsAggregatorTests method testRandomLongs.

public void testRandomLongs() throws IOException {
    MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    ft.setName("field");
    final SimpleStatsAggregator expected = new SimpleStatsAggregator();
    testCase(ft, iw -> {
        int numDocs = randomIntBetween(10, 50);
        for (int i = 0; i < numDocs; i++) {
            Document doc = new Document();
            int numValues = randomIntBetween(1, 5);
            for (int j = 0; j < numValues; j++) {
                long value = randomIntBetween(-100, 100);
                doc.add(new SortedNumericDocValuesField("field", value));
                expected.add(value);
            }
            iw.addDocument(doc);
        }
    }, stats -> {
        assertEquals(expected.count, stats.getCount(), 0);
        assertEquals(expected.sum, stats.getSum(), TOLERANCE);
        assertEquals(expected.min, stats.getMin(), 0);
        assertEquals(expected.max, stats.getMax(), 0);
        assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE);
    });
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Document(org.apache.lucene.document.Document)

Example 18 with Document

use of org.apache.lucene.document.Document in project elasticsearch by elastic.

the class HDRPercentileRanksAggregatorTests method testSimple.

public void testSimple() throws IOException {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (double value : new double[] { 3, 0.2, 10 }) {
            Document doc = new Document();
            doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            w.addDocument(doc);
        }
        PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg").field("field").method(PercentilesMethod.HDR).values(0.1, 0.5, 12);
        MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
        fieldType.setName("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            Iterator<Percentile> rankIterator = ranks.iterator();
            Percentile rank = rankIterator.next();
            assertEquals(0.1, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.equalTo(0d));
            rank = rankIterator.next();
            assertEquals(0.5, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.greaterThan(0d));
            assertThat(rank.getPercent(), Matchers.lessThan(100d));
            rank = rankIterator.next();
            assertEquals(12, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.equalTo(100d));
            assertFalse(rankIterator.hasNext());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Percentile(org.elasticsearch.search.aggregations.metrics.percentiles.Percentile) PercentileRanksAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 19 with Document

use of org.apache.lucene.document.Document in project elasticsearch by elastic.

the class TDigestPercentileRanksAggregatorTests method testSimple.

public void testSimple() throws IOException {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (double value : new double[] { 3, 0.2, 10 }) {
            Document doc = new Document();
            doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            w.addDocument(doc);
        }
        PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg").field("field").method(PercentilesMethod.TDIGEST).values(0.1, 0.5, 12);
        MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
        fieldType.setName("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            Iterator<Percentile> rankIterator = ranks.iterator();
            Percentile rank = rankIterator.next();
            assertEquals(0.1, rank.getValue(), 0d);
            // TODO: Fix T-Digest: this assertion should pass but we currently get ~15
            // https://github.com/elastic/elasticsearch/issues/14851
            // assertThat(rank.getPercent(), Matchers.equalTo(0d));
            rank = rankIterator.next();
            assertEquals(0.5, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.greaterThan(0d));
            assertThat(rank.getPercent(), Matchers.lessThan(100d));
            rank = rankIterator.next();
            assertEquals(12, rank.getValue(), 0d);
            // TODO: Fix T-Digest: this assertion should pass but we currently get ~59
            // https://github.com/elastic/elasticsearch/issues/14851
            // assertThat(rank.getPercent(), Matchers.equalTo(100d));
            assertFalse(rankIterator.hasNext());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Percentile(org.elasticsearch.search.aggregations.metrics.percentiles.Percentile) PercentileRanksAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 20 with Document

use of org.apache.lucene.document.Document in project elasticsearch by elastic.

the class TopHitsAggregatorTests method document.

private Document document(String id, String... stringValues) {
    Document document = new Document();
    document.add(new Field(UidFieldMapper.NAME, Uid.createUid("type", id), UidFieldMapper.Defaults.FIELD_TYPE));
    for (String stringValue : stringValues) {
        document.add(new Field("string", stringValue, STRING_FIELD_TYPE));
        document.add(new SortedSetDocValuesField("string", new BytesRef(stringValue)));
    }
    return document;
}
Also used : SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) Field(org.apache.lucene.document.Field) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

Document (org.apache.lucene.document.Document)2344 Directory (org.apache.lucene.store.Directory)1374 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)798 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)752 IndexReader (org.apache.lucene.index.IndexReader)598 Field (org.apache.lucene.document.Field)480 IndexSearcher (org.apache.lucene.search.IndexSearcher)470 Term (org.apache.lucene.index.Term)456 BytesRef (org.apache.lucene.util.BytesRef)415 StringField (org.apache.lucene.document.StringField)403 TextField (org.apache.lucene.document.TextField)389 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)325 IndexWriter (org.apache.lucene.index.IndexWriter)312 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)278 TopDocs (org.apache.lucene.search.TopDocs)270 TermQuery (org.apache.lucene.search.TermQuery)237 FieldType (org.apache.lucene.document.FieldType)231 DirectoryReader (org.apache.lucene.index.DirectoryReader)226 Test (org.junit.Test)222 RAMDirectory (org.apache.lucene.store.RAMDirectory)211