ElasticSearch Client文档官网
原生依赖
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
dependency>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
dependency>
//1.找到对象 2.放入spring容器中
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")));
return client;
}
}
@SpringBootTest
class ElasticsearchApi01ApplicationTests {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
//测试索引创建
@Test
void contextLoads() throws IOException {
//1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest("wjm_index");
//2.执行创建请求 IndecesClient 请求后获得响应
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
}
//测试获取索引
@Test
void contextLoads2() throws IOException {
//1.获取索引请求
GetIndexRequest getIndexRequest = new GetIndexRequest("wjm_index");
//2.client 索引操作 获取索引
GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(getIndexResponse);
}
//测试删除索引
@Test
void contextLoads3() throws IOException {
//1.获取索引请求
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("wjm_index");
//2.client 索引操作 获取索引
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
//创建文档
@Test
void contextLoadsDocumentsOne() throws IOException {
//创建对象
GamePlayer player = new GamePlayer(1,"一只小小狗",18,"79892965","79892965");
//创建请求
IndexRequest request = new IndexRequest("wjm_index");
//规则 put /wjm_index/_doc/1;
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
//source 将我们的数据放入请求json
request.source(JSON.toJSONString(player), XContentType.JSON);
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
System.out.println(index.status());
}
@Test
void contextLoadsDocumentsTwo() throws IOException {
//获取文档请求
GetRequest wjm_index = new GetRequest("wjm_index", "1");
//不获取返回的 文档 的上下文
//wjm_index.fetchSourceContext(new FetchSourceContext(false));
//wjm_index.storedFields("_none_");
//客户端直接获取文档
GetResponse documentFields = client.get(wjm_index, RequestOptions.DEFAULT);
boolean exists = client.exists(wjm_index, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
System.out.println(documentFields.toString());
System.out.println(exists);
}
//更新文档
@Test
void contextLoadsDocumentsT() throws IOException {
//获取文档请求
UpdateRequest updateRequest = new UpdateRequest("wjm_index", "1");
updateRequest.timeout("1s");
GamePlayer player = new GamePlayer(1,"一只小小狗",20,"123","123");
updateRequest.doc(JSON.toJSONString(player),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}
//删除文档
@Test
void contextLoadsDocumentsD() throws IOException {
//获取文档请求
DeleteRequest deleteRequest = new DeleteRequest("wjm_index", "1");
deleteRequest.timeout("1s");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.status());
}
//批量查询
@Test
void contextLoadsDocumentsQP() throws IOException, InterruptedException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<GamePlayer> gamePlayers = new ArrayList<>();
for (int i = 0;i<30;i++){
gamePlayers.add(new GamePlayer(i,"一只小小狗",i,""+i,""+i));
}
TimeUnit.SECONDS.sleep(1);
//批处理请求
for (int i=0;i<gamePlayers.size();i++){
bulkRequest.add(
new IndexRequest("wjm_index")
.id(""+(i+1))
.source(JSON.toJSONString(gamePlayers.get(i)),XContentType.JSON)
);
}
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.status());
}
//批量查询
@Test
void contextLoadsDocumentsQP3() throws IOException, InterruptedException {
SearchRequest searchRequest = new SearchRequest("wjm_index");
//构建搜索 精确匹配
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id", "1");
searchSourceBuilder.query(termQueryBuilder);
searchSourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
//构建搜索放入请求
searchRequest.source(searchSourceBuilder);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(search.getHits()));
System.out.println("===============");
for (SearchHit documentFields:search.getHits().getHits()
) {
System.out.println(documentFields.getSourceAsMap());
}
}