37
37
import org .elasticsearch .index .query .InnerHitBuilder ;
38
38
import org .elasticsearch .index .query .InnerHitBuilderTests ;
39
39
import org .elasticsearch .index .query .QueryShardContext ;
40
- import org .elasticsearch .search .SearchContextException ;
41
40
import org .elasticsearch .search .SearchModule ;
42
- import org .elasticsearch .search .internal .SearchContext ;
43
41
import org .elasticsearch .test .AbstractSerializingTestCase ;
44
42
import org .junit .AfterClass ;
45
43
import org .junit .BeforeClass ;
@@ -138,59 +136,49 @@ protected NamedXContentRegistry xContentRegistry() {
138
136
return xContentRegistry ;
139
137
}
140
138
141
- private SearchContext mockSearchContext () {
142
- SearchContext context = mock (SearchContext .class );
143
- QueryShardContext shardContext = mock (QueryShardContext .class );
144
- when (context .getQueryShardContext ()).thenReturn (shardContext );
145
- when (context .scrollContext ()).thenReturn (null );
146
- when (context .rescore ()).thenReturn (null );
147
- when (context .searchAfter ()).thenReturn (null );
148
- return context ;
149
- }
150
-
151
139
public void testBuild () throws IOException {
152
140
Directory dir = new RAMDirectory ();
153
141
try (IndexWriter writer = new IndexWriter (dir , newIndexWriterConfig (new MockAnalyzer (random ())))) {
154
142
writer .commit ();
155
143
}
156
- SearchContext searchContext = mockSearchContext ( );
144
+ QueryShardContext shardContext = mock ( QueryShardContext . class );
157
145
try (IndexReader reader = DirectoryReader .open (dir )) {
158
- when (searchContext . getQueryShardContext () .getIndexReader ()).thenReturn (reader );
146
+ when (shardContext .getIndexReader ()).thenReturn (reader );
159
147
MappedFieldType numberFieldType =
160
148
new NumberFieldMapper .NumberFieldType (NumberFieldMapper .NumberType .LONG );
161
149
MappedFieldType keywordFieldType =
162
150
new KeywordFieldMapper .KeywordFieldType ();
163
151
for (MappedFieldType fieldType : new MappedFieldType [] {numberFieldType , keywordFieldType }) {
164
152
fieldType .setName ("field" );
165
153
fieldType .setHasDocValues (true );
166
- when (searchContext . getQueryShardContext () .fieldMapper ("field" )).thenReturn (fieldType );
154
+ when (shardContext .fieldMapper ("field" )).thenReturn (fieldType );
167
155
CollapseBuilder builder = new CollapseBuilder ("field" );
168
- CollapseContext collapseContext = builder .build (searchContext );
156
+ CollapseContext collapseContext = builder .build (shardContext );
169
157
assertEquals (collapseContext .getFieldType (), fieldType );
170
158
171
159
fieldType .setIndexOptions (IndexOptions .NONE );
172
- collapseContext = builder .build (searchContext );
160
+ collapseContext = builder .build (shardContext );
173
161
assertEquals (collapseContext .getFieldType (), fieldType );
174
162
175
163
fieldType .setHasDocValues (false );
176
- SearchContextException exc = expectThrows (SearchContextException .class , () -> builder .build (searchContext ));
164
+ IllegalArgumentException exc = expectThrows (IllegalArgumentException .class , () -> builder .build (shardContext ));
177
165
assertEquals (exc .getMessage (), "cannot collapse on field `field` without `doc_values`" );
178
166
179
167
fieldType .setHasDocValues (true );
180
168
builder .setInnerHits (new InnerHitBuilder ());
181
- exc = expectThrows (SearchContextException .class , () -> builder .build (searchContext ));
169
+ exc = expectThrows (IllegalArgumentException .class , () -> builder .build (shardContext ));
182
170
assertEquals (exc .getMessage (),
183
171
"cannot expand `inner_hits` for collapse field `field`, " +
184
172
"only indexed field can retrieve `inner_hits`" );
185
173
}
186
174
}
187
175
}
188
176
189
- public void testBuildWithSearchContextExceptions () {
190
- SearchContext context = mockSearchContext ( );
177
+ public void testBuildWithExceptions () {
178
+ QueryShardContext shardContext = mock ( QueryShardContext . class );
191
179
{
192
180
CollapseBuilder builder = new CollapseBuilder ("unknown_field" );
193
- SearchContextException exc = expectThrows (SearchContextException .class , () -> builder .build (context ));
181
+ IllegalArgumentException exc = expectThrows (IllegalArgumentException .class , () -> builder .build (shardContext ));
194
182
assertEquals (exc .getMessage (), "no mapping found for `unknown_field` in order to collapse on" );
195
183
}
196
184
@@ -217,9 +205,9 @@ public Query existsQuery(QueryShardContext context) {
217
205
};
218
206
fieldType .setName ("field" );
219
207
fieldType .setHasDocValues (true );
220
- when (context . getQueryShardContext () .fieldMapper ("field" )).thenReturn (fieldType );
208
+ when (shardContext .fieldMapper ("field" )).thenReturn (fieldType );
221
209
CollapseBuilder builder = new CollapseBuilder ("field" );
222
- SearchContextException exc = expectThrows (SearchContextException .class , () -> builder .build (context ));
210
+ IllegalArgumentException exc = expectThrows (IllegalArgumentException .class , () -> builder .build (shardContext ));
223
211
assertEquals (exc .getMessage (), "unknown type for collapse field `field`, only keywords and numbers are accepted" );
224
212
}
225
213
}
0 commit comments