Search in sources :

Example 1 with DateRangeIncludingNowQuery

use of org.opensearch.index.query.DateRangeIncludingNowQuery in project OpenSearch by opensearch-project.

the class DateFieldTypeTests method testRangeQuery.

public void testRangeQuery() throws IOException {
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).build();
    QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetadata.builder("foo").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null, () -> true, null);
    MappedFieldType ft = new DateFieldType("field");
    String date1 = "2015-10-12T14:10:55";
    String date2 = "2016-04-28T11:33:52";
    long instant1 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date1)).toInstant().toEpochMilli();
    long instant2 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date2)).toInstant().toEpochMilli() + 999;
    Query expected = new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", instant1, instant2), SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2));
    assertEquals(expected, ft.rangeQuery(date1, date2, true, true, null, null, null, context).rewrite(new MultiReader()));
    instant1 = nowInMillis;
    instant2 = instant1 + 100;
    expected = new DateRangeIncludingNowQuery(new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", instant1, instant2), SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)));
    assertEquals(expected, ft.rangeQuery("now", instant2, true, true, null, null, null, context));
    MappedFieldType unsearchable = new DateFieldType("field", false, false, true, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, Resolution.MILLISECONDS, null, Collections.emptyMap());
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.rangeQuery(date1, date2, true, true, null, null, null, context));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : Query(org.apache.lucene.search.Query) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) DateRangeIncludingNowQuery(org.opensearch.index.query.DateRangeIncludingNowQuery) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) MultiReader(org.apache.lucene.index.MultiReader) DateRangeIncludingNowQuery(org.opensearch.index.query.DateRangeIncludingNowQuery) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

MultiReader (org.apache.lucene.index.MultiReader)1 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)1 IndexSortSortedNumericDocValuesRangeQuery (org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery)1 Query (org.apache.lucene.search.Query)1 Settings (org.opensearch.common.settings.Settings)1 IndexSettings (org.opensearch.index.IndexSettings)1 DateFieldType (org.opensearch.index.mapper.DateFieldMapper.DateFieldType)1 DateRangeIncludingNowQuery (org.opensearch.index.query.DateRangeIncludingNowQuery)1 QueryShardContext (org.opensearch.index.query.QueryShardContext)1