22
22
import org .apache .lucene .search .Query ;
23
23
import org .elasticsearch .Version ;
24
24
import org .elasticsearch .cluster .metadata .IndexMetaData ;
25
+ import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
25
26
import org .elasticsearch .common .lucene .search .Queries ;
26
27
import org .elasticsearch .common .settings .Settings ;
28
+ import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
27
29
import org .elasticsearch .index .IndexSettings ;
28
30
import org .elasticsearch .index .fielddata .IndexFieldData ;
29
31
import org .elasticsearch .index .fielddata .plain .AbstractAtomicOrdinalsFieldData ;
37
39
import org .hamcrest .Matchers ;
38
40
39
41
import java .io .IOException ;
42
+ import java .util .Collections ;
40
43
41
44
import static org .hamcrest .Matchers .equalTo ;
42
45
import static org .hamcrest .Matchers .instanceOf ;
49
52
public class QueryShardContextTests extends ESTestCase {
50
53
51
54
public void testFailIfFieldMappingNotFound () {
52
- IndexMetaData .Builder indexMetadataBuilder = new IndexMetaData .Builder ("index" );
53
- indexMetadataBuilder .settings (Settings .builder ().put ("index.version.created" , Version .CURRENT )
54
- .put ("index.number_of_shards" , 1 )
55
- .put ("index.number_of_replicas" , 1 )
56
- );
57
- IndexMetaData indexMetaData = indexMetadataBuilder .build ();
58
- IndexSettings indexSettings = new IndexSettings (indexMetaData , Settings .EMPTY );
59
- MapperService mapperService = mock (MapperService .class );
60
- when (mapperService .getIndexSettings ()).thenReturn (indexSettings );
61
- when (mapperService .index ()).thenReturn (indexMetaData .getIndex ());
62
- final long nowInMillis = randomNonNegativeLong ();
63
-
64
- QueryShardContext context = new QueryShardContext (
65
- 0 , indexSettings , null , (mappedFieldType , idxName ) ->
66
- mappedFieldType .fielddataBuilder (idxName ).build (indexSettings , mappedFieldType , null , null , null )
67
- , mapperService , null , null , xContentRegistry (), writableRegistry (), null , null ,
68
- () -> nowInMillis , null );
69
-
55
+ QueryShardContext context = createQueryShardContext (IndexMetaData .INDEX_UUID_NA_VALUE , null );
70
56
context .setAllowUnmappedFields (false );
71
57
MappedFieldType fieldType = new TextFieldMapper .TextFieldType ();
72
58
MappedFieldType result = context .failIfFieldMappingNotFound ("name" , fieldType );
@@ -91,30 +77,16 @@ public void testFailIfFieldMappingNotFound() {
91
77
}
92
78
93
79
public void testClusterAlias () throws IOException {
94
- IndexMetaData .Builder indexMetadataBuilder = new IndexMetaData .Builder ("index" );
95
- indexMetadataBuilder .settings (Settings .builder ().put ("index.version.created" , Version .CURRENT )
96
- .put ("index.number_of_shards" , 1 )
97
- .put ("index.number_of_replicas" , 1 )
98
- );
99
- IndexMetaData indexMetaData = indexMetadataBuilder .build ();
100
- IndexSettings indexSettings = new IndexSettings (indexMetaData , Settings .EMPTY );
101
- MapperService mapperService = mock (MapperService .class );
102
- when (mapperService .getIndexSettings ()).thenReturn (indexSettings );
103
- when (mapperService .index ()).thenReturn (indexMetaData .getIndex ());
104
- final long nowInMillis = randomNonNegativeLong ();
80
+ final String clusterAlias = randomBoolean () ? null : "remote_cluster" ;
81
+ QueryShardContext context = createQueryShardContext (IndexMetaData .INDEX_UUID_NA_VALUE , clusterAlias );
105
82
106
- Mapper .BuilderContext ctx = new Mapper .BuilderContext (indexSettings .getSettings (), new ContentPath ());
83
+
84
+ Mapper .BuilderContext ctx = new Mapper .BuilderContext (context .getIndexSettings ().getSettings (), new ContentPath ());
107
85
IndexFieldMapper mapper = new IndexFieldMapper .Builder (null ).build (ctx );
108
- final String clusterAlias = randomBoolean () ? null : "remote_cluster" ;
109
- QueryShardContext context = new QueryShardContext (
110
- 0 , indexSettings , null , (mappedFieldType , indexname ) ->
111
- mappedFieldType .fielddataBuilder (indexname ).build (indexSettings , mappedFieldType , null , null , mapperService )
112
- , mapperService , null , null , xContentRegistry (), writableRegistry (), null , null ,
113
- () -> nowInMillis , clusterAlias );
114
86
115
87
IndexFieldData <?> forField = context .getForField (mapper .fieldType ());
116
- String expected = clusterAlias == null ? indexMetaData .getIndex ().getName ()
117
- : clusterAlias + ":" + indexMetaData .getIndex ().getName ();
88
+ String expected = clusterAlias == null ? context . getIndexSettings (). getIndexMetaData () .getIndex ().getName ()
89
+ : clusterAlias + ":" + context . getIndexSettings () .getIndex ().getName ();
118
90
assertEquals (expected , ((AbstractAtomicOrdinalsFieldData )forField .load (null )).getOrdinalsValues ().lookupOrd (0 ).utf8ToString ());
119
91
Query query = mapper .fieldType ().termQuery ("index" , context );
120
92
if (clusterAlias == null ) {
@@ -133,4 +105,32 @@ public void testClusterAlias() throws IOException {
133
105
assertThat (query , Matchers .instanceOf (MatchNoDocsQuery .class ));
134
106
}
135
107
108
+ public void testGetFullyQualifiedIndex () {
109
+ String clusterAlias = randomAlphaOfLengthBetween (5 , 10 );
110
+ String indexUuid = randomAlphaOfLengthBetween (3 , 10 );
111
+ QueryShardContext shardContext = createQueryShardContext (indexUuid , clusterAlias );
112
+ assertThat (shardContext .getFullyQualifiedIndex ().getName (), equalTo (clusterAlias + ":index" ));
113
+ assertThat (shardContext .getFullyQualifiedIndex ().getUUID (), equalTo (indexUuid ));
114
+ }
115
+
116
+ public static QueryShardContext createQueryShardContext (String indexUuid , String clusterAlias ) {
117
+ IndexMetaData .Builder indexMetadataBuilder = new IndexMetaData .Builder ("index" );
118
+ indexMetadataBuilder .settings (Settings .builder ().put ("index.version.created" , Version .CURRENT )
119
+ .put ("index.number_of_shards" , 1 )
120
+ .put ("index.number_of_replicas" , 1 )
121
+ .put (IndexMetaData .SETTING_INDEX_UUID , indexUuid )
122
+ );
123
+ IndexMetaData indexMetaData = indexMetadataBuilder .build ();
124
+ IndexSettings indexSettings = new IndexSettings (indexMetaData , Settings .EMPTY );
125
+ MapperService mapperService = mock (MapperService .class );
126
+ when (mapperService .getIndexSettings ()).thenReturn (indexSettings );
127
+ when (mapperService .index ()).thenReturn (indexMetaData .getIndex ());
128
+ final long nowInMillis = randomNonNegativeLong ();
129
+
130
+ return new QueryShardContext (
131
+ 0 , indexSettings , null , (mappedFieldType , idxName ) ->
132
+ mappedFieldType .fielddataBuilder (idxName ).build (indexSettings , mappedFieldType , null , null , null )
133
+ , mapperService , null , null , NamedXContentRegistry .EMPTY , new NamedWriteableRegistry (Collections .emptyList ()), null , null ,
134
+ () -> nowInMillis , clusterAlias );
135
+ }
136
136
}
0 commit comments