I am trying to integrate the autocompletion feature by following the article https://www.elastic.co/blog/you-complete-me. I couldn't see any article to implement the same with the JAVA API
Please guide me to do the same.
Here is the code to insert the json object from the post request to the elastic search
public String createEventDocument(Event document) throws Exception {
IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.idAsString())
.source(convertEventDocumentToMap(document));
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
return indexResponse.getResult().name();
}
Code to convert object to the map
private Map<String, Object> convertEventDocumentToMap(Event evt) {
return objectMapper.convertValue(evt, Map.class);
}
How can I add the completion suggester filed before inserting the object here?
Search code
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(INDEX);
searchRequest.types(TYPE);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder =
SuggestBuilders.termSuggestion("name").text(name);
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
searchSourceBuilder.suggest(suggestBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
Suggest suggest = searchResponse.getSuggest();
TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
for (TermSuggestion.Entry.Option option : entry) {
String suggestText = option.getText().string();
System.out.println(suggestText);
}
}
Please help