Search in sources :

Example 11 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType in project OpenSearch by opensearch-project.

the class DateFieldTypeTests method testValueFormat.

public void testValueFormat() {
    MappedFieldType ft = new DateFieldType("field");
    long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse("2015-10-12T14:10:55")).toInstant().toEpochMilli();
    assertEquals("2015-10-12T14:10:55.000Z", ft.docValueFormat(null, ZoneOffset.UTC).format(instant));
    assertEquals("2015-10-12T15:10:55.000+01:00", ft.docValueFormat(null, ZoneOffset.ofHours(1)).format(instant));
    assertEquals("2015", new DateFieldType("field").docValueFormat("YYYY", ZoneOffset.UTC).format(instant));
    assertEquals(instant, ft.docValueFormat(null, ZoneOffset.UTC).parseLong("2015-10-12T14:10:55", false, null));
    assertEquals(instant + 999, ft.docValueFormat(null, ZoneOffset.UTC).parseLong("2015-10-12T14:10:55", true, null));
    long i = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse("2015-10-13")).toInstant().toEpochMilli();
    assertEquals(i - 1, ft.docValueFormat(null, ZoneOffset.UTC).parseLong("2015-10-12||/d", true, null));
}
Also used : DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType)

Example 12 with DateFieldType

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

Example 13 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testDateVsDateRangeBounds.

/**
 * We would like to ensure lower and upper bounds are consistent between queries on a `date` and a`date_range`
 * field, so we randomize a few cases and compare the generated queries here
 */
public void testDateVsDateRangeBounds() {
    QueryShardContext context = createContext();
    // date formatter that truncates seconds, so we get some rounding behavior
    final DateFormatter formatter = DateFormatter.forPattern("yyyy-dd-MM'T'HH:mm");
    long lower = randomLongBetween(formatter.parseMillis("2000-01-01T00:00"), formatter.parseMillis("2010-01-01T00:00"));
    long upper = randomLongBetween(formatter.parseMillis("2011-01-01T00:00"), formatter.parseMillis("2020-01-01T00:00"));
    RangeFieldType fieldType = new RangeFieldType("field", true, false, false, formatter, false, null);
    String lowerAsString = formatter.formatMillis(lower);
    String upperAsString = formatter.formatMillis(upper);
    // also add date math rounding to days occasionally
    if (randomBoolean()) {
        lowerAsString = lowerAsString + "||/d";
    }
    if (randomBoolean()) {
        upperAsString = upperAsString + "||/d";
    }
    boolean includeLower = randomBoolean();
    boolean includeUpper = randomBoolean();
    final Query query = fieldType.rangeQuery(lowerAsString, upperAsString, includeLower, includeUpper, ShapeRelation.INTERSECTS, null, null, context);
    // get exact lower and upper bounds similar to what we would parse for `date` fields for same input strings
    DateFieldType dateFieldType = new DateFieldType("field");
    long lowerBoundLong = dateFieldType.parseToLong(lowerAsString, !includeLower, null, formatter.toDateMathParser(), () -> 0);
    if (includeLower == false) {
        ++lowerBoundLong;
    }
    long upperBoundLong = dateFieldType.parseToLong(upperAsString, includeUpper, null, formatter.toDateMathParser(), () -> 0);
    if (includeUpper == false) {
        --upperBoundLong;
    }
    // check that using this bounds we get similar query when constructing equivalent query on date_range field
    Query range = LongRange.newIntersectsQuery("field", new long[] { lowerBoundLong }, new long[] { upperBoundLong });
    assertEquals(range, query);
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) BinaryDocValuesRangeQuery(org.apache.lucene.queries.BinaryDocValuesRangeQuery) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) DateFormatter(org.opensearch.common.time.DateFormatter) QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 14 with DateFieldType

use of org.opensearch.index.mapper.DateFieldMapper.DateFieldType in project OpenSearch by opensearch-project.

the class DistanceFeatureQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(DistanceFeatureQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
    String fieldName = expectedFieldName(queryBuilder.fieldName());
    Object origin = queryBuilder.origin().origin();
    String pivot = queryBuilder.pivot();
    final Query expectedQuery;
    if (fieldName.equals(GEO_POINT_FIELD_NAME)) {
        GeoPoint originGeoPoint = (origin instanceof GeoPoint) ? (GeoPoint) origin : GeoUtils.parseFromString((String) origin);
        double pivotDouble = DistanceUnit.DEFAULT.parse(pivot, DistanceUnit.DEFAULT);
        expectedQuery = LatLonPoint.newDistanceFeatureQuery(fieldName, 1.0f, originGeoPoint.lat(), originGeoPoint.lon(), pivotDouble);
    } else {
        // if (fieldName.equals(DATE_FIELD_NAME))
        MapperService mapperService = context.getMapperService();
        DateFieldType fieldType = (DateFieldType) mapperService.fieldType(fieldName);
        long originLong = fieldType.parseToLong(origin, true, null, null, context::nowInMillis);
        TimeValue pivotVal = TimeValue.parseTimeValue(pivot, DistanceFeatureQueryBuilder.class.getSimpleName() + ".pivot");
        long pivotLong;
        if (fieldType.resolution() == DateFieldMapper.Resolution.MILLISECONDS) {
            pivotLong = pivotVal.getMillis();
        } else {
            // NANOSECONDS
            pivotLong = pivotVal.getNanos();
        }
        expectedQuery = LongPoint.newDistanceFeatureQuery(fieldName, 1.0f, originLong, pivotLong);
    }
    assertEquals(expectedQuery, query);
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Query(org.apache.lucene.search.Query) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) Matchers.containsString(org.hamcrest.Matchers.containsString) MapperService(org.opensearch.index.mapper.MapperService) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

DateFieldType (org.opensearch.index.mapper.DateFieldMapper.DateFieldType)14 Query (org.apache.lucene.search.Query)6 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)5 QueryShardContext (org.opensearch.index.query.QueryShardContext)5 IndexSortSortedNumericDocValuesRangeQuery (org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Settings (org.opensearch.common.settings.Settings)3 IndexSettings (org.opensearch.index.IndexSettings)3 DateRangeIncludingNowQuery (org.opensearch.index.query.DateRangeIncludingNowQuery)3 MultiReader (org.apache.lucene.index.MultiReader)2 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 DateFormatter (org.opensearch.common.time.DateFormatter)2 RangeFieldType (org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)2 QueryRewriteContext (org.opensearch.index.query.QueryRewriteContext)2 IOException (java.io.IOException)1 Function (java.util.function.Function)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1