Search in sources :

Example 1 with DateMathParser

use of org.elasticsearch.common.joda.DateMathParser in project elasticsearch by elastic.

the class DateHistogramIT method testSingleValueFieldWithExtendedBoundsTimezone.

/**
     * Test date histogram aggregation with hour interval, timezone shift and
     * extended bounds (see https://github.com/elastic/elasticsearch/issues/12278)
     */
public void testSingleValueFieldWithExtendedBoundsTimezone() throws Exception {
    String index = "test12278";
    prepareCreate(index).setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).execute().actionGet();
    DateMathParser parser = new DateMathParser(Joda.getStrictStandardDateFormatter());
    // we pick a random timezone offset of +12/-12 hours and insert two documents
    // one at 00:00 in that time zone and one at 12:00
    List<IndexRequestBuilder> builders = new ArrayList<>();
    int timeZoneHourOffset = randomIntBetween(-12, 12);
    DateTimeZone timezone = DateTimeZone.forOffsetHours(timeZoneHourOffset);
    DateTime timeZoneStartToday = new DateTime(parser.parse("now/d", System::currentTimeMillis, false, timezone), DateTimeZone.UTC);
    DateTime timeZoneNoonToday = new DateTime(parser.parse("now/d+12h", System::currentTimeMillis, false, timezone), DateTimeZone.UTC);
    builders.add(indexDoc(index, timeZoneStartToday, 1));
    builders.add(indexDoc(index, timeZoneNoonToday, 2));
    indexRandom(true, builders);
    ensureSearchable(index);
    SearchResponse response = null;
    // retrieve those docs with the same time zone and extended bounds
    response = client().prepareSearch(index).setQuery(QueryBuilders.rangeQuery("date").from("now/d").to("now/d").includeLower(true).includeUpper(true).timeZone(timezone.getID())).addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.hours(1)).timeZone(timezone).minDocCount(0).extendedBounds(new ExtendedBounds("now/d", "now/d+23h"))).execute().actionGet();
    assertSearchResponse(response);
    assertThat("Expected 24 buckets for one day aggregation with hourly interval", response.getHits().getTotalHits(), equalTo(2L));
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(24));
    for (int i = 0; i < buckets.size(); i++) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat("InternalBucket " + i + " had wrong key", (DateTime) bucket.getKey(), equalTo(new DateTime(timeZoneStartToday.getMillis() + (i * 60 * 60 * 1000), DateTimeZone.UTC)));
        if (i == 0 || i == 12) {
            assertThat(bucket.getDocCount(), equalTo(1L));
        } else {
            assertThat(bucket.getDocCount(), equalTo(0L));
        }
    }
    internalCluster().wipeIndices("test12278");
}
Also used : ExtendedBounds(org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) DateMathParser(org.elasticsearch.common.joda.DateMathParser)

Example 2 with DateMathParser

use of org.elasticsearch.common.joda.DateMathParser in project elasticsearch by elastic.

the class RangeQueryBuilder method getRelation.

// Overridable for testing only
protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException {
    IndexReader reader = queryRewriteContext.getIndexReader();
    // rewrite so just pretend there is an intersection so that the rewrite is a noop
    if (reader == null) {
        return MappedFieldType.Relation.INTERSECTS;
    }
    final MapperService mapperService = queryRewriteContext.getMapperService();
    final MappedFieldType fieldType = mapperService.fullName(fieldName);
    if (fieldType == null) {
        // no field means we have no values
        return MappedFieldType.Relation.DISJOINT;
    } else {
        DateMathParser dateMathParser = format == null ? null : new DateMathParser(format);
        return fieldType.isFieldWithinQuery(queryRewriteContext.getIndexReader(), from, to, includeLower, includeUpper, timeZone, dateMathParser, queryRewriteContext);
    }
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) DateMathParser(org.elasticsearch.common.joda.DateMathParser) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 3 with DateMathParser

use of org.elasticsearch.common.joda.DateMathParser in project elasticsearch by elastic.

the class RangeQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query query = null;
    MappedFieldType mapper = context.fieldMapper(this.fieldName);
    if (mapper != null) {
        if (mapper instanceof DateFieldMapper.DateFieldType) {
            query = ((DateFieldMapper.DateFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, timeZone, getForceDateParser(), context);
        } else if (mapper instanceof RangeFieldMapper.RangeFieldType && mapper.typeName() == RangeFieldMapper.RangeType.DATE.name) {
            DateMathParser forcedDateParser = null;
            if (this.format != null) {
                forcedDateParser = new DateMathParser(this.format);
            }
            query = ((RangeFieldMapper.RangeFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, relation, timeZone, forcedDateParser, context);
        } else {
            if (timeZone != null) {
                throw new QueryShardException(context, "[range] time_zone can not be applied to non date field [" + fieldName + "]");
            }
            //LUCENE 4 UPGRADE Mapper#rangeQuery should use bytesref as well?
            query = mapper.rangeQuery(from, to, includeLower, includeUpper, context);
        }
    } else {
        if (timeZone != null) {
            throw new QueryShardException(context, "[range] time_zone can not be applied to non unmapped field [" + fieldName + "]");
        }
    }
    if (query == null) {
        query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper);
    }
    return query;
}
Also used : DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) Query(org.apache.lucene.search.Query) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) DateMathParser(org.elasticsearch.common.joda.DateMathParser)

Example 4 with DateMathParser

use of org.elasticsearch.common.joda.DateMathParser in project elasticsearch by elastic.

the class DateFieldTypeTests method testIsFieldWithinQuery.

public void testIsFieldWithinQuery() throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    long instant1 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime("2015-10-12").getMillis();
    long instant2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime("2016-04-03").getMillis();
    Document doc = new Document();
    LongPoint field = new LongPoint("my_date", instant1);
    doc.add(field);
    w.addDocument(doc);
    field.setLongValue(instant2);
    w.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(w);
    DateFieldType ft = new DateFieldType();
    ft.setName("my_date");
    DateMathParser alternateFormat = new DateMathParser(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER);
    doTestIsFieldWithinQuery(ft, reader, null, null);
    doTestIsFieldWithinQuery(ft, reader, null, alternateFormat);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat);
    // Fields with no value indexed.
    DateFieldType ft2 = new DateFieldType();
    ft2.setName("my_date2");
    QueryRewriteContext context = new QueryRewriteContext(null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, context));
    IOUtils.close(reader, w, dir);
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) DateFieldType(org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType) QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext) LongPoint(org.apache.lucene.document.LongPoint) DateMathParser(org.elasticsearch.common.joda.DateMathParser) Document(org.elasticsearch.index.mapper.ParseContext.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

DateMathParser (org.elasticsearch.common.joda.DateMathParser)4 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)2 ArrayList (java.util.ArrayList)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 Query (org.apache.lucene.search.Query)1 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)1 Directory (org.apache.lucene.store.Directory)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 DateFieldMapper (org.elasticsearch.index.mapper.DateFieldMapper)1 DateFieldType (org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType)1 MapperService (org.elasticsearch.index.mapper.MapperService)1 Document (org.elasticsearch.index.mapper.ParseContext.Document)1 QueryRewriteContext (org.elasticsearch.index.query.QueryRewriteContext)1 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)1 ExtendedBounds (org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds)1