5959import java .util .List ;
6060import java .util .Map ;
6161import java .util .function .Function ;
62+ import java .util .function .Predicate ;
6263
64+ import static java .util .Collections .emptyMap ;
6365import static org .hamcrest .Matchers .containsString ;
6466import static org .hamcrest .Matchers .equalTo ;
6567import static org .hamcrest .Matchers .instanceOf ;
@@ -85,17 +87,13 @@ protected TermsSetQueryBuilder doCreateTestQueryBuilder() {
8587 do {
8688 fieldName = randomFrom (MAPPED_FIELD_NAMES );
8789 } while (fieldName .equals (GEO_POINT_FIELD_NAME ) || fieldName .equals (GEO_SHAPE_FIELD_NAME ));
88- int numValues = randomIntBetween (0 , 10 );
89- List <Object > randomTerms = new ArrayList <>(numValues );
90- for (int i = 0 ; i < numValues ; i ++) {
91- randomTerms .add (getRandomValueForFieldName (fieldName ));
92- }
90+ List <?> randomTerms = randomValues (fieldName );
9391 TermsSetQueryBuilder queryBuilder = new TermsSetQueryBuilder (STRING_FIELD_NAME , randomTerms );
9492 if (randomBoolean ()) {
9593 queryBuilder .setMinimumShouldMatchField ("m_s_m" );
9694 } else {
9795 queryBuilder .setMinimumShouldMatchScript (
98- new Script (ScriptType .INLINE , MockScriptEngine .NAME , "_script" , Collections . emptyMap ()));
96+ new Script (ScriptType .INLINE , MockScriptEngine .NAME , "_script" , emptyMap ()));
9997 }
10098 return queryBuilder ;
10199 }
@@ -122,6 +120,41 @@ protected boolean builderGeneratesCacheableQueries() {
122120 return false ;
123121 }
124122
123+ @ Override
124+ public TermsSetQueryBuilder mutateInstance (final TermsSetQueryBuilder instance ) throws IOException {
125+ String fieldName = instance .getFieldName ();
126+ List <?> values = instance .getValues ();
127+ String minimumShouldMatchField = null ;
128+ Script minimumShouldMatchScript = null ;
129+
130+ switch (randomIntBetween (0 , 3 )) {
131+ case 0 :
132+ Predicate <String > predicate = s -> s .equals (instance .getFieldName ()) == false && s .equals (GEO_POINT_FIELD_NAME ) == false
133+ && s .equals (GEO_SHAPE_FIELD_NAME ) == false ;
134+ fieldName = randomValueOtherThanMany (predicate , () -> randomFrom (MAPPED_FIELD_NAMES ));
135+ values = randomValues (fieldName );
136+ break ;
137+ case 1 :
138+ values = randomValues (fieldName );
139+ break ;
140+ case 2 :
141+ minimumShouldMatchField = randomAlphaOfLengthBetween (1 , 10 );
142+ break ;
143+ case 3 :
144+ minimumShouldMatchScript = new Script (ScriptType .INLINE , MockScriptEngine .NAME , randomAlphaOfLength (10 ), emptyMap ());
145+ break ;
146+ }
147+
148+ TermsSetQueryBuilder newInstance = new TermsSetQueryBuilder (fieldName , values );
149+ if (minimumShouldMatchField != null ) {
150+ newInstance .setMinimumShouldMatchField (minimumShouldMatchField );
151+ }
152+ if (minimumShouldMatchScript != null ) {
153+ newInstance .setMinimumShouldMatchScript (minimumShouldMatchScript );
154+ }
155+ return newInstance ;
156+ }
157+
125158 public void testBothFieldAndScriptSpecified () {
126159 TermsSetQueryBuilder queryBuilder = new TermsSetQueryBuilder ("_field" , Collections .emptyList ());
127160 queryBuilder .setMinimumShouldMatchScript (new Script ("" ));
@@ -215,7 +248,7 @@ public void testDoToQuery_msmScriptField() throws Exception {
215248
216249 try (IndexReader ir = DirectoryReader .open (directory )) {
217250 QueryShardContext context = createShardContext ();
218- Script script = new Script (ScriptType .INLINE , MockScriptEngine .NAME , "_script" , Collections . emptyMap ());
251+ Script script = new Script (ScriptType .INLINE , MockScriptEngine .NAME , "_script" , emptyMap ());
219252 Query query = new TermsSetQueryBuilder ("message" , Arrays .asList ("a" , "b" , "c" , "d" ))
220253 .setMinimumShouldMatchScript (script ).doToQuery (context );
221254 IndexSearcher searcher = new IndexSearcher (ir );
@@ -228,6 +261,16 @@ public void testDoToQuery_msmScriptField() throws Exception {
228261 }
229262 }
230263
264+ private static List <?> randomValues (final String fieldName ) {
265+ final int numValues = randomIntBetween (0 , 10 );
266+ final List <Object > values = new ArrayList <>(numValues );
267+
268+ for (int i = 0 ; i < numValues ; i ++) {
269+ values .add (getRandomValueForFieldName (fieldName ));
270+ }
271+ return values ;
272+ }
273+
231274 public static class CustomScriptPlugin extends MockScriptPlugin {
232275
233276 @ Override
0 commit comments