1、索引的增加,查看,删除API
package com.quan.esapi.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
)
);
return client;
}
}
package com.quan.esapi;
import com.quan.esapi.config.ElasticSearchConfig;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class EsApiApplicationTests {
@Autowired
RestHighLevelClient restHighLevelClient;
@Test
void contextLoads() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("quan");
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
}
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("quan");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("===========" + exists);
}
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("quan");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println("===========" + delete.isAcknowledged());
}
2、文档的增加,查看,删除API
@Test
void testAddDocument() throws IOException{
//创建对象
User user = new User("luzequan","xinyi");
//创建请求
IndexRequest request = new IndexRequest("ze");
//规则
request.id("1");
request.timeout(TimeValue.timeValueSeconds(10));
request.timeout("10s");
//将我们的数据放入到请求 json
IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求,获取响应结果。
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
@Test
void testIsExistsDocument() throws IOException{
GetRequest getRequest = new GetRequest("ze","1");
//不获取source返回的上下文了
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = restHighLevelClient.exists(getRequest,RequestOptions.DEFAULT);
System.out.println("==============" + exists);
}
@Test
void testGetDocument() throws IOException{
GetRequest getRequest = new GetRequest("ze","1");
//不获取source返回的上下文了
GetResponse getResponse = restHighLevelClient.get(getRequest,RequestOptions.DEFAULT);
System.out.println("==============" + getResponse.getSourceAsString());
System.out.println("==============" + getResponse);
}
@Test
void testUpdateDocument() throws IOException{
UpdateRequest updateRequest = new UpdateRequest("ze","1");
updateRequest.timeout("10s");
User user = new User("luzequan","maoming");
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
System.out.println("==============" + updateResponse.status());
}
@Test
void testDeleteDocument() throws IOException{
DeleteRequest deleteRequest = new DeleteRequest("ze","1");
deleteRequest.timeout("10s");
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
System.out.println("==============" + deleteResponse.status());
}
3、特殊的API
@Test
void testBulkDocument() throws IOException{
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> list = new ArrayList<>();
list.add(new User("luzequan","xinyi1"));
list.add(new User("luzequan","xinyi2"));
list.add(new User("luzequan","xinyi3"));
list.add(new User("luzequan","xinyi4"));
list.add(new User("luzequan","xinyi5"));
for(int i = 0; i < list.size(); i++){
bulkRequest.add(
new IndexRequest("lu")
.id("" + (i + 1))
.source(JSON.toJSONString(list.get(i)),XContentType.JSON));
}
BulkResponse bulkItemResponses = restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
System.out.println(bulkItemResponses);
}
在这里插入代码片