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());
}
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));
}
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"));
}
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"));
}
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));
}
Aggregations