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 ...
随机推荐
- python+k8s——基础练习
列表 core_api = client.CoreV1Api() # 管理核心资源(Pod, Service, ConfigMap 等) apps_api = client.AppsV1Api() # ...
- Python结合文件名称将多个文件复制到不同路径下
本文介绍基于Python语言,针对一个文件夹下的大量栅格遥感影像文件,基于其各自的文件名,分别创建指定名称的新文件夹,并将对应的栅格遥感影像文件复制到不同的新文件夹下的方法. 首先,我们来看一 ...
- c# webApi返回Excel数据流 || 使用Excel数据流的方式下载Excel
背景: 在前端无法生成特殊的excel表格,或操作复杂的时候会通过后台进行生成excel.但是服务器的资源也非常宝贵,所以通过数据流的方式就可以实现:不在服务器存储的情况下,使前端成功下载excel文 ...
- elementUI slider组件,带范围选择实现双向绑定
网上查过很多相关文章都没有一章是写element ui滑块带范围实现双向绑定 二个滑块二头的数据怎么得到 我的需求是做个时间轴要滑动选择不同的时间 开始很难做最后一点一点摸索得出的结论 好在写出来了先 ...
- LeetCode 685. Redundant Connection II 冗余连接 II (C++/Java)
题目: In this problem, a rooted tree is a directed graph such that, there is exactly one node (the roo ...
- 剑指Offer-64.滑动窗口的最大值(C++/Java)
题目: 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6, ...
- redis高可用哨兵篇
https://redis.io/docs/manual/sentinel/#sentinels-and-replicas-auto-discovery 官网资料 在上文主从复制的基础上,如果注节点出 ...
- 关于excel表
对excel表的操作主要通过xlwt,xlrd模块. 创建excel表 import xlwtworkbook = xlwt.Workbook(encoding='utf-8') worksheet ...
- 天地图添加多个覆盖物,点击切换选中icon
天地图添加多个覆盖物,点击覆盖物,切换选中的icon,移除之前的icon,再次点击移除之前的... 这个是react写的,先是确定中心位置,然后渲染点位,添加覆盖物,选中icon的不同, 主要看 ...
- 小米节假日API, 查询调休
小米的节假日API, 用于查询一年中的第X天是否正在放假或是在调休. 在浏览器中打开保存下来, 一年只需要调用一次即可. https://api.comm.miui.com/holiday/holid ...