java引入es使用
引入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
初始化对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.150.101:9200")
));
索引CRUD操作
public class ElasticIndexTest {
private RestHighLevelClient client;
@BeforeEach
void setUp(){
client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.88.95",9200,"http")));
}
@AfterEach
void tearDown() throws IOException {
if(client!=null)
client.close();
}
@Test
void create() throws IOException {
CreateIndexRequest test = new CreateIndexRequest("hmall");
test.source(MAPPING_TEMPLATE, XContentType.JSON);
client.indices().create(test, RequestOptions.DEFAULT);
}
@Test
void get() throws IOException {
GetIndexRequest test = new GetIndexRequest("hmall");
client.indices().get(test, RequestOptions.DEFAULT);
}
@Test
void delete() throws IOException {
DeleteIndexRequest test = new DeleteIndexRequest("hmall");
client.indices().delete(test, RequestOptions.DEFAULT);
}
private final static String MAPPING_TEMPLATE="{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_smart\"\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"image\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"category\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"sold\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"comment_count\":{\n" +
" \"type\": \"integer\",\n" +
" \"index\": false\n" +
" },\n" +
" \"isAD\":{\n" +
" \"type\": \"boolean\" \n" +
" },\n" +
" \"update_time\":{\n" +
" \"type\": \"date\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
文档CRUD操作
public class ElasticDocumentTest {
@Autowired
private IItemService service;
private RestHighLevelClient client;
@BeforeEach
void setUp(){
client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.88.95",9200,"http")));
}
@AfterEach
void tearDown() throws IOException {
if(client!=null)
client.close();
}
/* 新增
* 全量修改:写入重复ID即视为全量修改
* */
@Test
void addAndUpdateDoc() throws IOException {
Item byId = service.getById("317578");
ItemDoc itemDoc = BeanUtil.copyProperties(byId, ItemDoc.class);
IndexRequest request=new IndexRequest("hmall").id(itemDoc.getId());
request.source(JSONUtil.toJsonStr(itemDoc),XContentType.JSON);
client.index(request,RequestOptions.DEFAULT);
}
/*局部修改*/
@Test
void updateDoc() throws IOException {
UpdateRequest request=new UpdateRequest("hmall","317578");
request.doc(
"price","25600"
);
client.update(request,RequestOptions.DEFAULT);
}
/*查询*/
@Test
void getDoc() throws IOException {
GetRequest getRequest=new GetRequest("hmall","317578");
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
String json = response.getSourceAsString();
ItemDoc itemDoc = JSONUtil.toBean(json, ItemDoc.class);
}
/*删除*/
@Test
void deleteDoc() throws IOException {
DeleteRequest deleteRequest=new DeleteRequest("hmall","317578");
client.delete(deleteRequest, RequestOptions.DEFAULT);
}
}
文档批处理操作:
/*批处理*/
@Test
void bulkDoc() throws IOException {
int pageSize=500;
int pageNum=1;
while(true){
Page<Item> page = service.lambdaQuery()
.eq(Item::getStatus, 1)
.page(Page.of(pageNum, pageSize));
List<Item>items=page.getRecords();
if(items==null||items.isEmpty()){
return;
}
BulkRequest request=new BulkRequest(); for (Item item : items) {
request.add(new IndexRequest("hmall")
.id(item.getId().toString())
.source(JSONUtil.toJsonStr(BeanUtil.copyProperties(item, ItemDoc.class)),XContentType.JSON)
);
}
client.bulk(request,RequestOptions.DEFAULT);
pageNum++;
} }
java引入es使用的更多相关文章
- Java操作ES
一.ES基本概念 1. 节点:Elastic是一个分布式数据库,每个数据库实例是一个节点Node,一台服务器上可以有多个Node,可以多台服务器协同工作 2. 集群:一组节点Node构成一个集群Clu ...
- Elasticsearch笔记五之java操作es
Java操作es集群步骤1:配置集群对象信息:2:创建客户端:3:查看集群信息 1:集群名称 默认集群名为elasticsearch,如果集群名称和指定的不一致则在使用节点资源时会报错. 2:嗅探功能 ...
- Java引入的一些新特性
Java引入的一些新特性 Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程, ...
- java 引入自定义字体font后出现的硬盘吃光的问题
有个需求要用美术字体在图片上写字 用自定义的文字有两个方法: 1. Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, InputStream ...
- 基于java的ES开发
3.1 环境配置 Jdk 1.8及以上 Elasticsearch.client 5.5.2(与服务器版本一致) Log4j 2.7及以下 maven工程必要的jar包依赖 <project x ...
- JAVA 引入 junit工具框架
我遇到的麻烦 : 开始直接按照视频上的来做,直接也是引入的他上面的jar ,但是我只引入了一个,就是上面的junit-4.4.jar,然后就会报错,会出现,空指针的错误, 后面我又按照网上的教程 这里 ...
- 4、2 java 使用es
1.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- java——引入第三方jar包
第一步:项目->New->Folder:创建一个文件夹: 第二步:把要引入的jar包粘贴到新建的文件夹中: 第三步:选中引入的jar包->Build Path->Add to ...
- Java创建ES索引实现
1.pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- maven-腾讯SDK(QQ)接口java引入pom配置
maven的pom.xml配置 <dependency> <groupId>net.gplatform</groupId> <artifactId>Sd ...
随机推荐
- 批量处理图片(resize;grayscale)以及重命名
批量处理图片(resize:grayscale)以及重命名 做深度学习,有大量图片需要做为训练集,为方便批量resize,rename,灰度化,制作python脚本. 先纪录一些函数: resize ...
- 国产大模型参加高考,同写2024年高考作文,及格分(通义千问、Kimi、智谱清言、Gemini Advanced、Claude-3-Sonnet、GPT-4o)
大家好,我是章北海 今天高考,上午的语文结束,市面上又要来一场大模型参考的文章了. 我也凑凑热闹,让通义千问.Kimi.智谱清言一起来写一下高考作文. 公平起见,不加任何其他prompt,直接把题目甩 ...
- SQL server查看触发器是否被禁用
1 select a.name as 触发器名,b.name as 表名, 2 case a.is_disabled when 0 then '启用' when 1 then '禁用' else '未 ...
- LidarView工程搭建指南
前言 笔者做过一段时间的车载LiDAR开发,对LidarView开源项目进行过深度定制,摸索了一套LidarView软件的开发和调试方法 1 软件安装 1.1 安装准备 以Windows10系统平台为 ...
- INFINI Labs 产品更新 | 修复 Easysearch 跨集群复制索引同步问题,Gateway 内存异常增长等问题
INFINI Labs 产品又更新啦~,本次更新主要对 Easysearch.Gateway.Console.Agent 等产品功能进行优化和相关 Bug 修复,解决了内存异常增长等问题,以下是详细说 ...
- PowerShell 遇到 .ps1,因为在此系统上禁止运行脚本
PowerShell 遇到 .ps1,因为在此系统上禁止运行脚本 解决方法: 以管理员身份打开PowerShell: 查看当前的执行策略: Get-ExecutionPolicy * `Restric ...
- NumPy 简单算术:加减乘除及其他运算
简单算术 你可以直接在 NumPy 数组之间使用算术运算符 + - * /,但本节讨论了一个扩展,其中我们有函数可以接受任何类似数组的对象,如列表.元组等,并根据条件执行算术运算. 条件算术:意味着我 ...
- 《Android开发卷——HTTP网络通信,HTTP网络连接》
为了访问互联网,需要设置应用程序获取"androd.permission.INTERNET"权限的许可. 一.使用Apache接口(org.apache.http)并实现网络连接的 ...
- CloseableHttpClient设置超时时间demo 未设置默认是2分钟
# CloseableHttpClient设置超时时间demo 未设置默认是2分钟 import org.apache.http.HttpHeaders; import org.apache.http ...
- Java与React轻松导出Excel/PDF数据
前言 在B/S架构中,服务端导出是一种高效的方式.它将导出的逻辑放在服务端,前端仅需发起请求即可.通过在服务端完成导出后,前端再下载文件完成整个导出过程.服务端导出具有许多优点,如数据安全.适用于大规 ...