# Java client version of the REST CompletionSuggestion

**URL:** <https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334>\
**Category:** Elastic Search\
**Created:** [September 10, 2024, 3:25pm UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334 "2024-09-10T15:25:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elopez42](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@elopez42](https://discuss.elastic.co/u/elopez42)\
**Post date:** [September 10, 2024, 3:25pm UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334/1 "2024-09-10T15:25:48Z")

</div>

I've been tasked to upgrade code from RestHighLevelClient to the ES 8 Java Client. Most of it has been pretty smooth, until I got to our CompletionSuggestion code.

Each record in the index has this field:

```auto
    "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:

```auto
    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](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/elasticsearch/core/search/CompletionSuggester.html), 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 the Java Client to return a list of suggested terms, instead of the full record?

---

<div class="post-metadata">

**Author:** ![elopez42](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@elopez42](https://discuss.elastic.co/u/elopez42)\
**Post date:** [September 10, 2024, 3:27pm UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334/2 "2024-09-10T15:27:08Z")

</div>

From #Elasticsearch to #Elastic Search

---

<div class="post-metadata">

**Author:** ![elopez42](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@elopez42](https://discuss.elastic.co/u/elopez42)\
**Post date:** [September 10, 2024, 3:27pm UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334/3 "2024-09-10T15:27:09Z")

</div>

Removed #language-clients

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [September 11, 2024, 9:03am UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334/4 "2024-09-11T09:03:47Z")

</div>

Hello and welcome!  
Yes the new CompletionSuggester is a bit convoluted in terms of usage, to map it as precisely as possible to the various responses the server can send we relied heavily on generics, thus the complexity of retrieving the results - we'll try to make it easier in the future.

I implemented the [Completion Suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html#completion-suggester) example in the documentation with the new java client, and it seems to be working as expected, here is the code for the search:

```java
SearchResponse suggestResponse = esClient.search(s -> s
    .index("music")
    .suggest(su -> su
        .suggesters("song-suggest", sg -> sg
            .completion(c -> c
                .field("suggest"))
            .prefix("nir"))), MusicSuggest.class);

```

MusicSuggest is a custom class created to map this document:

```json
  "suggest" : {
    "input": ["Nevermind", "Nirvana"],
    "weight" : 34
  }

```

so something like this (with getters and setters):

```java
public class MusicSuggest {
    private Music suggest;
}

class Music{
    private List<String> input;
    private int weight;
}

```

to get the list of suggestions from the SearchResponse:

```java
List<Suggestion> suggestions = (List<Suggestion>) suggestResponse.suggest().get("song-suggest");
List<CompletionSuggestOption> options = suggestions.get(0).completion().options();
MusicSuggest originalDocument = (MusicSuggest) options.get(0).source();
List<String> stringSuggestions = originalDocument.getSuggest().getInput();

```

of course imagine the lists being streamed/iterated instead of just reading the first value, but this should give an idea on how to proceed. Let me know if it works for your case!

---

<div class="post-metadata">

**Author:** ![elopez42](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@elopez42](https://discuss.elastic.co/u/elopez42)\
**Post date:** [September 17, 2024, 6:35pm UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334/5 "2024-09-17T18:35:03Z")

</div>

Hello Laura. Thank you for the response and suggestions. I was able to get this solution implemented and functioning, however it still isn't getting me what I need.

I'm getting a set of entire records, of which "suggest" is just one of many fields on each record. When I go through the results as you've indicated, I'm presented with the entire list of "suggest:input" strings.

In your example, when using a prefix of "nir", I am needing only "Nirvana" to be returned. What I'm getting is the entire record, with the .getSuggest().getInput() returning ["Nevermind","Nirvana"].

I can point you to exactly what I am needing because our old RestHighLevelClient code is out for the world to see. If you go to [NRD](https://nrd.gov/) and start typing in the search field, you will see a list of up to 5 possible suggestions on what you might want to search for.

---

<div class="post-metadata">

**Author:** ![elopez42](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@elopez42](https://discuss.elastic.co/u/elopez42)\
**Post date:** [September 18, 2024, 5:43pm UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334/6 "2024-09-18T17:43:43Z")

</div>

I decided trying out the search directly in Kibana to see if I could figure out what search settings will return what I needed. Lo and behold, I found the solution!

The documentation, along with Laura's help, was almost exactly right. The problem was in retrieving the results. What I needed was not in this:

```auto
MusicSuggest originalDocument = (MusicSuggest) options.get(0).source();
List<String> stringSuggestions = originalDocument.getSuggest().getInput();

```

The above just provided the entire set of records that matched the query. What I needed was to, after this code:

```auto
List<Suggestion> suggestions = (List<Suggestion>) suggestResponse.suggest().get("song-suggest");
List<CompletionSuggestOption> options = suggestions.get(0).completion().options();

```

simply walk the options as such:

```auto
for (CompletionSuggestOption o : options) {
    result.add(o.text());
}

```

The text field provides the actual match of the query prefix.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 16, 2024, 5:44pm UTC](https://discuss.elastic.co/t/java-client-version-of-the-rest-completionsuggestion/366334/7 "2024-10-16T17:44:05Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
