Search in sources :

Example 1 with DateFieldType

use of org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType in project elasticsearch by elastic.

the class DateFieldTypeTests method testIsFieldWithinQueryEmptyReader.

public void testIsFieldWithinQueryEmptyReader() throws IOException {
    QueryRewriteContext context = new QueryRewriteContext(null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    IndexReader reader = new MultiReader();
    DateFieldType ft = new DateFieldType();
    ft.setName("my_date");
    assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", randomBoolean(), randomBoolean(), null, null, context));
}
Also used : DateFieldType(org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType) MultiReader(org.apache.lucene.index.MultiReader) QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext) IndexReader(org.apache.lucene.index.IndexReader)

Example 2 with DateFieldType

use of org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType 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

DateFieldType (org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType)2 QueryRewriteContext (org.elasticsearch.index.query.QueryRewriteContext)2 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 MultiReader (org.apache.lucene.index.MultiReader)1 Directory (org.apache.lucene.store.Directory)1 DateMathParser (org.elasticsearch.common.joda.DateMathParser)1 Document (org.elasticsearch.index.mapper.ParseContext.Document)1