package com.mao;
import co.elastic.clients.elasticsearch.core.search.Highlight;
import com.alibaba.fastjson.JSON;
import com.mao.pojo.User;
import org.apache.lucene.util.QueryBuilder;
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.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.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.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.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import static com.mao.utils.EsConst.ES_INDEX;
/**
* @author mao
* @createTime 2022/8/22 0022 19:59
* @Description: 使用@RunWith() 可以避免在测试类中使用自动注入(注入的对象为空)
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class JdTests {
// @Autowired
// @Qualifier("restHighLevelClient")
// private RestHighLevelClient restHighLevelClient;
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 创建一个索引的请求,将request,执行创建的请求,获取相应结果
* 关闭客户端
* @throws IOException
*/
@Test
public void testCreateIndex() throws IOException {
// 1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest(ES_INDEX);
// 2.执行创建请求,请求后获得响应createIndexResponse
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
System.out.println(response.isAcknowledged());// 查看是否创建成功
System.out.println(response);// 查看返回对象
restHighLevelClient.close();
}
/**
* 判断索引是否存在,获取索引的名字,客户端判断请求是否存在,返回结果
* 客户端关闭
* @throws IOException
*/
@Test
public void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest(ES_INDEX);
boolean response = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(response);
restHighLevelClient.close();
}
/**
* 删除索引库,获取删除索引的请求,客户端执行删除索引请求,返回响应
* 客户端关闭
* @throws IOException
*/
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(ES_INDEX);
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
restHighLevelClient.close();
}
/**
* 向 ES_INDEX 索引中添加文档信息,文档默认保存在_doc 下面
* 新建实体类,初始化实体类信息,获取到索引请求,设置索引的 id,请求延迟 1s,初始化好的实体类对象转化成JSON字符串,加入到请求源中
* 客户端的索引方法执行请求返回响应结果,对于结果可以打印出字符串,也可以查看响应的状态
* @throws IOException
*/
@Test
public void testAddDoc() throws IOException {
User user = new User("熊11", 18);
IndexRequest request = new IndexRequest(ES_INDEX);
request.id("11");
request.timeout("1s");
// request.timeout(TimeValue.timeValueSeconds(1));
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());
}
/**
* 判断索引是否存在,Get获取索引id,返回请求
* @throws IOException
*/
@Test
public void testDocIsExists() throws IOException {
GetRequest request = new GetRequest(ES_INDEX, "2");
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
public void getDocInfo() throws IOException {
GetRequest request = new GetRequest(ES_INDEX, "1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println(response);
}
@Test
public void updateDocInfo() throws IOException {
UpdateRequest updateRequest = new UpdateRequest(ES_INDEX, "1");
updateRequest.timeout(TimeValue.timeValueSeconds(1));
User user = new User("熊妹妹", 18);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}
@Test
public void deleteDoc() throws IOException{
DeleteRequest request = new DeleteRequest(ES_INDEX, "1");
request.timeout(TimeValue.timeValueSeconds(1));
DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(delete.status());
}
/**
* 批量加入文档信息
* @throws IOException
*/
@Test
public void addInfo() throws IOException{
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout(TimeValue.timeValueSeconds(1));
ArrayList<User> users = new ArrayList<>();
Random random = new Random();
for (int i = 1; i < 11; i++) {
users.add(new User("熊"+i,random.nextInt(20)+1));
}
for (int i = 0; i < users.size(); i++) {
bulkRequest.add(
new IndexRequest(ES_INDEX)
.id(""+(i+1))
.source(JSON.toJSONString(users.get(i)),XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.hasFailures());
}
// 查询
@Test
public void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest(ES_INDEX);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// HighlightBuilder highlightBuilder = new HighlightBuilder();
// sour
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Mybatis学习项目源码,共392个文件,采用Java、HTML、JavaScript和CSS等语言开发,涉及多种文件类型如Java源代码、XML配置文件、HTML文档、JavaScript脚本、Properties配置文件、CSS样式表、LST文件、YAML配置文件、Git忽略文件、JPG图片等。该项目是一个基于Java的Mybatis学习设计,旨在提供一个高效、稳定的Mybatis学习解决方案。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的Mybatis学习设计源码 (397个子文件)
bootstrap.min.css 141KB
bootstrap.min.css 141KB
style.css 10KB
style.css 10KB
dashboard.css 2KB
dashboard.css 2KB
signin.css 906B
signin.css 906B
qinstyle.css 125B
qinstyle.css 125B
.gitignore 278B
.gitignore 180B
.gitignore 180B
index.html 6KB
index.html 6KB
index.html 5KB
index.html 5KB
dashboard.html 4KB
dashboard.html 4KB
update.html 4KB
update.html 4KB
add.html 4KB
add.html 4KB
list.html 4KB
list.html 4KB
query.html 3KB
query.html 3KB
commons.html 3KB
commons.html 3KB
404.html 3KB
404.html 3KB
login.html 2KB
login.html 2KB
test.html 2KB
test.html 2KB
index.html 2KB
index.html 2KB
demo-herf.html 1KB
2.html 756B
3.html 756B
1.html 756B
2.html 756B
1.html 756B
3.html 756B
2.html 756B
3.html 756B
1.html 756B
2.html 756B
3.html 756B
1.html 756B
3.html 756B
2.html 756B
1.html 756B
2.html 756B
3.html 756B
1.html 756B
index.html 537B
index.html 537B
login.html 399B
login.html 399B
demo-img.html 290B
update.html 144B
update.html 144B
add.html 141B
add.html 141B
favicon.ico 35KB
favicon.ico 35KB
JdTests.java 9KB
DateUtils.java 6KB
ContentServiceImpl.java 5KB
UserDaoTest.java 5KB
MyUtil.java 4KB
BuilderPattern.java 3KB
LockTest.java 3KB
CommonResult.java 3KB
AbstractFactoryPattern.java 3KB
UserDaoTest.java 3KB
UserDaoTest.java 3KB
AdaptorPattern.java 3KB
NewPac.java 3KB
NewPacSort.java 3KB
EmployeeController.java 3KB
SyncQueue.java 3KB
ShiroConfig.java 2KB
PrototypePattern.java 2KB
Common.java 2KB
lock8.java 2KB
Pac.java 2KB
WebSecurityConfig.java 2KB
Demo.java 2KB
Auxiliary.java 2KB
callable01.java 2KB
IndexController.java 2KB
Users.java 2KB
LazyManSingle.java 2KB
RWLock.java 2KB
UserRealm.java 2KB
Appointment.java 2KB
Test.java 2KB
HtmlParseUtil.java 2KB
共 397 条
- 1
- 2
- 3
- 4
资源评论
沐知全栈开发
- 粉丝: 5799
- 资源: 5225
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算器react的简单写法
- 建筑工地安全检测9-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- 【含源码、数据库、毕业论文及答辩PPT展示】Java毕设微信小程序项目:基于SSM框架的微信小程序购物商城系统,界面优美,推荐!
- 岁的女孩比局迪士尼覅就
- 车载对话问答系统利用大型语言模型实现安全高效的驾驶辅助问答
- redis api翻译.zip
- 将CMS组合内存系统集成到存储与数据密集型任务
- Redis 4.x Cookbook 的代码文件.zip
- springboot快讯新闻信息发布系统74379(数据库+源码)
- redis012345.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功