目录
例:利用JavaRestClient实现创建、删除索引库,判断索引库是否存在
1、编写mapping映射
2、初始化JavaRestClient
(1)导入elasticsearch的依赖
(2)修改elasticsearch的版本
(3)初始化RestHighLevelClient
3、创建索引库
(1)编写代码
(2)创建常量类,将mapping映射作为常量导入
(3)运行测试
4、删除索引库
5、查询索引库是否存在
例:利用JavaRestClient实现创建、删除索引库,判断索引库是否存在
1、编写mapping映射
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"}}}
}
2、初始化JavaRestClient
(1)导入elasticsearch的依赖
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency>
(2)修改elasticsearch的版本
<properties><java.version>1.8</java.version><elasticsearch.version>7.12.1</elasticsearch.version></properties>
(3)初始化RestHighLevelClient
新建测试类并编写代码
public class HotelIndexTest {private RestHighLevelClient client;@Testvoid testInit(){System.out.println(client);}@BeforeEachvoid setUp(){this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.248.152:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}
}
3、创建索引库
(1)编写代码
public class HotelIndexTest {private RestHighLevelClient client;@Testvoid testInit(){System.out.println(client);}@BeforeEachvoid setUp(){this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.248.152:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}@Testvoid createHotelIndex() throws IOException {
// 创建request对象CreateIndexRequest request = new CreateIndexRequest("hotel");
// 准备请求参数request.source(MAPPING, XContentType.JSON);
// 发送请求client.indices().create(request, RequestOptions.DEFAULT);}}
(2)创建常量类,将mapping映射作为常量导入
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" +"}";
}
(3)运行测试
4、删除索引库
@Testvoid deleteHotelIndex() throws IOException {
// 创建request对象DeleteIndexRequest request = new DeleteIndexRequest("hotel");
// 发送请求client.indices().delete(request, RequestOptions.DEFAULT);}
5、查询索引库是否存在
@Testvoid existsHotelIndex() throws IOException {
// 创建request对象GetIndexRequest request = new GetIndexRequest("hotel");
// 发送请求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists ? "已经存在":"不存在");}