19
19
20
20
package org .elasticsearch .index .mapper ;
21
21
22
- import com .carrotsearch .randomizedtesting .generators .RandomPicks ;
23
22
import org .apache .lucene .document .DoubleRange ;
24
23
import org .apache .lucene .document .FloatRange ;
25
24
import org .apache .lucene .document .InetAddressPoint ;
31
30
import org .apache .lucene .search .IndexOrDocValuesQuery ;
32
31
import org .apache .lucene .search .Query ;
33
32
import org .apache .lucene .util .BytesRef ;
33
+ import org .elasticsearch .ElasticsearchParseException ;
34
34
import org .elasticsearch .Version ;
35
35
import org .elasticsearch .cluster .metadata .IndexMetaData ;
36
36
import org .elasticsearch .common .geo .ShapeRelation ;
37
+ import org .elasticsearch .common .joda .FormatDateTimeFormatter ;
37
38
import org .elasticsearch .common .joda .Joda ;
38
39
import org .elasticsearch .common .network .InetAddresses ;
39
40
import org .elasticsearch .common .settings .Settings ;
40
41
import org .elasticsearch .index .IndexSettings ;
42
+ import org .elasticsearch .index .mapper .RangeFieldMapper .RangeFieldType ;
41
43
import org .elasticsearch .index .mapper .RangeFieldMapper .RangeType ;
42
44
import org .elasticsearch .index .query .QueryShardContext ;
43
45
import org .elasticsearch .test .IndexSettingsModule ;
@@ -55,49 +57,80 @@ public class RangeFieldTypeTests extends FieldTypeTestCase {
55
57
56
58
@ Before
57
59
public void setupProperties () {
58
- type = RandomPicks . randomFrom (random (), RangeType .values ());
60
+ type = randomFrom (RangeType .values ());
59
61
nowInMillis = randomNonNegativeLong ();
60
62
if (type == RangeType .DATE ) {
61
63
addModifier (new Modifier ("format" , true ) {
62
64
@ Override
63
65
public void modify (MappedFieldType ft ) {
64
- ((RangeFieldMapper . RangeFieldType ) ft ).setDateTimeFormatter (Joda .forPattern ("basic_week_date" , Locale .ROOT ));
66
+ ((RangeFieldType ) ft ).setDateTimeFormatter (Joda .forPattern ("basic_week_date" , Locale .ROOT ));
65
67
}
66
68
});
67
69
addModifier (new Modifier ("locale" , true ) {
68
70
@ Override
69
71
public void modify (MappedFieldType ft ) {
70
- ((RangeFieldMapper . RangeFieldType ) ft ).setDateTimeFormatter (Joda .forPattern ("date_optional_time" , Locale .CANADA ));
72
+ ((RangeFieldType ) ft ).setDateTimeFormatter (Joda .forPattern ("date_optional_time" , Locale .CANADA ));
71
73
}
72
74
});
73
75
}
74
76
}
75
77
76
78
@ Override
77
- protected RangeFieldMapper . RangeFieldType createDefaultFieldType () {
78
- return new RangeFieldMapper . RangeFieldType (type , Version .CURRENT );
79
+ protected RangeFieldType createDefaultFieldType () {
80
+ return new RangeFieldType (type , Version .CURRENT );
79
81
}
80
82
81
83
public void testRangeQuery () throws Exception {
82
- Settings indexSettings = Settings .builder ()
83
- .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
84
- IndexSettings idxSettings = IndexSettingsModule .newIndexSettings (randomAlphaOfLengthBetween (1 , 10 ), indexSettings );
85
- QueryShardContext context = new QueryShardContext (0 , idxSettings , null , null , null , null , null , xContentRegistry (),
86
- writableRegistry (), null , null , () -> nowInMillis , null );
87
- RangeFieldMapper .RangeFieldType ft = new RangeFieldMapper .RangeFieldType (type , Version .CURRENT );
84
+ QueryShardContext context = createContext ();
85
+ RangeFieldType ft = new RangeFieldType (type , Version .CURRENT );
88
86
ft .setName (FIELDNAME );
89
87
ft .setIndexOptions (IndexOptions .DOCS );
90
88
91
- ShapeRelation relation = RandomPicks . randomFrom (random (), ShapeRelation .values ());
92
- boolean includeLower = random (). nextBoolean ();
93
- boolean includeUpper = random (). nextBoolean ();
89
+ ShapeRelation relation = randomFrom (ShapeRelation .values ());
90
+ boolean includeLower = randomBoolean ();
91
+ boolean includeUpper = randomBoolean ();
94
92
Object from = nextFrom ();
95
93
Object to = nextTo (from );
96
94
97
95
assertEquals (getExpectedRangeQuery (relation , from , to , includeLower , includeUpper ),
98
96
ft .rangeQuery (from , to , includeLower , includeUpper , relation , null , null , context ));
99
97
}
100
98
99
+ private QueryShardContext createContext () {
100
+ Settings indexSettings = Settings .builder ()
101
+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
102
+ IndexSettings idxSettings = IndexSettingsModule .newIndexSettings (randomAlphaOfLengthBetween (1 , 10 ), indexSettings );
103
+ return new QueryShardContext (0 , idxSettings , null , null , null , null , null , xContentRegistry (),
104
+ writableRegistry (), null , null , () -> nowInMillis , null );
105
+ }
106
+
107
+ public void testDateRangeQueryUsingMappingFormat () {
108
+ QueryShardContext context = createContext ();
109
+ RangeFieldType fieldType = new RangeFieldType (RangeType .DATE , Version .CURRENT );
110
+ fieldType .setName (FIELDNAME );
111
+ fieldType .setIndexOptions (IndexOptions .DOCS );
112
+ fieldType .setHasDocValues (false );
113
+ ShapeRelation relation = randomFrom (ShapeRelation .values ());
114
+
115
+ // dates will break the default format
116
+ final String from = "2016-15-06T15:29:50+08:00" ;
117
+ final String to = "2016-16-06T15:29:50+08:00" ;
118
+
119
+ ElasticsearchParseException ex = expectThrows (ElasticsearchParseException .class ,
120
+ () -> fieldType .rangeQuery (from , to , true , true , relation , null , null , context ));
121
+ assertEquals ("failed to parse date field [2016-15-06T15:29:50+08:00] with format [strict_date_optional_time||epoch_millis]" ,
122
+ ex .getMessage ());
123
+
124
+ // setting mapping format which is compatible with those dates
125
+ final FormatDateTimeFormatter formatter = Joda .forPattern ("yyyy-dd-MM'T'HH:mm:ssZZ" );
126
+ assertEquals (1465975790000L , formatter .parser ().parseMillis (from ));
127
+ assertEquals (1466062190000L , formatter .parser ().parseMillis (to ));
128
+
129
+ fieldType .setDateTimeFormatter (formatter );
130
+ final Query query = fieldType .rangeQuery (from , to , true , true , relation , null , null , context );
131
+ assertEquals ("field:<ranges:[1465975790000 : 1466062190000]>" , query .toString ());
132
+ }
133
+
101
134
private Query getExpectedRangeQuery (ShapeRelation relation , Object from , Object to , boolean includeLower , boolean includeUpper ) {
102
135
switch (type ) {
103
136
case DATE :
@@ -277,14 +310,10 @@ public void testParseIp() {
277
310
assertEquals (InetAddresses .forString ("::1" ), RangeFieldMapper .RangeType .IP .parse (new BytesRef ("::1" ), randomBoolean ()));
278
311
}
279
312
280
- public void testTermQuery () throws Exception , IllegalArgumentException {
313
+ public void testTermQuery () throws Exception {
281
314
// See https://github.com/elastic/elasticsearch/issues/25950
282
- Settings indexSettings = Settings .builder ()
283
- .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
284
- IndexSettings idxSettings = IndexSettingsModule .newIndexSettings (randomAlphaOfLengthBetween (1 , 10 ), indexSettings );
285
- QueryShardContext context = new QueryShardContext (0 , idxSettings , null , null , null , null , null , xContentRegistry (),
286
- writableRegistry (), null , null , () -> nowInMillis , null );
287
- RangeFieldMapper .RangeFieldType ft = new RangeFieldMapper .RangeFieldType (type , Version .CURRENT );
315
+ QueryShardContext context = createContext ();
316
+ RangeFieldType ft = new RangeFieldType (type , Version .CURRENT );
288
317
ft .setName (FIELDNAME );
289
318
ft .setIndexOptions (IndexOptions .DOCS );
290
319
0 commit comments