Search in sources :

Example 1 with RangeFieldType

use of org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testDateRangeQueryUsingMappingFormat.

public void testDateRangeQueryUsingMappingFormat() {
    QueryShardContext context = createContext();
    RangeFieldType strict = new RangeFieldType("field", RangeFieldMapper.Defaults.DATE_FORMATTER);
    // don't use DISJOINT here because it doesn't work on date fields which we want to compare bounds with
    ShapeRelation relation = randomValueOtherThan(ShapeRelation.DISJOINT, () -> randomFrom(ShapeRelation.values()));
    // dates will break the default format, month/day of month is turned around in the format
    final String from = "2016-15-06T15:29:50+08:00";
    final String to = "2016-16-06T15:29:50+08:00";
    OpenSearchParseException ex = expectThrows(OpenSearchParseException.class, () -> strict.rangeQuery(from, to, true, true, relation, null, null, context));
    assertThat(ex.getMessage(), containsString("failed to parse date field [2016-15-06T15:29:50+08:00] with format [strict_date_optional_time||epoch_millis]"));
    // setting mapping format which is compatible with those dates
    final DateFormatter formatter = DateFormatter.forPattern("yyyy-dd-MM'T'HH:mm:ssZZZZZ");
    assertEquals(1465975790000L, formatter.parseMillis(from));
    assertEquals(1466062190000L, formatter.parseMillis(to));
    RangeFieldType fieldType = new RangeFieldType("field", formatter);
    final Query query = fieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context);
    assertEquals("field:<ranges:[1465975790000 : 1466062190999]>", query.toString());
    // compare lower and upper bounds with what we would get on a `date` field
    DateFieldType dateFieldType = new DateFieldType("field", DateFieldMapper.Resolution.MILLISECONDS, formatter);
    final Query queryOnDateField = dateFieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context);
    assertEquals("field:[1465975790000 TO 1466062190999]", queryOnDateField.toString());
}
Also used : ShapeRelation(org.opensearch.common.geo.ShapeRelation) OpenSearchParseException(org.opensearch.OpenSearchParseException) 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 2 with RangeFieldType

use of org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testRangeQuery.

public void testRangeQuery() throws Exception {
    QueryShardContext context = createContext();
    RangeFieldType ft = createDefaultFieldType();
    ShapeRelation relation = randomFrom(ShapeRelation.values());
    boolean includeLower = randomBoolean();
    boolean includeUpper = randomBoolean();
    Object from = nextFrom();
    Object to = nextTo(from);
    if (includeLower == false && includeUpper == false) {
        // need to increase once more, otherwise interval is empty because edge values are exclusive
        to = nextTo(to);
    }
    assertEquals(getExpectedRangeQuery(relation, from, to, includeLower, includeUpper), ft.rangeQuery(from, to, includeLower, includeUpper, relation, null, null, context));
}
Also used : ShapeRelation(org.opensearch.common.geo.ShapeRelation) QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)

Example 3 with RangeFieldType

use of org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testCaseInsensitiveQuery.

public void testCaseInsensitiveQuery() throws Exception {
    QueryShardContext context = createContext();
    RangeFieldType ft = createDefaultFieldType();
    Object value = nextFrom();
    QueryShardException ex = expectThrows(QueryShardException.class, () -> ft.termQueryCaseInsensitive(value, context));
    assertTrue(ex.getMessage().contains("does not support case insensitive term queries"));
}
Also used : QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 4 with RangeFieldType

use of org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testFromLargerToErrors.

/**
 * check that we catch cases where the user specifies larger "from" than "to" value, not counting the include upper/lower settings
 */
public void testFromLargerToErrors() throws Exception {
    QueryShardContext context = createContext();
    RangeFieldType ft = createDefaultFieldType();
    final Object from;
    final Object to;
    switch(type) {
        case LONG:
            {
                long fromValue = randomLong();
                from = fromValue;
                to = fromValue - 1L;
                break;
            }
        case DATE:
            {
                long fromValue = randomInt();
                from = new DateTime(fromValue);
                to = new DateTime(fromValue - 1);
                break;
            }
        case INTEGER:
            {
                int fromValue = randomInt();
                from = fromValue;
                to = fromValue - 1;
                break;
            }
        case DOUBLE:
            {
                double fromValue = randomDoubleBetween(0, 100, true);
                from = fromValue;
                to = fromValue - 1.0d;
                break;
            }
        case FLOAT:
            {
                float fromValue = randomFloat();
                from = fromValue;
                to = fromValue - 1.0f;
                break;
            }
        case IP:
            {
                byte[] ipv4 = new byte[4];
                random().nextBytes(ipv4);
                InetAddress fromValue = InetAddress.getByAddress(ipv4);
                from = fromValue;
                to = InetAddressPoint.nextDown(fromValue);
                break;
            }
        default:
            // quit test for other range types
            return;
    }
    ShapeRelation relation = randomFrom(ShapeRelation.values());
    IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery(from, to, true, true, relation, null, null, context));
    assertTrue(ex.getMessage().contains("Range query `from` value"));
    assertTrue(ex.getMessage().contains("is greater than `to` value"));
}
Also used : ShapeRelation(org.opensearch.common.geo.ShapeRelation) QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType) InetAddress(java.net.InetAddress) DateTime(org.joda.time.DateTime)

Example 5 with RangeFieldType

use of org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType in project OpenSearch by opensearch-project.

the class RangeFieldTypeTests method testTermQuery.

public void testTermQuery() throws Exception {
    // See https://github.com/elastic/elasticsearch/issues/25950
    QueryShardContext context = createContext();
    RangeFieldType ft = createDefaultFieldType();
    Object value = nextFrom();
    ShapeRelation relation = ShapeRelation.INTERSECTS;
    boolean includeLower = true;
    boolean includeUpper = true;
    assertEquals(getExpectedRangeQuery(relation, value, value, includeLower, includeUpper), ft.termQuery(value, context));
}
Also used : ShapeRelation(org.opensearch.common.geo.ShapeRelation) QueryShardContext(org.opensearch.index.query.QueryShardContext) RangeFieldType(org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)

Aggregations

RangeFieldType (org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)7 QueryShardContext (org.opensearch.index.query.QueryShardContext)7 ShapeRelation (org.opensearch.common.geo.ShapeRelation)5 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)3 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 Query (org.apache.lucene.search.Query)3 InetAddress (java.net.InetAddress)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 DateTime (org.joda.time.DateTime)2 DateFormatter (org.opensearch.common.time.DateFormatter)2 DateFieldType (org.opensearch.index.mapper.DateFieldMapper.DateFieldType)2 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 QueryShardException (org.opensearch.index.query.QueryShardException)1