1919
2020package org .elasticsearch .search .suggest ;
2121
22+ import org .apache .lucene .analysis .core .SimpleAnalyzer ;
23+ import org .elasticsearch .Version ;
24+ import org .elasticsearch .cluster .metadata .IndexMetaData ;
2225import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
2326import org .elasticsearch .common .io .stream .Writeable ;
2427import org .elasticsearch .common .settings .Settings ;
2831import org .elasticsearch .common .xcontent .XContentFactory ;
2932import org .elasticsearch .common .xcontent .XContentParser ;
3033import org .elasticsearch .common .xcontent .XContentType ;
34+ import org .elasticsearch .index .Index ;
35+ import org .elasticsearch .index .IndexSettings ;
36+ import org .elasticsearch .index .analysis .AnalyzerScope ;
37+ import org .elasticsearch .index .analysis .NamedAnalyzer ;
38+ import org .elasticsearch .index .mapper .MappedFieldType ;
39+ import org .elasticsearch .index .mapper .MapperService ;
40+ import org .elasticsearch .index .query .QueryShardContext ;
41+ import org .elasticsearch .mock .orig .Mockito ;
42+ import org .elasticsearch .script .Script ;
43+ import org .elasticsearch .script .ScriptService ;
44+ import org .elasticsearch .script .TemplateScript ;
3145import org .elasticsearch .search .SearchModule ;
46+ import org .elasticsearch .search .suggest .SuggestionSearchContext .SuggestionContext ;
3247import org .elasticsearch .test .ESTestCase ;
48+ import org .elasticsearch .test .IndexSettingsModule ;
3349import org .junit .AfterClass ;
3450import org .junit .BeforeClass ;
3551
3652import java .io .IOException ;
3753
3854import static java .util .Collections .emptyList ;
55+ import static org .elasticsearch .common .lucene .BytesRefs .toBytesRef ;
3956import static org .elasticsearch .test .EqualsHashCodeTestUtils .checkEqualsAndHashCode ;
57+ import static org .mockito .Matchers .any ;
58+ import static org .mockito .Mockito .mock ;
59+ import static org .mockito .Mockito .when ;
4060
4161public abstract class AbstractSuggestionBuilderTestCase <SB extends SuggestionBuilder <SB >> extends ESTestCase {
4262
@@ -48,7 +68,7 @@ public abstract class AbstractSuggestionBuilderTestCase<SB extends SuggestionBui
4868 * setup for the whole base test class
4969 */
5070 @ BeforeClass
51- public static void init () throws IOException {
71+ public static void init () {
5272 SearchModule searchModule = new SearchModule (Settings .EMPTY , false , emptyList ());
5373 namedWriteableRegistry = new NamedWriteableRegistry (searchModule .getNamedWriteables ());
5474 xContentRegistry = new NamedXContentRegistry (searchModule .getNamedXContents ());
@@ -98,7 +118,7 @@ public static void setCommonPropertiesOnRandomBuilder(SuggestionBuilder<?> rando
98118 /**
99119 * Test equality and hashCode properties
100120 */
101- public void testEqualsAndHashcode () throws IOException {
121+ public void testEqualsAndHashcode () {
102122 for (int runs = 0 ; runs < NUMBER_OF_TESTBUILDERS ; runs ++) {
103123 checkEqualsAndHashCode (randomTestBuilder (), this ::copy , this ::mutate );
104124 }
@@ -131,6 +151,62 @@ public void testFromXContent() throws IOException {
131151 }
132152 }
133153
154+ public void testBuild () throws IOException {
155+ Settings indexSettings = Settings .builder ().put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
156+ IndexSettings idxSettings = IndexSettingsModule .newIndexSettings (new Index (randomAlphaOfLengthBetween (1 , 10 ), "_na_" ),
157+ indexSettings );
158+ MapperService mapperService = mock (MapperService .class );
159+ ScriptService scriptService = mock (ScriptService .class );
160+ MappedFieldType fieldType = mockFieldType ();
161+ boolean fieldTypeSearchAnalyzerSet = randomBoolean ();
162+ if (fieldTypeSearchAnalyzerSet ) {
163+ NamedAnalyzer searchAnalyzer = new NamedAnalyzer ("fieldSearchAnalyzer" , AnalyzerScope .INDEX , new SimpleAnalyzer ());
164+ if (Mockito .mockingDetails (fieldType ).isMock ()) {
165+ when (fieldType .searchAnalyzer ()).thenReturn (searchAnalyzer );
166+ } else {
167+ fieldType .setSearchAnalyzer (searchAnalyzer );
168+ }
169+ } else {
170+ when (mapperService .searchAnalyzer ())
171+ .thenReturn (new NamedAnalyzer ("mapperServiceSearchAnalyzer" , AnalyzerScope .INDEX , new SimpleAnalyzer ()));
172+ }
173+ when (mapperService .fullName (any (String .class ))).thenReturn (fieldType );
174+ when (mapperService .getNamedAnalyzer (any (String .class ))).then (
175+ invocation -> new NamedAnalyzer ((String ) invocation .getArguments ()[0 ], AnalyzerScope .INDEX , new SimpleAnalyzer ()));
176+ when (scriptService .compile (any (Script .class ), any ())).thenReturn (mock (TemplateScript .Factory .class ));
177+ QueryShardContext mockShardContext = new QueryShardContext (0 , idxSettings , null , null , mapperService , null , scriptService ,
178+ xContentRegistry (), null , null , System ::currentTimeMillis );
179+
180+ for (int runs = 0 ; runs < NUMBER_OF_TESTBUILDERS ; runs ++) {
181+ SB suggestionBuilder = randomTestBuilder ();
182+ SuggestionContext suggestionContext = suggestionBuilder .build (mockShardContext );
183+ assertEquals (toBytesRef (suggestionBuilder .text ()), suggestionContext .getText ());
184+ if (suggestionBuilder .text () != null && suggestionBuilder .prefix () == null ) {
185+ assertEquals (toBytesRef (suggestionBuilder .text ()), suggestionContext .getPrefix ());
186+ } else {
187+ assertEquals (toBytesRef (suggestionBuilder .prefix ()), suggestionContext .getPrefix ());
188+ }
189+ assertEquals (toBytesRef (suggestionBuilder .regex ()), suggestionContext .getRegex ());
190+ assertEquals (suggestionBuilder .field (), suggestionContext .getField ());
191+ int expectedSize = suggestionBuilder .size () != null ? suggestionBuilder .size : 5 ;
192+ assertEquals (expectedSize , suggestionContext .getSize ());
193+ Integer expectedShardSize = suggestionBuilder .shardSize != null ? suggestionBuilder .shardSize : Math .max (expectedSize , 5 );
194+ assertEquals (expectedShardSize , suggestionContext .getShardSize ());
195+ assertSame (mockShardContext , suggestionContext .getShardContext ());
196+ if (suggestionBuilder .analyzer () != null ) {
197+ assertEquals (suggestionBuilder .analyzer (), ((NamedAnalyzer ) suggestionContext .getAnalyzer ()).name ());
198+ } else if (fieldTypeSearchAnalyzerSet ){
199+ assertEquals ("fieldSearchAnalyzer" , ((NamedAnalyzer ) suggestionContext .getAnalyzer ()).name ());
200+ } else {
201+ assertEquals ("mapperServiceSearchAnalyzer" , ((NamedAnalyzer ) suggestionContext .getAnalyzer ()).name ());
202+ }
203+ }
204+ }
205+
206+ protected MappedFieldType mockFieldType () {
207+ return mock (MappedFieldType .class );
208+ }
209+
134210 /**
135211 * Subclasses can override this method and return a set of fields which should be protected from
136212 * recursive random shuffling in the {@link #testFromXContent()} test case
0 commit comments