Skip to content

Commit 27c84c1

Browse files
committed
Use date format in mapping before fallback to default if not force format (elastic#29282)
1 parent b6568d0 commit 27c84c1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ public Query termQuery(Object value, QueryShardContext context) {
292292
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper,
293293
ShapeRelation relation, DateTimeZone timeZone, DateMathParser parser, QueryShardContext context) {
294294
failIfNotIndexed();
295+
if (parser == null) {
296+
parser = dateMathParser();
297+
}
295298
return rangeType.rangeQuery(name(), hasDocValues(), lowerTerm, upperTerm, includeLower, includeUpper, relation,
296299
timeZone, parser, context);
297300
}

server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.elasticsearch.Version;
3535
import org.elasticsearch.cluster.metadata.IndexMetaData;
3636
import org.elasticsearch.common.geo.ShapeRelation;
37+
import org.elasticsearch.common.joda.DateMathParser;
38+
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
3739
import org.elasticsearch.common.joda.Joda;
3840
import org.elasticsearch.common.network.InetAddresses;
3941
import org.elasticsearch.common.settings.Settings;
@@ -98,6 +100,33 @@ public void testRangeQuery() throws Exception {
98100
ft.rangeQuery(from, to, includeLower, includeUpper, relation, null, null, context));
99101
}
100102

103+
public void testRangeQueryUsingMappingFormat() throws Exception {
104+
Settings indexSettings = Settings.builder()
105+
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
106+
IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(randomAlphaOfLengthBetween(1, 10), indexSettings);
107+
QueryShardContext context = new QueryShardContext(0, idxSettings, null, null, null, null, null, xContentRegistry(),
108+
writableRegistry(), null, null, () -> nowInMillis, null);
109+
110+
Version version = randomFrom(Version.V_5_0_0_alpha1, Version.V_6_0_0_beta1, Version.CURRENT);
111+
RangeFieldMapper.RangeFieldType ft = new RangeFieldMapper.RangeFieldType(RangeType.DATE, version);
112+
ft.setName(FIELDNAME);
113+
ft.setIndexOptions(IndexOptions.DOCS);
114+
115+
FormatDateTimeFormatter formatter = Joda.forPattern( "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis");
116+
ft.setDateTimeFormatter(formatter);
117+
ShapeRelation relation = RandomPicks.randomFrom(random(), ShapeRelation.values());
118+
boolean includeLower = random().nextBoolean();
119+
boolean includeUpper = random().nextBoolean();
120+
Object from = nextFrom();
121+
Object to = nextTo(from);
122+
123+
boolean hasDocValues = version.onOrAfter(Version.V_6_0_0_beta1);
124+
assertEquals(
125+
RangeType.DATE.rangeQuery(FIELDNAME, hasDocValues, from, to, includeLower,
126+
includeUpper, relation, null, ft.dateMathParser(), context),
127+
ft.rangeQuery(from, to, includeLower, includeUpper, relation, null, ft.dateMathParser(), context));
128+
}
129+
101130
private Query getExpectedRangeQuery(ShapeRelation relation, Object from, Object to, boolean includeLower, boolean includeUpper) {
102131
switch (type) {
103132
case DATE:

0 commit comments

Comments
 (0)