Search in sources :

Example 1 with ShapeRelation

use of org.opensearch.common.geo.ShapeRelation 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 ShapeRelation

use of org.opensearch.common.geo.ShapeRelation 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 ShapeRelation

use of org.opensearch.common.geo.ShapeRelation in project OpenSearch by opensearch-project.

the class GeoPointShapeQueryTests method testProcessRelationSupport.

public void testProcessRelationSupport() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    Rectangle rectangle = new Rectangle(-35, -25, -25, -35);
    for (ShapeRelation shapeRelation : ShapeRelation.values()) {
        if (shapeRelation.equals(ShapeRelation.INTERSECTS) == false) {
            try {
                client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, rectangle).relation(shapeRelation)).get();
            } catch (SearchPhaseExecutionException e) {
                assertThat(e.getCause().getMessage(), containsString(shapeRelation + " query relation not supported for Field [" + defaultGeoFieldName + "]"));
            }
        }
    }
}
Also used : ShapeRelation(org.opensearch.common.geo.ShapeRelation) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) Rectangle(org.opensearch.geometry.Rectangle) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 4 with ShapeRelation

use of org.opensearch.common.geo.ShapeRelation 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 ShapeRelation

use of org.opensearch.common.geo.ShapeRelation 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

ShapeRelation (org.opensearch.common.geo.ShapeRelation)6 RangeFieldType (org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)5 QueryShardContext (org.opensearch.index.query.QueryShardContext)5 InetAddress (java.net.InetAddress)2 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)2 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2 DateTime (org.joda.time.DateTime)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1 DateFormatter (org.opensearch.common.time.DateFormatter)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 Rectangle (org.opensearch.geometry.Rectangle)1 DateFieldType (org.opensearch.index.mapper.DateFieldMapper.DateFieldType)1