I am new to elastic search and trying to integrate an autocomplete feature for an app by following the article https://www.elastic.co/blog/you-complete-me.
I have followed the below approach to do the same.
Event class
public class Event {
private Long eventId;
private Long catalogId;
private Long orgId;
private String orgName;
private String catalogName;
private String name;
private String eventStatus;
.....
}
An objectmapper is used to convert the event object to json string. Here is the code to insert the document
public String createEventDocument(Event document) throws Exception {
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();
}
Convert code
private Map<String, Object> convertEventDocumentToMap(Event evt) {
return objectMapper.convertValue(evt, Map.class);
}
I would like to create an index, and setup the completion suggester for the name_suggest field. How can I achieve the same ?
Any help is appreciated
You can do some thing like,
public static void main( String[] args )
{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost( "192.168.1.245", 9200, "http" ) ) );
try
{
createIndex( client );
updateIndexMapping( client );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
private static void createIndex( RestHighLevelClient client ) throws IOException
{
//1. create index
Map<String, String> map = new HashMap<String, String>();
map.put( "eventId", "eventId" );
map.put( "catalogId", "catalogId" );
map.put( "orgId", "orgId" );
map.put( "orgName", "orgName" );
map.put( "catalogName", "catalogName" );
map.put( "name", "name" );
map.put( "eventStatus", "eventStatus" );
IndexRequest indexRequest = new IndexRequest( "event", "event", "123" )
.source( map );
IndexResponse indexResponse = client.index( indexRequest, RequestOptions.DEFAULT );
indexResponse.getResult().name();
}
private static void updateIndexMapping( RestHighLevelClient client ) throws IOException
{
//2. update index mapping to set the filed 'name_suggest' into type 'completion'
PutMappingRequest request = new PutMappingRequest( "event" );
request.type( "event" );
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject( "properties" );
{
builder.startObject( "name_suggest" );
{
builder.field( "type", "completion" );
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
request.source( builder );
request.timeout( TimeValue.timeValueMinutes( 1 ) );
AcknowledgedResponse acknowledgedResponse = client.indices().putMapping( request, RequestOptions.DEFAULT );
//check if the request is sucess
boolean acknowledged = acknowledgedResponse.isAcknowledged();
}
Here is the solution to do the same. Create index with mappers first and insert the data
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();
}