1、写在前面
注意:导入的包区别,不同的包创建索引的方式不同。博主亲身实践,具体体现在createIndexRequest.mapping()里面。读者可自行试验。
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.*;
可以看到上述两种方式导入的包的子类名是相同的,但是具体对索引的操作方式可能是不同的。具体的区别博主暂时还不清楚,后续若有进展再在此处更新。
2、同步创建索引
例如要实现如下索引的创建
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种方式):
//创建索引
@Test
public void testCreateIndex() throws IOException {
//创建索引对象
CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book");
//设置参数
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


1723

被折叠的 条评论
为什么被折叠?



