Edgar Lopez
Edgar Lopez

Reputation: 1

ElasticSearch 8 CompletionSuggestion Replacement

I've been tasked to upgrade code from ElasticSearch 7 to ES 8. Most of it has been pretty smooth, until I got to our CompletionSuggestion code.

Each record in the index has this field:

    "suggest" : {
      "type" : "completion",
      "analyzer" : "simple",
      "preserve_separators" : true,
      "preserve_position_increments" : true,
      "max_input_length" : 50
    }

When the user starts typing in the search field, we present them with a list of possible search terms to use, based on results returned from the above "suggest" field.

Our current code:

    List<String> result = new ArrayList<>();
    CompletionSuggestionBuilder csb = new CompletionSuggestionBuilder("suggest");
    csb.text(query).size(100);  // query is the incoming search text

    SearchSourceBuilder ssb = new SearchSourceBuilder();
    SearchRequest sr = new SearchRequest(index);
    ssb.suggest(new SuggestBuilder()
            .addSuggestion("Suggestion", csb));
    sr.source(ssb);
    SearchResponse resp = rhlClient.search(sr, RequestOptions.DEFAULT);

    if (resp.getSuggest().size() == 0) {
        return result;
    }
    
    Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestionBldr = resp.getSuggest().getSuggestion("Suggestion");
    Iterator<? extends Suggest.Suggestion.Entry.Option> iterator =
            suggestionBldr.iterator().next().getOptions().iterator();
    // We then just loop through the iterator and build the result

I have tried using the new CompletionSuggester, along with other new options, but each tutorial or how-to I've seen/attempted returns a list of the the full records, not a list of search terms like we were getting before.

How can I get ES8 to return a list of suggested terms, instead of the full record?

Upvotes: 0

Views: 59

Answers (0)

Related Questions