2 回答

TA貢獻1798條經驗 獲得超3個贊
這是執行相同操作的解決方案。首先使用映射器創建索引并插入數據
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();
}

TA貢獻1784條經驗 獲得超7個贊
你可以做一些事情,比如,
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();
}
添加回答
舉報