# Java客户端RestClient基础操作.md
## Maven 依赖
```xml
//SpringBoot项目在properties中添加上版本号就能生效
7.12.1
org.elasticsearch.client
elasticsearch-rest-high-level-client
```
## 索引基础操作
```java
/**
* @author ds
* @since 2023/1/3
*/
public class HotelIndexTest {
private RestHighLevelClient client;
//创建客户端连接
@BeforeEach
void setUp(){
client=new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://10.0.0.101:9200")
));
}
//创建索引库
@Test
void createIndexTest() throws IOException {
//创建Request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
//DSL语句
request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
//发送请求
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.err.println("create:"+createIndexResponse.isAcknowledged());
}
//查询索引库(索引库不存在会抛异常)
@Test
void getIndexTest() throws IOException {
//创建GetIndexRequest请求
GetIndexRequest request = new GetIndexRequest("hotel");
//执行请求
GetIndexResponse hotelIndex = client.indices().get(request, RequestOptions.DEFAULT);
//打印相应信息
System.err.println("get:"+JSON.toJSONString(hotelIndex.getMappings()));
}
//删除索引库(索引库不存在会抛异常)
@Test
void deleteIndexTest() throws IOException {
//创建DeleteIndexRequest请求
DeleteIndexRequest deleteRequest = new DeleteIndexRequest("hotel");
//执行请求
AcknowledgedResponse delete = client.indices().delete(deleteRequest, RequestOptions.DEFAULT);
System.err.println("delete:"+delete.isAcknowledged());
}
//判断索引库是否存在
@Test
void existsIndexTest() throws IOException {
//创建GetIndexRequest请求
GetIndexRequest request = new GetIndexRequest("hotel");
//执行请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.err.println(exists?"存在":"不存在");
}
//更新索引(索引库不存在会抛异常)
@Test
void updateIndexTest() throws IOException {
//创建请求
PutMappingRequest request = new PutMappingRequest("hotel");
//构建DSL
request.source(HotelConstants.MAPPING_UP, XContentType.JSON);
//执行请求
AcknowledgedResponse acknowledgedResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
System.err.println(JSON.toJSONString(acknowledgedResponse));
}
//关闭客户端
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
/**
* @author ds
* @since 2023/1/3
*/
public class HotelConstants {
//正常JSON格式见文档末尾
public static final String MAPPING_UP="{\n" +
" \"properties\":{\n" +
" \"name2\":{\n" +
" \"type\":\"keyword\"\n" +
" }\n" +
" }\n" +
"}";
public static final String MAPPING_TEMPLATE="{\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" +
" \"city\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"starName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\"\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" +
"}";
}
```
## DOC基础操作
```java
/**
* @author ds
* @since 2023/1/3
*/
@Slf4j
@SpringBootTest
public class HotelDocTest {
@Autowired
private IHotelService iHotelService;
private RestHighLevelClient client;
//创建连接
@BeforeEach
void setUp(){
client=new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://10.0.0.101:9200")
));
}
//创建doc
@Test
void createDocTest() throws IOException {
//查询mysql中的数据
Hotel hotel = iHotelService.getById(38812);
//转换为doc对象
HotelDoc hotelDoc = new HotelDoc(hotel);
//创建求情
IndexRequest indexRequest = new IndexRequest("hotel").id(hotel.getId().toString());
//设置请求数据
indexRequest.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
//执行请求
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
log.info("create:"+JSON.toJSONString(index.getResult()));
}
//查询doc
@Test
void getDocTest() throws IOException {
//创建请求
GetRequest request = new GetRequest("hotel").id("38812");
//执行请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
log.info("get:"+JSON.toJSONString(response));
}
//删除doc
@Test
void deleteDocTest() throws IOException {
//创建请求
DeleteRequest request = new DeleteRequest("hotel").id("38812");
//执行请求
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
log.info("get:"+JSON.toJSONString(response));
}
//查询doc是否存在
@Test
void existsDocTest() throws IOException {
//创建请求
GetRequest request = new GetRequest("hotel","38812");
//执行请求
boolean exists = client.exists(request, RequestOptions.DEFAULT);
log.info(exists?"存在":"不存在");
}
//更新doc
@Test
void updateDocTest() throws IOException {
//创建请求
UpdateRequest request = new UpdateRequest("hotel","38812");
//设置请求数据
request.doc(
"name","大聪明酒店"
);
//有则更新,没有则新建
request.docAsUpsert(true);
//执行请求
UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
log.info("update:"+JSON.toJSONString(update));
}
//批量测试
@Test
void batchCreateDoc() throws IOException {
// 批量查询酒店数据
List list = iHotelService.list();
//创建求情
BulkRequest bulkRequest = new BulkRequest("hotel");
//构建请求数据
for (Hotel hotel : list) {
//DeleteRequest\GetRequest\UpdateRequest都可添加
IndexRequest indexRequest = new IndexRequest().id(hotel.getId().toString());
indexRequest.source(JSON.toJSONString(new HotelDoc(hotel)),XContentType.JSON);
bulkRequest.add(indexRequest);
}
//执行请求
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.info("bulk:"+JSON.toJSONString(bulk.getItems()));
}
//关闭客户端连接
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
```
HotelConstants-JSON
```apl
#MAPPING_UP
PUT /hotel
{
"properties": {
"name2":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
#MAPPING_TEMPLATE
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",
"copy_to": "all"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
```