ES 5.1.2 Java API: Suggester: Source fields not returned


(Fuad Efendi) #1

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


(Fuad Efendi) #2

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;

(system) #3

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