package com.example;
import com.alibaba.fastjson.JSON;
import com.example.entry.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.SearchRequestBuilder;
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.get.GetResult;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.event.TransactionalEventListener;
import java.io.IOException;
import java.net.CacheResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@SpringBootTest
@RunWith(SpringRunner.class)
class EsdemoApplicationTests {
//ES7
@Autowired
private RestHighLevelClient restHighLevelClient;
// 创建索引
@Test
void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("yuan");
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.index());
}
@Test
void isExists() throws IOException {
GetIndexRequest request = new GetIndexRequest("yuan1-es1");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
void deletIndex() throws IOException {
DeleteIndexRequest delet = new DeleteIndexRequest("yuan1-es1");
AcknowledgedResponse response = restHighLevelClient.indices().delete(delet, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
//添加文档 类比数据库表
@Test
public void addDocument() throws IOException {
//创建数据User
User user = new User();
user.setId(3);
user.setName("王二");
user.setPassword("1234");
user.setAge(18);
//创建索引
IndexRequest request = new IndexRequest("yuan");
//设置规则
request.id("1");
request.timeout("3m");
//将数据放入索引中
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 isDocumentExits() throws IOException {
GetRequest request = new GetRequest("yuan", "2");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
void getDocument() throws IOException {
GetRequest request = new GetRequest("yuan", "5");
GetResponse documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
}
//更新文档信息
@Test
void updateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("yuan", "2");
request.timeout("3m");
User user = new User();
user.setId(1);
user.setName("lisi");
user.setPassword("1234");
user.setAge(18);
// 修改数据 修改数据上传的类型
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(update.status());
}
//删除文档
@Test
void deletDocument() throws IOException {
DeleteRequest request = new DeleteRequest("yuan", "2");
request.timeout("3m");
DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
isDocumentExits();
}
//批量插入
@Test
void addDocumentMore() throws IOException {
List<User> list = new ArrayList<>();
for (int i = 20; i <100; i++) {
User user = new User((i + 1), "wangping", "1234", 22);
list.add(user);
}
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("20m");
for (int i = 20; i < list.size(); i++) {
bulkRequest.add(
new IndexRequest("yuan")
.id("" + (i + 1))
.source(JSON.toJSONString(list.get(i)), XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
// ok 表示出入过程中没有失败数据 插入成功
System.out.println(bulk.status());
}
// 查询 重点
@Test
void searchDocument() throws IOException {
//查询索引
SearchRequest searchRequest = new SearchRequest("yuan");
//查询构造器 sourceBuilder
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 使用精确查询
TermQueryBuilder termQueryBuilder = new TermQueryBuilder("name", "wangping");
//关键字高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("name");
//关闭多个高亮显示
highlightBuilder.requireFieldMatch(false);
highlightBuilder.preTags("<span style='color: red'>");
highlightBuilder.postTags("</span>");
sourceBuilder.highlighter(highlightBuilder);
sourceBuilder.query(termQueryBuilder);
//sourceBuilder 关联 termQueryBuilder 查询
sourceBuilder.from(0).size(99);
//searchRequest 关联 sourceBuilder
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//获取查询的数据
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
for (SearchHit documentFields : hits.getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
}
ELK API
最新推荐文章于 2023-02-26 21:39:18 发布