Search in sources :

Example 81 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class MinAggregatorTests method testMinAggregator_numericDv.

public void testMinAggregator_numericDv() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    Document document = new Document();
    document.add(new NumericDocValuesField("number", 9));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new NumericDocValuesField("number", 7));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new NumericDocValuesField("number", 5));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new NumericDocValuesField("number", 3));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new NumericDocValuesField("number", 1));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new NumericDocValuesField("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) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) 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 82 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class StatsAggregatorTests method testRandomDoubles.

public void testRandomDoubles() throws IOException {
    MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
    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++) {
                double value = randomDoubleBetween(-100d, 100d, true);
                long valueAsLong = NumericUtils.doubleToSortableLong(value);
                doc.add(new SortedNumericDocValuesField("field", valueAsLong));
                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 83 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class MinAggregatorTests method testMinAggregator_noDocs.

public void testMinAggregator_noDocs() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    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(Double.POSITIVE_INFINITY, result.getValue(), 0);
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 84 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class MinAggregatorTests method testMinAggregator_noValue.

public void testMinAggregator_noValue() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    Document document = new Document();
    document.add(new SortedNumericDocValuesField("number1", 7));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number1", 3));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number1", 1));
    indexWriter.addDocument(document);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number2");
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    fieldType.setName("number2");
    try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
        aggregator.preCollection();
        indexSearcher.search(new MatchAllDocsQuery(), aggregator);
        aggregator.postCollection();
        InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
        assertEquals(Double.POSITIVE_INFINITY, 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 85 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class SliceBuilderTests method testToFilter.

public void testToFilter() throws IOException {
    Directory dir = new RAMDirectory();
    try (IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())))) {
        writer.commit();
    }
    QueryShardContext context = mock(QueryShardContext.class);
    try (IndexReader reader = DirectoryReader.open(dir)) {
        MappedFieldType fieldType = new MappedFieldType() {

            @Override
            public MappedFieldType clone() {
                return null;
            }

            @Override
            public String typeName() {
                return null;
            }

            @Override
            public Query termQuery(Object value, @Nullable QueryShardContext context) {
                return null;
            }
        };
        fieldType.setName(UidFieldMapper.NAME);
        fieldType.setHasDocValues(false);
        when(context.fieldMapper(UidFieldMapper.NAME)).thenReturn(fieldType);
        when(context.getIndexReader()).thenReturn(reader);
        SliceBuilder builder = new SliceBuilder(5, 10);
        Query query = builder.toFilter(context, 0, 1);
        assertThat(query, instanceOf(TermsSliceQuery.class));
        assertThat(builder.toFilter(context, 0, 1), equalTo(query));
        try (IndexReader newReader = DirectoryReader.open(dir)) {
            when(context.getIndexReader()).thenReturn(newReader);
            assertThat(builder.toFilter(context, 0, 1), equalTo(query));
        }
    }
    try (IndexReader reader = DirectoryReader.open(dir)) {
        MappedFieldType fieldType = new MappedFieldType() {

            @Override
            public MappedFieldType clone() {
                return null;
            }

            @Override
            public String typeName() {
                return null;
            }

            @Override
            public Query termQuery(Object value, @Nullable QueryShardContext context) {
                return null;
            }
        };
        fieldType.setName("field_doc_values");
        fieldType.setHasDocValues(true);
        fieldType.setDocValuesType(DocValuesType.SORTED_NUMERIC);
        when(context.fieldMapper("field_doc_values")).thenReturn(fieldType);
        when(context.getIndexReader()).thenReturn(reader);
        IndexNumericFieldData fd = mock(IndexNumericFieldData.class);
        when(context.getForField(fieldType)).thenReturn(fd);
        SliceBuilder builder = new SliceBuilder("field_doc_values", 5, 10);
        Query query = builder.toFilter(context, 0, 1);
        assertThat(query, instanceOf(DocValuesSliceQuery.class));
        assertThat(builder.toFilter(context, 0, 1), equalTo(query));
        try (IndexReader newReader = DirectoryReader.open(dir)) {
            when(context.getIndexReader()).thenReturn(newReader);
            assertThat(builder.toFilter(context, 0, 1), equalTo(query));
        }
        // numSlices > numShards
        int numSlices = randomIntBetween(10, 100);
        int numShards = randomIntBetween(1, 9);
        Map<Integer, AtomicInteger> numSliceMap = new HashMap<>();
        for (int i = 0; i < numSlices; i++) {
            for (int j = 0; j < numShards; j++) {
                SliceBuilder slice = new SliceBuilder("_uid", i, numSlices);
                Query q = slice.toFilter(context, j, numShards);
                if (q instanceof TermsSliceQuery || q instanceof MatchAllDocsQuery) {
                    AtomicInteger count = numSliceMap.get(j);
                    if (count == null) {
                        count = new AtomicInteger(0);
                        numSliceMap.put(j, count);
                    }
                    count.incrementAndGet();
                    if (q instanceof MatchAllDocsQuery) {
                        assertThat(count.get(), equalTo(1));
                    }
                } else {
                    assertThat(q, instanceOf(MatchNoDocsQuery.class));
                }
            }
        }
        int total = 0;
        for (Map.Entry<Integer, AtomicInteger> e : numSliceMap.entrySet()) {
            total += e.getValue().get();
        }
        assertThat(total, equalTo(numSlices));
        // numShards > numSlices
        numShards = randomIntBetween(4, 100);
        numSlices = randomIntBetween(2, numShards - 1);
        List<Integer> targetShards = new ArrayList<>();
        for (int i = 0; i < numSlices; i++) {
            for (int j = 0; j < numShards; j++) {
                SliceBuilder slice = new SliceBuilder("_uid", i, numSlices);
                Query q = slice.toFilter(context, j, numShards);
                if (q instanceof MatchNoDocsQuery == false) {
                    assertThat(q, instanceOf(MatchAllDocsQuery.class));
                    targetShards.add(j);
                }
            }
        }
        assertThat(targetShards.size(), equalTo(numShards));
        assertThat(new HashSet<>(targetShards).size(), equalTo(numShards));
        // numShards == numSlices
        numShards = randomIntBetween(2, 10);
        numSlices = numShards;
        for (int i = 0; i < numSlices; i++) {
            for (int j = 0; j < numShards; j++) {
                SliceBuilder slice = new SliceBuilder("_uid", i, numSlices);
                Query q = slice.toFilter(context, j, numShards);
                if (i == j) {
                    assertThat(q, instanceOf(MatchAllDocsQuery.class));
                } else {
                    assertThat(q, instanceOf(MatchNoDocsQuery.class));
                }
            }
        }
    }
    try (IndexReader reader = DirectoryReader.open(dir)) {
        MappedFieldType fieldType = new MappedFieldType() {

            @Override
            public MappedFieldType clone() {
                return null;
            }

            @Override
            public String typeName() {
                return null;
            }

            @Override
            public Query termQuery(Object value, @Nullable QueryShardContext context) {
                return null;
            }
        };
        fieldType.setName("field_without_doc_values");
        when(context.fieldMapper("field_without_doc_values")).thenReturn(fieldType);
        when(context.getIndexReader()).thenReturn(reader);
        SliceBuilder builder = new SliceBuilder("field_without_doc_values", 5, 10);
        IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> builder.toFilter(context, 0, 1));
        assertThat(exc.getMessage(), containsString("cannot load numeric doc values"));
    }
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) HashMap(java.util.HashMap) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ArrayList(java.util.ArrayList) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RAMDirectory(org.apache.lucene.store.RAMDirectory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexWriter(org.apache.lucene.index.IndexWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexReader(org.apache.lucene.index.IndexReader) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(org.elasticsearch.common.Nullable)

Aggregations

MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)122 IndexSearcher (org.apache.lucene.search.IndexSearcher)34 IndexReader (org.apache.lucene.index.IndexReader)33 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)29 Directory (org.apache.lucene.store.Directory)29 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)26 Document (org.apache.lucene.document.Document)23 Query (org.apache.lucene.search.Query)19 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)17 Term (org.apache.lucene.index.Term)17 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 IndexableField (org.apache.lucene.index.IndexableField)9 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)9 ArrayList (java.util.ArrayList)8 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 TermQuery (org.apache.lucene.search.TermQuery)8 IndexNumericFieldData (org.elasticsearch.index.fielddata.IndexNumericFieldData)8 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)8 Analyzer (org.apache.lucene.analysis.Analyzer)7