Completion suggester in java API

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

Please do not ping people who are not yet involved in your thread. Thanks.

Read this and specifically the "Also be patient" part.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

Please accept my sincere apology for tagging you in this thread.

1 Like
Here is the solution

public String createEventDocument(Event document) throws Exception {
GetIndexRequest request = new GetIndexRequest();
request.indices(INDEX);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
if(!exists){
    createIndexWithMapping();
}
IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.idAsString())
        .source(convertEventDocumentToMap(document));
//create mapping with a complete field
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
return indexResponse.getResult().name();
}

private boolean createIndexWithMapping() throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
    builder.startObject( "properties" );
    {
        builder.startObject( "name_suggest" );
        {
            builder.field( "type", "completion" );
        }
        builder.endObject();
    }
    builder.endObject();
}
builder.endObject();
createIndexRequest.mapping(TYPE,builder);
createIndexRequest.timeout(TimeValue.timeValueMinutes(2));
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
return createIndexResponse.isAcknowledged();

}

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