I am using the Completition suggester for an auto complete App in Java, I was able to extract the suggest text from the Search response using the JAVA api. While checking the raw response I saw that suggest response contain the _source data (complete document instead of just the Suggest string). How to extract the source data from the Suggest Search response ?
Please find below the Code I have used to get the suggested text -
SearchRequest searchRequest = new SearchRequest("my_entitiy");
CompletionSuggestionBuilder suggestionBuilder = new CompletionSuggestionBuilder("nameSuggest");
suggestionBuilder.size(10).prefix(input).skipDuplicates(true);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.suggest(
new SuggestBuilder().addSuggestion(SUGGESTION_NAME, suggestionBuilder));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = elasticClient.search(searchRequest, RequestOptions.DEFAULT);
Suggest suggest = searchResponse.getSuggest();
Suggest.Suggestion<Suggest.Suggestion.Entry<Suggest.Suggestion.Entry.Option>> suggesition =
suggest.getSuggestion(SUGGESTION_NAME);
List<String> suggestionList = new ArrayList<>();
for (Suggest.Suggestion.Entry<Suggest.Suggestion.Entry.Option> entry : suggesition.getEntries()) {
for(Suggest.Suggestion.Entry.Option option:entry.getOptions()){
suggestionList.add(option.getText().toString());
}
}
In the Option few methods are available to extract the score, text and highlighted. Is it possible to get the _source data from the option ? I saw a toXContent function is it possible to use that to get the source data ?
Above snippet is saving the Suggested string to a list I was wondering whether it's possible to get the complete Doc JSON.