1、寫在前面
注意:導(dǎo)入的包區(qū)別,不同的包創(chuàng)建索引的方式不同。博主親身實(shí)踐,具體體現(xiàn)在createIndexRequest.mapping()里面。讀者可自行試驗(yàn)。
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
?由此可以猜想一下:
import org.elasticsearch.client.indices.*;
import org.elasticsearch.action.admin.indices.*;
可以看到上述兩種方式導(dǎo)入的包的子類名是相同的,但是具體對(duì)索引的操作方式可能是不同的。具體的區(qū)別博主暫時(shí)還不清楚,后續(xù)若有進(jìn)展再在此處更新。
2、同步創(chuàng)建索引
例如要實(shí)現(xiàn)如下索引的創(chuàng)建文章來源:http://www.zghlxwxcb.cn/news/detail-588253.html
PUT /my_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"description": {
"type": "text"
},
"price": {
"type": "long"
},
"pic": {
"type": "text",
"index": false
}
}
},
"aliases": {
"default_index": {}
}
}
代碼如下(3種方式):文章來源地址http://www.zghlxwxcb.cn/news/detail-588253.html
//創(chuàng)建索引
@Test
public void testCreateIndex() throws IOException {
//創(chuàng)建索引對(duì)象
CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book");
//設(shè)置參數(shù)
createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0"));
//指定映射1
createIndexRequest.mapping(" {\n" +
" \t\"properties\": {\n" +
" \"name\":{\n" +
" \"type\":\"keyword\"\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"price\":{\n" +
" \"type\":\"long\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\":\"text\",\n" +
" \"index\":false\n" +
" }\n" +
" \t}\n" +
"}", XContentType.JSON);
//指定映射2
// Map<String, Object> message = new HashMap<>();
// message.put("type", "text");
// Map<String, Object> properties = new HashMap<>();
// properties.put("message", message);
// Map<String, Object> mapping = new HashMap<>();
// mapping.put("properties", properties);
// createIndexRequest.mapping(mapping);
//指定映射3
// XContentBuilder builder = XContentFactory.jsonBuilder();
// builder.startObject();
// {
// builder.startObject("properties");
// {
// builder.startObject("message");
// {
// builder.field("type", "text");
// }
// builder.endObject();
// }
// builder.endObject();
// }
// builder.endObject();
// createIndexRequest.mapping(builder);
//設(shè)置別名
createIndexRequest.alias(new Alias("itheima_index_new"));
// 額外參數(shù)
//設(shè)置超時(shí)時(shí)間
createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2));
//設(shè)置主節(jié)點(diǎn)超時(shí)時(shí)間
createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes(1));
//在創(chuàng)建索引API返回響應(yīng)之前等待的活動(dòng)分片副本的數(shù)量,以int形式表示
createIndexRequest.waitForActiveShards(ActiveShardCount.from(2));
createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT);
//操作索引的客戶端
IndicesClient indices = client.indices();
//執(zhí)行創(chuàng)建索引庫(kù)
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
//得到響應(yīng)(全部)
boolean acknowledged = createIndexResponse.isAcknowledged();
//得到響應(yīng) 指示是否在超時(shí)前為索引中的每個(gè)分片啟動(dòng)了所需數(shù)量的碎片副本
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
System.out.println("acknowledged:" + acknowledged);
System.out.println("shardsAcknowledged:" + shardsAcknowledged);
}
3、異步創(chuàng)建索引
//創(chuàng)建索引異步方式
@Test
public void testCreateIndexAsync() throws IOException {
//創(chuàng)建索引對(duì)象
CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book");
//設(shè)置參數(shù)
createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0"));
//指定映射1 與創(chuàng)建文檔做類別
createIndexRequest.mapping(" {\n" +
" \t\"properties\": {\n" +
" \"name\":{\n" +
" \"type\":\"keyword\"\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"price\":{\n" +
" \"type\":\"long\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\":\"text\",\n" +
" \"index\":false\n" +
" }\n" +
" \t}\n" +
"}", XContentType.JSON);
//指定映射2
// Map<String, Object> message = new HashMap<>();
// message.put("type", "text");
// Map<String, Object> properties = new HashMap<>();
// properties.put("message", message);
// Map<String, Object> mapping = new HashMap<>();
// mapping.put("properties", properties);
// createIndexRequest.mapping(mapping);
//指定映射3
// XContentBuilder builder = XContentFactory.jsonBuilder();
// builder.startObject();
// {
// builder.startObject("properties");
// {
// builder.startObject("message");
// {
// builder.field("type", "text");
// }
// builder.endObject();
// }
// builder.endObject();
// }
// builder.endObject();
// createIndexRequest.mapping(builder);
//設(shè)置別名
createIndexRequest.alias(new Alias("itheima_index_new"));
// 額外參數(shù)
//設(shè)置超時(shí)時(shí)間
createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2));
//設(shè)置主節(jié)點(diǎn)超時(shí)時(shí)間
createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes(1));
//在創(chuàng)建索引API返回響應(yīng)之前等待的活動(dòng)分片副本的數(shù)量,以int形式表示
createIndexRequest.waitForActiveShards(ActiveShardCount.from(2));
createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT);
//操作索引的客戶端
IndicesClient indices = client.indices();
//執(zhí)行創(chuàng)建索引庫(kù)
// CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
@Override
public void onResponse(CreateIndexResponse createIndexResponse) {
//得到響應(yīng)(全部)
boolean acknowledged = createIndexResponse.isAcknowledged();
//得到響應(yīng) 指示是否在超時(shí)前為索引中的每個(gè)分片啟動(dòng)了所需數(shù)量的碎片副本
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
System.out.println("acknowledged:" + acknowledged);
System.out.println("shardsAcknowledged:" + shardsAcknowledged);
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
};
client.indices().createAsync(createIndexRequest, RequestOptions.DEFAULT, listener);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
4、同步刪除索引
// 刪除索引
@Test
public void testDeleteIndex() throws IOException {
//創(chuàng)建刪除索引請(qǐng)求
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("itheima_book");
// 執(zhí)行
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//得到相應(yīng)
boolean acknowledged = delete.isAcknowledged();
System.out.println("acknowledged:" + acknowledged);
}
5、異步刪除索引
// 刪除索引異步操作
@Test
public void testDeleteIndexAsync() throws IOException {
//創(chuàng)建刪除索引請(qǐng)求
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("itheima_book");
// 執(zhí)行
ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
//得到相應(yīng)
boolean acknowledged = acknowledgedResponse.isAcknowledged();
System.out.println("acknowledged:" + acknowledged);
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
};
// AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
}
6、索引是否存在
//index exist api
@Test
public void testExistIndex() throws IOException {
GetIndexRequest request=new GetIndexRequest("itheima_book");
//參數(shù)
request.local(false);//從主節(jié)點(diǎn)返回本地索引信息狀態(tài)
request.humanReadable(true);//以適合人類的格式返回
request.includeDefaults(false);//是否返回每個(gè)索引的所有默認(rèn)配置
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("exists:"+exists);
}
7、關(guān)閉索引
//關(guān)閉索引
@Test
public void testCloseIndex() throws IOException {
CloseIndexRequest request=new CloseIndexRequest("itheima_book");
AcknowledgedResponse close = client.indices().close(request, RequestOptions.DEFAULT);
boolean acknowledged = close.isAcknowledged();
System.out.println("acknowledged:"+acknowledged);
}
8、開啟索引
//開啟索引
@Test
public void testOpenIndex() throws IOException {
OpenIndexRequest request=new OpenIndexRequest("itheima_book");
OpenIndexResponse open = client.indices().open(request, RequestOptions.DEFAULT);
boolean acknowledged = open.isAcknowledged();
System.out.println("acknowledged:"+acknowledged);
}
到了這里,關(guān)于ElasticSearch7.3學(xué)習(xí)(十六)----RestHighLevelClient Java api實(shí)現(xiàn)索引的創(chuàng)建、刪除、是否存在、關(guān)閉、開啟的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!