package com.elastic;
import com.alibaba.fastjson.JSON;
import com.elastic.pojo.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.webservices.client.WebServiceClientTest;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class ElasticApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
//创建索引
@Test
void contextLoads() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest("springboot-es");
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
//获取索引
@Test
void getindex() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest("springboot-es");
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
//删除索引
@Test
void deleteindex() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("springboot-es");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
//添加文档
@Test
void adddoc() throws IOException {
//创建对象
User elasticsearch = new User("elasticsearch", "20");
//创建请求
IndexRequest doc_index = new IndexRequest("doc_index");
//规则 put /doc_index/_doc/1
doc_index.id("1");
doc_index.timeout("1s");
//降数据放入请求 json
doc_index.source(JSON.toJSONString(elasticsearch), XContentType.JSON);
//发送请求获得响应结果
IndexResponse indexResponse = restHighLevelClient.index(doc_index, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
//获取文档
@Test
void getisexit() throws IOException {
GetRequest doc_index = new GetRequest("doc_index", "1");
doc_index.fetchSourceContext(new FetchSourceContext(false));
doc_index.storedFields("_none_");
boolean exists = restHighLevelClient.exists(doc_index, RequestOptions.DEFAULT);
System.out.println(exists);
}
//获取文档的信息
@Test
void getindexmes() throws IOException {
GetRequest doc_index = new GetRequest("doc_index", "1");
GetResponse documentFields = restHighLevelClient.get(doc_index, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
System.out.println(documentFields);
}
//更新文档信息
@Test
void updateindex() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("doc_index", "1");
updateRequest.timeout("1s");
User elastic = new User("elastic", "28");
UpdateRequest doc = updateRequest.doc(JSON.toJSONString(elastic), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(doc, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
//删除文档记录
@Test
void deleteindexRequest() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("doc_index", "1");
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
}
//批量插入文档内容
@Test
void blukresquest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("1s");
ArrayList<User> users = new ArrayList<>();
users.add(new User("haha","25"));
users.add(new User("uy","26"));
users.add(new User("hg","24"));
users.add(new User("vb","27"));
users.add(new User("re","28"));
users.add(new User("sf","29"));
users.add(new User("qw","21"));
for (int i = 0; i <users.size() ; i++) {
bulkRequest.add(
new IndexRequest("doc_index")
.id(""+(i+1))
.source(JSON.toJSONString(users.get(i)),XContentType.JSON));
}
BulkResponse bulkItemResponses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkItemResponses.hasFailures());//是否失败 false代表成功
}
//查询文档内容
@Test
void searchindex() throws IOException {
SearchRequest searchRequest = new SearchRequest("doc_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//精确查找
//TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "haha");
//searchSourceBuilder.query(termQueryBuilder);
//全部查找
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(matchAllQueryBuilder);
searchSourceBuilder.from();
searchSourceBuilder.size();
SearchRequest source = searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
for (SearchHit hit : searchResponse.getHits().getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
}
配置类
package com.elastic.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("192.168.80.122" , 9200 , "http" )));
return client;
}
}