I am trying to retrieve the suggestor result from elasticsearch 5.1 but currently I am getting an empty result. I am currently using jest client. My code is below.
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("localhost:9200")
.multiThreaded(true)
.build());
JestClient client = factory.getObject();
String query="{\n" +
" \"suggest\": {\n" +
" \"text\": \"porm\",\n" +
" \"simple_phrase\": {\n" +
" \"phrase\": {\n" +
" \"field\": \"content\",\n" +
" \"size\": 1,\n" +
" \"gram_size\": 3,\n" +
" \"direct_generator\": [ {\n" +
" \"field\": \"content\",\n" +
" \"suggest_mode\": \"always\"\n" +
" } ],\n" +
" \"highlight\": {\n" +
" \"pre_tag\": \"<em>\",\n" +
" \"post_tag\": \"</em>\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Suggest suggest1 = new Suggest.Builder(query).addIndex("index").build();
SuggestResult result1 = client.execute(suggest1);
List<SuggestResult.Suggestion> suggestions = result1.getSuggestions("simple_phrase");
ArrayList<String> suggest2=new ArrayList<String>();
for (SuggestResult.Suggestion sugg:
suggestions) {
suggest2.add(sugg.text);
}
return ok(Json.toJson(suggest2));
}
I have fixed this issue.Actually there was a problem with the json query.
I have modified the query to:
String query="{\n" +
" \"simple_phrase\": {\n" +
" \"text\": \"porm\",\n" +
" \"phrase\": {\n" +
" \"field\": \"content\",\n" +
" \"size\": 1,\n" +
" \"gram_size\": 3,\n" +
" \"direct_generator\": [ {\n" +
" \"field\": \"content\",\n" +
" \"suggest_mode\": \"always\"\n" +
" } ],\n" +
" \"highlight\": {\n" +
" \"pre_tag\": \"<em>\",\n" +
" \"post_tag\": \"</em>\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";