Search code examples
elasticsearchelasticsearch-java-api

Retrieve documents from Completion Suggester using Elasticsearch Java API


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

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

{
  "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

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("product-suggest").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?


Solution

  • org.elasticsearch.search.suggest.Suggest suggest = searchResponse.getSuggest();
    org.elasticsearch.search.suggest.completion.CompletionSuggestion suggestion = suggest.getSuggestion("product-suggest");
    
    for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry entry : suggestion.getEntries()) {
         for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option option : entry) {
            SearchHit hit = option.getHit();
            System.out.println(hit.getSourceAsString());
         }
    }
    

    There are two problems:

    1. you are using wrong suggester name search but it is product-suggest.
    2. You are not casting it. So you are getting only parent class options from suggester. Once you assign it to a proper type, you will get hits from that you cam get _source

    Older answer:

    import org.apache.http.util.EntityUtils; //import
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    
    String responseEntity = EntityUtils.toString(searchResponse.getEntity());
    JsonObject obj = new JsonParser().parse(responseEntity).getAsJsonObject();
                JsonArray completionSUggesterArr = obj.getAsJsonObject("suggest").getAsJsonArray("completion-suggester");
                System.out.println(completionSUggesterArr.get(0).getAsJsonObject().getAsJsonArray("options"));
    
    for(int i = 0; i < completionSUggesterArr.size(); i++) {
            JsonArray arr = completionSUggesterArr.get(i).getAsJsonObject().get("options").getAsJsonArray();
            System.out.println(arr.get(0).getAsJsonObject().get("_source"));
    }
    

    This one is for low level client.