Search in sources :

Example 11 with MatchAllDocsQuery

use of org.apache.lucene.search.MatchAllDocsQuery in project elasticsearch by elastic.

the class ParentToChildrenAggregatorTests method testParentChild.

public void testParentChild() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
    indexWriter.close();
    IndexReader indexReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), new ShardId(new Index("foo", "_na_"), 1));
    // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved
    IndexSearcher indexSearcher = newSearcher(indexReader, false, true);
    testCase(new MatchAllDocsQuery(), indexSearcher, child -> {
        int expectedTotalChildren = 0;
        int expectedMinValue = Integer.MAX_VALUE;
        for (Tuple<Integer, Integer> expectedValues : expectedParentChildRelations.values()) {
            expectedTotalChildren += expectedValues.v1();
            expectedMinValue = Math.min(expectedMinValue, expectedValues.v2());
        }
        assertEquals(expectedTotalChildren, child.getDocCount());
        assertEquals(expectedMinValue, ((InternalMin) child.getAggregations().get("in_child")).getValue(), Double.MIN_VALUE);
    });
    for (String parent : expectedParentChildRelations.keySet()) {
        testCase(new TermInSetQuery(UidFieldMapper.NAME, new BytesRef(Uid.createUid(PARENT_TYPE, parent))), indexSearcher, child -> {
            assertEquals((long) expectedParentChildRelations.get(parent).v1(), child.getDocCount());
            assertEquals(expectedParentChildRelations.get(parent).v2(), ((InternalMin) child.getAggregations().get("in_child")).getValue(), Double.MIN_VALUE);
        });
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Index(org.elasticsearch.index.Index) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ShardId(org.elasticsearch.index.shard.ShardId) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Tuple(org.elasticsearch.common.collect.Tuple) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 12 with MatchAllDocsQuery

use of org.apache.lucene.search.MatchAllDocsQuery in project elasticsearch by elastic.

the class DateHistogramAggregatorTests method testIntervalHour.

public void testIntervalHour() throws IOException {
    testBothCases(new MatchAllDocsQuery(), Arrays.asList("2017-02-01T09:02:00.000Z", "2017-02-01T09:35:00.000Z", "2017-02-01T10:15:00.000Z", "2017-02-01T13:06:00.000Z", "2017-02-01T14:04:00.000Z", "2017-02-01T14:05:00.000Z", "2017-02-01T15:59:00.000Z", "2017-02-01T16:06:00.000Z", "2017-02-01T16:48:00.000Z", "2017-02-01T16:59:00.000Z"), aggregation -> aggregation.dateHistogramInterval(DateHistogramInterval.HOUR).field(DATE_FIELD).minDocCount(1L), histogram -> {
        List<Histogram.Bucket> buckets = histogram.getBuckets();
        assertEquals(6, buckets.size());
        Histogram.Bucket bucket = buckets.get(0);
        assertEquals("2017-02-01T09:00:00.000Z", bucket.getKeyAsString());
        assertEquals(2, bucket.getDocCount());
        bucket = buckets.get(1);
        assertEquals("2017-02-01T10:00:00.000Z", bucket.getKeyAsString());
        assertEquals(1, bucket.getDocCount());
        bucket = buckets.get(2);
        assertEquals("2017-02-01T13:00:00.000Z", bucket.getKeyAsString());
        assertEquals(1, bucket.getDocCount());
        bucket = buckets.get(3);
        assertEquals("2017-02-01T14:00:00.000Z", bucket.getKeyAsString());
        assertEquals(2, bucket.getDocCount());
        bucket = buckets.get(4);
        assertEquals("2017-02-01T15:00:00.000Z", bucket.getKeyAsString());
        assertEquals(1, bucket.getDocCount());
        bucket = buckets.get(5);
        assertEquals("2017-02-01T16:00:00.000Z", bucket.getKeyAsString());
        assertEquals(3, bucket.getDocCount());
    });
}
Also used : MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 13 with MatchAllDocsQuery

use of org.apache.lucene.search.MatchAllDocsQuery in project elasticsearch by elastic.

the class DateHistogramAggregatorTests method testMatchAllDocs.

public void testMatchAllDocs() throws IOException {
    Query query = new MatchAllDocsQuery();
    testSearchCase(query, dataset, aggregation -> aggregation.dateHistogramInterval(DateHistogramInterval.YEAR).field(DATE_FIELD), histogram -> assertEquals(6, histogram.getBuckets().size()));
    testSearchAndReduceCase(query, dataset, aggregation -> aggregation.dateHistogramInterval(DateHistogramInterval.YEAR).field(DATE_FIELD), histogram -> assertEquals(8, histogram.getBuckets().size()));
    testBothCases(query, dataset, aggregation -> aggregation.dateHistogramInterval(DateHistogramInterval.YEAR).field(DATE_FIELD).minDocCount(1L), histogram -> assertEquals(6, histogram.getBuckets().size()));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 14 with MatchAllDocsQuery

use of org.apache.lucene.search.MatchAllDocsQuery in project elasticsearch by elastic.

the class DateHistogramAggregatorTests method testIntervalDay.

public void testIntervalDay() throws IOException {
    testBothCases(new MatchAllDocsQuery(), Arrays.asList("2017-02-01", "2017-02-02", "2017-02-02", "2017-02-03", "2017-02-03", "2017-02-03", "2017-02-05"), aggregation -> aggregation.dateHistogramInterval(DateHistogramInterval.DAY).field(DATE_FIELD).minDocCount(1L), histogram -> {
        List<Histogram.Bucket> buckets = histogram.getBuckets();
        assertEquals(4, buckets.size());
        Histogram.Bucket bucket = buckets.get(0);
        assertEquals("2017-02-01T00:00:00.000Z", bucket.getKeyAsString());
        assertEquals(1, bucket.getDocCount());
        bucket = buckets.get(1);
        assertEquals("2017-02-02T00:00:00.000Z", bucket.getKeyAsString());
        assertEquals(2, bucket.getDocCount());
        bucket = buckets.get(2);
        assertEquals("2017-02-03T00:00:00.000Z", bucket.getKeyAsString());
        assertEquals(3, bucket.getDocCount());
        bucket = buckets.get(3);
        assertEquals("2017-02-05T00:00:00.000Z", bucket.getKeyAsString());
        assertEquals(1, bucket.getDocCount());
    });
}
Also used : MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 15 with MatchAllDocsQuery

use of org.apache.lucene.search.MatchAllDocsQuery in project elasticsearch by elastic.

the class DateHistogramAggregatorTests method testIntervalMonth.

public void testIntervalMonth() throws IOException {
    testBothCases(new MatchAllDocsQuery(), Arrays.asList("2017-01-01", "2017-02-02", "2017-02-03", "2017-03-04", "2017-03-05", "2017-03-06"), aggregation -> aggregation.dateHistogramInterval(DateHistogramInterval.MONTH).field(DATE_FIELD), histogram -> {
        List<Histogram.Bucket> buckets = histogram.getBuckets();
        assertEquals(3, buckets.size());
        Histogram.Bucket bucket = buckets.get(0);
        assertEquals("2017-01-01T00:00:00.000Z", bucket.getKeyAsString());
        assertEquals(1, bucket.getDocCount());
        bucket = buckets.get(1);
        assertEquals("2017-02-01T00:00:00.000Z", bucket.getKeyAsString());
        assertEquals(2, bucket.getDocCount());
        bucket = buckets.get(2);
        assertEquals("2017-03-01T00:00:00.000Z", bucket.getKeyAsString());
        assertEquals(3, bucket.getDocCount());
    });
}
Also used : MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)282 IndexSearcher (org.apache.lucene.search.IndexSearcher)168 Directory (org.apache.lucene.store.Directory)133 Document (org.apache.lucene.document.Document)122 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)122 IndexReader (org.apache.lucene.index.IndexReader)118 TopDocs (org.apache.lucene.search.TopDocs)91 Sort (org.apache.lucene.search.Sort)71 SortField (org.apache.lucene.search.SortField)64 Query (org.apache.lucene.search.Query)52 BooleanQuery (org.apache.lucene.search.BooleanQuery)49 TermQuery (org.apache.lucene.search.TermQuery)48 FacetsCollector (org.apache.lucene.facet.FacetsCollector)40 Facets (org.apache.lucene.facet.Facets)34 Term (org.apache.lucene.index.Term)33 DirectoryReader (org.apache.lucene.index.DirectoryReader)30 ArrayList (java.util.ArrayList)29 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)28 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)23 FacetResult (org.apache.lucene.facet.FacetResult)22