34
34
import org .apache .lucene .search .PhraseQuery ;
35
35
import org .apache .lucene .search .Query ;
36
36
import org .apache .lucene .search .TermQuery ;
37
+ import org .apache .lucene .search .TermRangeQuery ;
37
38
import org .apache .lucene .store .Directory ;
38
39
import org .apache .lucene .util .BytesRef ;
39
- import org .elasticsearch .core .internal .io .IOUtils ;
40
40
import org .elasticsearch .Version ;
41
41
import org .elasticsearch .cluster .metadata .IndexMetaData ;
42
42
import org .elasticsearch .common .UUIDs ;
43
43
import org .elasticsearch .common .lucene .search .Queries ;
44
44
import org .elasticsearch .common .settings .Settings ;
45
+ import org .elasticsearch .core .internal .io .IOUtils ;
45
46
import org .elasticsearch .index .IndexSettings ;
46
47
import org .elasticsearch .index .query .QueryShardContext ;
47
48
import org .elasticsearch .test .VersionUtils ;
51
52
import java .util .Collections ;
52
53
import java .util .Set ;
53
54
55
+ import static org .hamcrest .Matchers .instanceOf ;
56
+
54
57
public class TypeFieldTypeTests extends FieldTypeTestCase {
55
58
@ Override
56
59
protected MappedFieldType createDefaultFieldType () {
57
60
return new TypeFieldMapper .TypeFieldType ();
58
61
}
59
62
60
- public void testTermsQueryWhenTypesAreDisabled () throws Exception {
63
+ private QueryShardContext createMockContext ( Version versionFrom , Version versionTo ) {
61
64
QueryShardContext context = Mockito .mock (QueryShardContext .class );
62
- Version indexVersionCreated = VersionUtils .randomVersionBetween (random (), Version . V_6_0_0 , Version . CURRENT );
65
+ Version indexVersionCreated = VersionUtils .randomVersionBetween (random (), versionFrom , versionTo );
63
66
Settings indexSettings = Settings .builder ()
64
67
.put (IndexMetaData .SETTING_VERSION_CREATED , indexVersionCreated )
65
68
.put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 )
@@ -70,6 +73,12 @@ public void testTermsQueryWhenTypesAreDisabled() throws Exception {
70
73
Mockito .when (context .getIndexSettings ()).thenReturn (mockSettings );
71
74
Mockito .when (context .indexVersionCreated ()).thenReturn (indexVersionCreated );
72
75
76
+ return context ;
77
+ }
78
+
79
+ public void testTermsQueryWhenTypesAreDisabled () throws Exception {
80
+ QueryShardContext context = createMockContext (Version .V_6_0_0 , Version .CURRENT );
81
+
73
82
MapperService mapperService = Mockito .mock (MapperService .class );
74
83
Set <String > types = Collections .emptySet ();
75
84
Mockito .when (mapperService .types ()).thenReturn (types );
@@ -100,16 +109,7 @@ public void testTermsQueryWhenTypesAreEnabled() throws Exception {
100
109
IndexWriter w = new IndexWriter (dir , newIndexWriterConfig ());
101
110
IndexReader reader = openReaderWithNewType ("my_type" , w );
102
111
103
- QueryShardContext context = Mockito .mock (QueryShardContext .class );
104
- Settings indexSettings = Settings .builder ()
105
- .put (IndexMetaData .SETTING_VERSION_CREATED , Version .V_5_6_0 ) // to allow for multiple types
106
- .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 )
107
- .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
108
- .put (IndexMetaData .SETTING_INDEX_UUID , UUIDs .randomBase64UUID ())
109
- .build ();
110
- IndexMetaData indexMetaData = IndexMetaData .builder (IndexMetaData .INDEX_UUID_NA_VALUE ).settings (indexSettings ).build ();
111
- IndexSettings mockSettings = new IndexSettings (indexMetaData , Settings .EMPTY );
112
- Mockito .when (context .getIndexSettings ()).thenReturn (mockSettings );
112
+ QueryShardContext context = createMockContext (Version .V_5_6_0 , Version .V_5_6_0 ); // to allow for multiple types
113
113
114
114
TypeFieldMapper .TypeFieldType ft = new TypeFieldMapper .TypeFieldType ();
115
115
ft .setName (TypeFieldMapper .NAME );
@@ -166,6 +166,43 @@ public void testTermsQueryWhenTypesAreEnabled() throws Exception {
166
166
IOUtils .close (reader , w , dir );
167
167
}
168
168
169
+ public void testRangeWhenTypesAreDisabled () throws Exception {
170
+ QueryShardContext context = createMockContext (Version .V_6_0_0 , Version .CURRENT );
171
+
172
+ MapperService mapperService = Mockito .mock (MapperService .class );
173
+ Set <String > types = Collections .emptySet ();
174
+ Mockito .when (mapperService .types ()).thenReturn (types );
175
+ Mockito .when (context .getMapperService ()).thenReturn (mapperService );
176
+
177
+ TypeFieldMapper .TypeFieldType ft = new TypeFieldMapper .TypeFieldType ();
178
+ ft .setName (TypeFieldMapper .NAME );
179
+ Query query = ft .rangeQuery ("a_type" , "z_type" , randomBoolean (), randomBoolean (), context );
180
+ assertEquals (new MatchNoDocsQuery (), query );
181
+
182
+ types = Collections .singleton ("my_type" );
183
+ Mockito .when (mapperService .types ()).thenReturn (types );
184
+ query = ft .rangeQuery ("a_type" , "z_type" , randomBoolean (), randomBoolean (), context );
185
+ assertEquals (new MatchAllDocsQuery (), query );
186
+
187
+ query = ft .rangeQuery ("n_type" , "z_type" , randomBoolean (), randomBoolean (), context );
188
+ assertEquals (new MatchNoDocsQuery (), query );
189
+
190
+ query = ft .rangeQuery ("a_type" , "l_type" , randomBoolean (), randomBoolean (), context );
191
+ assertEquals (new MatchNoDocsQuery (), query );
192
+ assertWarnings ("Running [range] query on [_type] field for an index with a single type. As types are deprecated, this "
193
+ + "functionality will be removed in future releases." );
194
+ }
195
+
196
+ public void testRangeWhenTypesEnabled () throws Exception {
197
+ TypeFieldMapper .TypeFieldType ft = new TypeFieldMapper .TypeFieldType ();
198
+ ft .setName (TypeFieldMapper .NAME );
199
+ String lowerTerm = randomBoolean () ? "a_type" : null ;
200
+ String upperTerm = randomBoolean () ? "z_type" : null ;
201
+ QueryShardContext context = createMockContext (Version .V_5_6_0 , Version .V_5_6_0 ); // to allow for multiple types
202
+ Query query = ft .rangeQuery (lowerTerm , upperTerm , randomBoolean (), randomBoolean (), context );
203
+ assertThat (query , instanceOf (TermRangeQuery .class ));
204
+ }
205
+
169
206
static DirectoryReader openReaderWithNewType (String type , IndexWriter writer ) throws IOException {
170
207
Document doc = new Document ();
171
208
StringField typeField = new StringField (TypeFieldMapper .NAME , type , Store .NO );
0 commit comments