@@ -379,17 +379,13 @@ public void testThatWeightsAreWorking() throws Exception {
379
379
public void testThatWeightMustBeAnInteger () throws Exception {
380
380
createIndexAndMapping (completionMappingBuilder );
381
381
382
- try {
383
- client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ()
384
- .startObject ().startObject (FIELD )
385
- .startArray ("input" ).value ("sth" ).endArray ()
386
- .field ("weight" , 2.5 )
387
- .endObject ().endObject ()
388
- ).get ();
389
- fail ("Indexing with a float weight was successful, but should not be" );
390
- } catch (MapperParsingException e ) {
391
- assertThat (e .toString (), containsString ("2.5" ));
392
- }
382
+ MapperParsingException e = expectThrows (MapperParsingException .class ,
383
+ () -> client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ()
384
+ .startObject ().startObject (FIELD )
385
+ .startArray ("input" ).value ("sth" ).endArray ()
386
+ .field ("weight" , 2.5 )
387
+ .endObject ().endObject ()).get ());
388
+ assertThat (e .toString (), containsString ("2.5" ));
393
389
}
394
390
395
391
public void testThatWeightCanBeAString () throws Exception {
@@ -422,34 +418,28 @@ public void testThatWeightCanBeAString() throws Exception {
422
418
public void testThatWeightMustNotBeANonNumberString () throws Exception {
423
419
createIndexAndMapping (completionMappingBuilder );
424
420
425
- try {
426
- client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ()
427
- .startObject ().startObject (FIELD )
428
- .startArray ("input" ).value ("sth" ).endArray ()
429
- .field ("weight" , "thisIsNotValid" )
430
- .endObject ().endObject ()
431
- ).get ();
432
- fail ("Indexing with a non-number representing string as weight was successful, but should not be" );
433
- } catch (MapperParsingException e ) {
434
- assertThat (e .toString (), containsString ("thisIsNotValid" ));
435
- }
421
+ MapperParsingException e = expectThrows (MapperParsingException .class ,
422
+ () -> client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ()
423
+ .startObject ().startObject (FIELD )
424
+ .startArray ("input" ).value ("sth" ).endArray ()
425
+ .field ("weight" , "thisIsNotValid" )
426
+ .endObject ().endObject ()
427
+ ).get ());
428
+ assertThat (e .toString (), containsString ("thisIsNotValid" ));
436
429
}
437
430
438
431
public void testThatWeightAsStringMustBeInt () throws Exception {
439
432
createIndexAndMapping (completionMappingBuilder );
440
433
441
434
String weight = String .valueOf (Long .MAX_VALUE - 4 );
442
- try {
443
- client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ()
444
- .startObject ().startObject (FIELD )
445
- .startArray ("input" ).value ("testing" ).endArray ()
446
- .field ("weight" , weight )
447
- .endObject ().endObject ()
448
- ).get ();
449
- fail ("Indexing with weight string representing value > Int.MAX_VALUE was successful, but should not be" );
450
- } catch (MapperParsingException e ) {
451
- assertThat (e .toString (), containsString (weight ));
452
- }
435
+
436
+ MapperParsingException e = expectThrows (MapperParsingException .class ,
437
+ () -> client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ()
438
+ .startObject ().startObject (FIELD )
439
+ .startArray ("input" ).value ("testing" ).endArray ()
440
+ .field ("weight" , weight )
441
+ .endObject ().endObject ()).get ());
442
+ assertThat (e .toString (), containsString (weight ));
453
443
}
454
444
455
445
public void testThatInputCanBeAStringInsteadOfAnArray () throws Exception {
@@ -821,13 +811,11 @@ public void testThatSortingOnCompletionFieldReturnsUsefulException() throws Exce
821
811
).get ();
822
812
823
813
refresh ();
824
- try {
825
- client ().prepareSearch (INDEX ).setTypes (TYPE ).addSort (new FieldSortBuilder (FIELD )).get ();
826
- fail ("Expected an exception due to trying to sort on completion field, but did not happen" );
827
- } catch (SearchPhaseExecutionException e ) {
828
- assertThat (e .status ().getStatus (), is (400 ));
829
- assertThat (e .toString (), containsString ("Fielddata is not supported on field [" + FIELD + "] of type [completion]" ));
830
- }
814
+
815
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class ,
816
+ () -> client ().prepareSearch (INDEX ).setTypes (TYPE ).addSort (new FieldSortBuilder (FIELD )).get ());
817
+ assertThat (e .status ().getStatus (), is (400 ));
818
+ assertThat (e .toString (), containsString ("Fielddata is not supported on field [" + FIELD + "] of type [completion]" ));
831
819
}
832
820
833
821
public void testThatSuggestStopFilterWorks () throws Exception {
@@ -1118,17 +1106,12 @@ public void testReservedChars() throws IOException {
1118
1106
.endObject ()).get ());
1119
1107
// can cause stack overflow without the default max_input_length
1120
1108
String string = "foo" + (char ) 0x00 + "bar" ;
1121
- try {
1122
- client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ()
1123
- .startObject ().startObject (FIELD )
1124
- .startArray ("input" ).value (string ).endArray ()
1125
- .field ("output" , "foobar" )
1126
- .endObject ().endObject ()
1127
- ).get ();
1128
- fail ("Expected MapperParsingException" );
1129
- } catch (MapperParsingException e ) {
1130
- assertThat (e .getMessage (), containsString ("failed to parse" ));
1131
- }
1109
+ MapperParsingException e = expectThrows (MapperParsingException .class ,
1110
+ () -> client ().prepareIndex (INDEX , TYPE , "1" ).setSource (jsonBuilder ().startObject ().startObject (FIELD )
1111
+ .startArray ("input" ).value (string ).endArray ()
1112
+ .field ("output" , "foobar" )
1113
+ .endObject ().endObject ()).get ());
1114
+ assertThat (e .getMessage (), containsString ("failed to parse" ));
1132
1115
}
1133
1116
1134
1117
// see #5930
@@ -1147,14 +1130,10 @@ public void testIssue5930() throws IOException {
1147
1130
.endObject ()
1148
1131
).setRefreshPolicy (IMMEDIATE ).get ();
1149
1132
1150
- try {
1151
- client ().prepareSearch (INDEX ).addAggregation (AggregationBuilders .terms ("suggest_agg" ).field (FIELD )
1152
- .collectMode (randomFrom (SubAggCollectionMode .values ()))).get ();
1153
- // Exception must be thrown
1154
- assertFalse (true );
1155
- } catch (SearchPhaseExecutionException e ) {
1156
- assertThat (e .toString (), containsString ("Fielddata is not supported on field [" + FIELD + "] of type [completion]" ));
1157
- }
1133
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class ,
1134
+ () -> client ().prepareSearch (INDEX ).addAggregation (AggregationBuilders .terms ("suggest_agg" ).field (FIELD )
1135
+ .collectMode (randomFrom (SubAggCollectionMode .values ()))).get ());
1136
+ assertThat (e .toString (), containsString ("Fielddata is not supported on field [" + FIELD + "] of type [completion]" ));
1158
1137
}
1159
1138
1160
1139
public void testMultiDocSuggestions () throws Exception {
@@ -1205,6 +1184,29 @@ public void testSuggestWithFieldAlias() throws Exception {
1205
1184
assertSuggestions ("suggestion" , suggestionBuilder , "apple" );
1206
1185
}
1207
1186
1187
+ public void testSuggestOnlyExplain () throws Exception {
1188
+ final CompletionMappingBuilder mapping = new CompletionMappingBuilder ();
1189
+ createIndexAndMapping (mapping );
1190
+ int numDocs = 10 ;
1191
+ List <IndexRequestBuilder > indexRequestBuilders = new ArrayList <>();
1192
+ for (int i = 1 ; i <= numDocs ; i ++) {
1193
+ indexRequestBuilders .add (client ().prepareIndex (INDEX , TYPE , "" + i )
1194
+ .setSource (jsonBuilder ()
1195
+ .startObject ()
1196
+ .startObject (FIELD )
1197
+ .field ("input" , "suggestion" + i )
1198
+ .field ("weight" , i )
1199
+ .endObject ()
1200
+ .endObject ()
1201
+ ));
1202
+ }
1203
+ indexRandom (true , indexRequestBuilders );
1204
+ CompletionSuggestionBuilder prefix = SuggestBuilders .completionSuggestion (FIELD ).prefix ("sugg" );
1205
+ SearchResponse searchResponse = client ().prepareSearch (INDEX ).setExplain (true )
1206
+ .suggest (new SuggestBuilder ().addSuggestion ("foo" , prefix )).get ();
1207
+ assertSuggestions (searchResponse , "foo" , "suggestion10" , "suggestion9" , "suggestion8" , "suggestion7" , "suggestion6" );
1208
+ }
1209
+
1208
1210
public static boolean isReservedChar (char c ) {
1209
1211
switch (c ) {
1210
1212
case '\u001F' :
0 commit comments