# Retrieve documents from Completion Suggester using Elasticsearch Java API

**URL:** <https://discuss.elastic.co/t/retrieve-documents-from-completion-suggester-using-elasticsearch-java-api/238810>\
**Category:** Elasticsearch\
**Created:** [June 26, 2020, 8:29am UTC](https://discuss.elastic.co/t/retrieve-documents-from-completion-suggester-using-elasticsearch-java-api/238810 "2020-06-26T08:29:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitterhh](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@gitterhh](https://discuss.elastic.co/u/gitterhh)\
**Post date:** [June 26, 2020, 8:29am UTC](https://discuss.elastic.co/t/retrieve-documents-from-completion-suggester-using-elasticsearch-java-api/238810/1 "2020-06-26T08:29:11Z")

</div>

When I execute completion suggester request using curl, in the response I can find also the documents

```auto
curl -X GET 'localhost:9200/shop/_search?pretty&size=20' -H 'Content-Type: application/json' -d'{
    "suggest": {
        "product-suggest" : {
            "prefix" : "phone",
            "completion" : {
                "field" : "title",
                "skip_duplicates": true
            }
        }
    }
}'

```

the response

```auto
{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "suggest" : {
    "product-suggest" : [
      {
        "text" : "phone",
        "offset" : 0,
        "length" : 12,
        "options" : [
          {
            "text" : "Phone",
            "_index" : "shop",
            "_type" : "_doc",
            "_id" : "a02fd264-c25c-4634-8bd9-e275a293ce1d",
            "_score" : 1.0,
            "_source" : {
              "title" : "Phone",
              "price" : 219.99
            }
          }
        ]
      }
    ]
  }
}

```

So, in the response I can find the document in the "\_source" field.

Using Java API

```auto
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.suggest(new SuggestBuilder()
                .addSuggestion("product-suggest",
                        new CompletionSuggestionBuilder("title").prefix("phone").skipDuplicates(true)));
        searchRequest.source(searchSourceBuilder);

SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

searchResponse.getSuggest().getSuggestion("search").getEntries().get(0).getOptions(.get(0).getText();

```

So, I can find the method to retrieve the text `getText`, but there are no method to retrieve the source.

Is there some way to retrieve the `source`?

---

<div class="post-metadata">

**Author:** ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)\
**Post date:** [June 26, 2020, 9:16am UTC](https://discuss.elastic.co/t/retrieve-documents-from-completion-suggester-using-elasticsearch-java-api/238810/2 "2020-06-26T09:16:55Z")

</div>

You need to type cast suggestion to CompletionSuggestion.

```auto
String s = ((CompletionSuggestion)searchResponse.getSuggest().getSuggestion("product-suggest")).getOptions().get(0).getHit().getSourceAsString();
        
System.out.println(s);

```

---

<div class="post-metadata">

**Author:** ![gitterhh](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@gitterhh](https://discuss.elastic.co/u/gitterhh)\
**Post date:** [June 26, 2020, 10:18am UTC](https://discuss.elastic.co/t/retrieve-documents-from-completion-suggester-using-elasticsearch-java-api/238810/3 "2020-06-26T10:18:28Z")

</div>

Thank you.

---

<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:** [July 24, 2020, 10:30am UTC](https://discuss.elastic.co/t/retrieve-documents-from-completion-suggester-using-elasticsearch-java-api/238810/4 "2020-07-24T10:30:04Z")

</div>

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