# Fetch suggested document from the elastic search

**URL:** <https://discuss.elastic.co/t/fetch-suggested-document-from-the-elastic-search/166268>\
**Category:** Elasticsearch\
**Created:** [January 30, 2019, 6:34am UTC](https://discuss.elastic.co/t/fetch-suggested-document-from-the-elastic-search/166268 "2019-01-30T06:34:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shyju\_M](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Shyju\_M](https://discuss.elastic.co/u/Shyju_M)\
**Post date:** [January 30, 2019, 6:34am UTC](https://discuss.elastic.co/t/fetch-suggested-document-from-the-elastic-search/166268/1 "2019-01-30T06:34:53Z")

</div>

I have integrated the complex suggester feature using the Java REST API and it returns a list of suggestions as shown below

```
["NO DATE EVENT","NO Date","no","nodateevent"]

```

How can I fetch the particular document if the user chooses one of the options from the autocomplete entry?  
Is there any way to return the id along with the name from the suggester ?

Suggestion code in Java

```
SearchRequest searchRequest = new SearchRequest( INDEX );
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());

    CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders
            .completionSuggestion( "name_suggest" )
            .prefix(searchKey).skipDuplicates(true);

    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.addSuggestion( "name_suggest", completionSuggestionBuilder );
    searchSourceBuilder.suggest( suggestBuilder );

    searchRequest.source( searchSourceBuilder );
    SearchResponse searchResponse = null;

    try
    {
        searchResponse = client.search( searchRequest,RequestOptions.DEFAULT );
    }
    catch ( IOException e )
    {
        e.printStackTrace();
    }

    List<String> result = new ArrayList<>();
    Suggest suggest = searchResponse.getSuggest();
    CompletionSuggestion entries = suggest.getSuggestion( "name_suggest" );

    for ( CompletionSuggestion.Entry entry : entries )
    {
        for ( CompletionSuggestion.Entry.Option option : entry.getOptions() )
        {
            String suggestText = option.getText().string();
            result.add(suggestText);
        }
    }
    return result;
```

---

<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:** [February 27, 2019, 6:34am UTC](https://discuss.elastic.co/t/fetch-suggested-document-from-the-elastic-search/166268/2 "2019-02-27T06:34:54Z")

</div>

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