目录
例:利用JavaRestClient实现创建、删除索引库,判断索引库是否存在
1、编写mapping映射
2、初始化JavaRestClient
(1)导入elasticsearch的依赖
(2)修改elasticsearch的版本
(3)初始化RestHighLevelClient
3、创建索引库
(1)编写代码
(2)创建常量类,将mapping映射作为常量导入
(3)运行测试
4、删除索引库
5、查询索引库是否存在
PUT /hotel
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
org.elasticsearch.client
elasticsearch-rest-high-level-client
1.8
7.12.1
新建测试类并编写代码
public class HotelIndexTest {
private RestHighLevelClient client;
@Test
void testInit(){
System.out.println(client);
}
@BeforeEach
void setUp(){
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.248.152:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
public class HotelIndexTest {
private RestHighLevelClient client;
@Test
void testInit(){
System.out.println(client);
}
@BeforeEach
void setUp(){
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.248.152:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
@Test
void createHotelIndex() throws IOException {
// 创建request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 准备请求参数
request.source(MAPPING, XContentType.JSON);
// 发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
}
public class HotelConstans {
public static final String MAPPING =
"{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"address\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"score\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" \n" +
" },\n" +
" \"city\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"starName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"location\":{\n" +
" \"type\": \"geo_point\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"all\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
@Test
void deleteHotelIndex() throws IOException {
// 创建request对象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
// 发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
@Test
void existsHotelIndex() throws IOException {
// 创建request对象
GetIndexRequest request = new GetIndexRequest("hotel");
// 发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? "已经存在":"不存在");
}