# ES 5.1.2 Java API: Suggester: Source fields not returned

**URL:** <https://discuss.elastic.co/t/es-5-1-2-java-api-suggester-source-fields-not-returned/72711>\
**Category:** Elasticsearch\
**Created:** [January 24, 2017, 11:28pm UTC](https://discuss.elastic.co/t/es-5-1-2-java-api-suggester-source-fields-not-returned/72711 "2017-01-24T23:28:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fuad\_Efendi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fuad_efendi/32/14876_2.png) [@Fuad\_Efendi](https://discuss.elastic.co/u/Fuad_Efendi)\
**Post date:** [January 24, 2017, 11:28pm UTC](https://discuss.elastic.co/t/es-5-1-2-java-api-suggester-source-fields-not-returned/72711/1 "2017-01-24T23:28:25Z")

</div>

I have following Java CLient snippet and I have difficulties in finding "fields" from "\_source":

```
CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion("country_suggest").prefix(prefix).size(10);
SearchResponse searchResponse = client.prepareSearch("country-003").suggest(
        new SuggestBuilder().addSuggestion("foo", completionSuggestionBuilder)).setFetchSource(true).get();
CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo");
CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0);
for (CompletionSuggestion.Entry.Option option : options) {
    SearchHit hit = option.getHit();
}

```

"hit.fields()" returns empty Map.

This Java code generates following query:

```
{
  "_source" : {
    "includes" : [],
    "excludes" : []
  },
  "suggest" : {
    "foo" : {
      "prefix" : "u",
      "completion" : {
        "field" : "country_suggest",
        "size" : 10
      }
    }
  },
  "ext" : { }
}

```

I run it in console; I have correct search results:

```
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
  },
  "hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : []
  },
  "suggest" : {
"foo" : [
  {
    "text" : "u",
    "offset" : 0,
    "length" : 1,
    "options" : [
      {
        "text" : "United States",
        "_index" : "country-003",
        "_type" : "CountryNames",
        "_id" : "6252001",
        "_score" : 3.10232864E8,
        "_source" : {
          "iso" : "US",
          "iso3" : "USA",
          "isoNumeric" : "840",
          "fips" : "US",
          "country" : "United States",
          "country_suggest" : {
            "input" : "United States",
            "weight" : "310232863"
          },
          "capital" : "Washington",
          "areaSqKm" : "9629091",
          "population" : "310232863",
          "continent" : "NA",
          "tld" : ".us",
          "currencyCode" : "USD",
          "currencyName" : "Dollar",
          "phone" : "1",
          "postalCodeFormat" : "#####-####",
          "postalCodeRegex" : "^\\d{5}(-\\d{4})?$",
          "languages" : "en-US,es-US,haw,fr",
          "geonameid" : "6252001",
          "neighbours" : "CA,MX,CU",
          "equivalentFipsCode" : ""
        }
      },

```

Where is a problem, should I use hits-API or anything else to get "source"? Thanks,

Fuad Efendi

---

<div class="post-metadata">

**Author:** ![Fuad\_Efendi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fuad_efendi/32/14876_2.png) [@Fuad\_Efendi](https://discuss.elastic.co/u/Fuad_Efendi)\
**Post date:** [January 24, 2017, 11:42pm UTC](https://discuss.elastic.co/t/es-5-1-2-java-api-suggester-source-fields-not-returned/72711/2 "2017-01-24T23:42:45Z")

</div>

Ok, I found & tested it, following methods of SearchHit interface work fine for me:

```
/**
 * The source of the document (can be <tt>null</tt>). Note, its a copy of the source
 * into a byte array, consider using {@link #sourceRef()} so there won't be a need to copy.
 */
byte[] source();

/**
 * Is the source available or not. A source with no fields will return true. This will return false if {@code fields} doesn't contain
 * {@code _source} or if source is disabled in the mapping.
 */
boolean hasSource();

/**
 * The source of the document as a map (can be <tt>null</tt>).
 */
Map<String, Object> getSource();

/**
 * The source of the document as string (can be <tt>null</tt>).
 */
String sourceAsString();

/**
 * The source of the document as string (can be <tt>null</tt>).
 */
String getSourceAsString();

/**
 * The source of the document as a map (can be <tt>null</tt>).
 */
Map<String, Object> sourceAsMap() throws ElasticsearchParseException;
```

---

<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 21, 2017, 11:43pm UTC](https://discuss.elastic.co/t/es-5-1-2-java-api-suggester-source-fields-not-returned/72711/3 "2017-02-21T23:43:16Z")

</div>

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