37
37
import org .springframework .data .annotation .Id ;
38
38
import org .springframework .data .elasticsearch .annotations .CompletionField ;
39
39
import org .springframework .data .elasticsearch .annotations .Document ;
40
- import org .springframework .data .elasticsearch .core .query .NativeSearchQuery ;
41
- import org .springframework .data .elasticsearch .core .query .NativeSearchQueryBuilder ;
42
40
import org .springframework .data .elasticsearch .core .ReactiveElasticsearchOperations ;
43
41
import org .springframework .data .elasticsearch .core .mapping .IndexCoordinates ;
44
42
import org .springframework .data .elasticsearch .core .query .IndexQuery ;
43
+ import org .springframework .data .elasticsearch .core .query .NativeSearchQueryBuilder ;
44
+ import org .springframework .data .elasticsearch .core .query .Query ;
45
45
import org .springframework .data .elasticsearch .core .suggest .response .CompletionSuggestion ;
46
46
import org .springframework .data .elasticsearch .core .suggest .response .Suggest ;
47
47
import org .springframework .data .elasticsearch .junit .jupiter .ReactiveElasticsearchRestTemplateConfiguration ;
@@ -88,34 +88,38 @@ void shouldDoSomeTest() {
88
88
@ DisplayName ("should find suggestions for given prefix completion" )
89
89
void shouldFindSuggestionsForGivenPrefixCompletion () {
90
90
91
- loadCompletionObjectEntities ().map (unused -> {
92
-
93
- NativeSearchQuery query = new NativeSearchQueryBuilder ().withSuggestBuilder (new SuggestBuilder ()
94
- .addSuggestion ("test-suggest" , SuggestBuilders .completionSuggestion ("suggest" ).prefix ("m" , Fuzziness .AUTO )))
95
- .build ();
96
-
97
- operations .suggest (query , CompletionEntity .class ) //
98
- .as (StepVerifier ::create ) //
99
- .assertNext (suggest -> {
100
- Suggest .Suggestion <? extends Suggest .Suggestion .Entry <? extends Suggest .Suggestion .Entry .Option >> suggestion = suggest
101
- .getSuggestion ("test-suggest" );
102
- assertThat (suggestion ).isNotNull ();
103
- assertThat (suggestion ).isInstanceOf (CompletionSuggestion .class );
104
- // noinspection unchecked
105
- List <CompletionSuggestion .Entry .Option <CompletionIntegrationTests .AnnotatedCompletionEntity >> options = ((CompletionSuggestion <CompletionIntegrationTests .AnnotatedCompletionEntity >) suggestion )
106
- .getEntries ().get (0 ).getOptions ();
107
- assertThat (options ).hasSize (2 );
108
- assertThat (options .get (0 ).getText ()).isIn ("Marchand" , "Mohsin" );
109
- assertThat (options .get (1 ).getText ()).isIn ("Marchand" , "Mohsin" );
110
-
111
- }) //
112
- .verifyComplete ();
113
- return Mono .empty ();
114
- });
91
+ loadCompletionObjectEntities () //
92
+ .flatMap (unused -> {
93
+ Query query = getSuggestQuery ("test-suggest" , "suggest" , "m" );
94
+ return operations .suggest (query , CompletionEntity .class );
95
+ }) //
96
+ .as (StepVerifier ::create ) //
97
+ .assertNext (suggest -> {
98
+ Suggest .Suggestion <? extends Suggest .Suggestion .Entry <? extends Suggest .Suggestion .Entry .Option >> suggestion = suggest
99
+ .getSuggestion ("test-suggest" );
100
+ assertThat (suggestion ).isNotNull ();
101
+ assertThat (suggestion ).isInstanceOf (CompletionSuggestion .class );
102
+ // noinspection unchecked
103
+ List <CompletionSuggestion .Entry .Option <CompletionIntegrationTests .AnnotatedCompletionEntity >> options = ((CompletionSuggestion <CompletionIntegrationTests .AnnotatedCompletionEntity >) suggestion )
104
+ .getEntries ().get (0 ).getOptions ();
105
+ assertThat (options ).hasSize (2 );
106
+ assertThat (options .get (0 ).getText ()).isIn ("Marchand" , "Mohsin" );
107
+ assertThat (options .get (1 ).getText ()).isIn ("Marchand" , "Mohsin" );
108
+ }) //
109
+ .verifyComplete ();
110
+ }
111
+
112
+ protected Query getSuggestQuery (String suggestionName , String fieldName , String prefix ) {
113
+ return new NativeSearchQueryBuilder () //
114
+ .withSuggestBuilder (new SuggestBuilder () //
115
+ .addSuggestion (suggestionName , //
116
+ SuggestBuilders .completionSuggestion (fieldName ) //
117
+ .prefix (prefix , Fuzziness .AUTO ))) //
118
+ .build (); //
115
119
}
116
120
117
121
// region helper functions
118
- private Mono <Void > loadCompletionObjectEntities () {
122
+ private Mono <CompletionEntity > loadCompletionObjectEntities () {
119
123
120
124
CompletionEntity rizwan_idrees = new CompletionEntityBuilder ("1" ).name ("Rizwan Idrees" )
121
125
.suggest (new String [] { "Rizwan Idrees" }).build ();
@@ -128,7 +132,7 @@ private Mono<Void> loadCompletionObjectEntities() {
128
132
List <CompletionEntity > entities = new ArrayList <>(
129
133
Arrays .asList (rizwan_idrees , franck_marchand , mohsin_husen , artur_konczak ));
130
134
IndexCoordinates index = IndexCoordinates .of (indexNameProvider .indexName ());
131
- return operations .saveAll (entities , index ).then ();
135
+ return operations .saveAll (entities , index ).last ();
132
136
}
133
137
// endregion
134
138
0 commit comments