package com.es.springbootesdemo.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
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.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
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.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.es.springbootesdemo.domain.BulkUser;
import com.es.springbootesdemo.domain.User;
/**
* @author: yushizhong
* @date: 2022-01-19 11:12
* @version: 1.0
**/
@RestController
@RequestMapping("/es")
public class ElasticSearchController {
@Resource
private RestHighLevelClient client;
/**
* 数据添加到ES
*
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public HttpEntity addUser(@RequestBody User user) throws IOException {
IndexRequest request = new IndexRequest("user", "ysz");
// request.id("3");
request.timeout("1s");
request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse resp = client.index(request, RequestOptions.DEFAULT);
System.out.println("添加文档-------" + JSON.toJSONString(resp));
return new HttpEntity(resp.getResult().getLowercase().equals("created"));
}
/**
* 删除
*
* @param id
* @return
* @throws IOException
*/
@DeleteMapping("/{id}/delete")
public HttpEntity deleteUser(@PathVariable("id") String id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("user", "ysz", id);
deleteRequest.timeout("1s");
DeleteResponse resp = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println("测试删除文档------" + resp.status());
return new HttpEntity("OK".equals(resp.status()));
}
/**
* 批量增加
*/
@PostMapping("/batch-add")
public HttpEntity bulkAddUsers(@RequestBody BulkUser bulkUser) throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("1s");
for (User user : bulkUser.getUserList()) {
bulkRequest.add(new IndexRequest("user").type("ysz").source(JSON.toJSONString(user), XContentType.JSON));
}
BulkResponse resp = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("测试批量添加文档-----" + resp.hasFailures());
return new HttpEntity(!resp.hasFailures());
}
/**
* 修改数据
*
* @param user
* @param id
* @return
* @throws IOException
*/
@PostMapping("/update")
public HttpEntity updateUser(@RequestBody BulkUser user, @RequestParam(value = "id") String id) throws IOException {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("user").type("ysz").timeout("1s").id(id).doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse resp = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("测试修改文档-----" + resp.status());
return new HttpEntity("OK".equals(resp.status()));
}
/**
* 查询es所有用户
*/
@GetMapping("/user/list")
public BulkUser getUsers() throws IOException {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(0).size(100).timeout(new TimeValue(60, TimeUnit.SECONDS));
// sourceBuilder.fetchSource(new String[] {"fields.name", "fields.sex", "fields.age"}, new String[] {});
SearchRequest searchRequest = new SearchRequest("user");
searchRequest.types("ysz").source(sourceBuilder);
SearchResponse resp = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = resp.getHits();
SearchHit[] searchHits = hits.getHits();
List<User> users = new ArrayList<>();
for (SearchHit searchHit : searchHits) {
String aa = searchHit.getSourceAsString();
User sourceUser = JSONObject.parseObject(searchHit.getSourceAsString(), User.class);
System.out.println(sourceUser);
users.add(sourceUser);
}
BulkUser bulkUser = new BulkUser();
bulkUser.setUserList(users);
return bulkUser;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SpringBoot集成Elasticsearch实现增删改查的功能代码
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共14个文件
java:5个
xml:2个
properties:2个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 74 浏览量
2024-06-21
10:46:54
上传
评论
收藏 67KB ZIP 举报
温馨提示
SpringBoot集成Elasticsearch实现增删改查的功能代码
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 14 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/7ec11ac754d345228d32a6ea43f54894_weixin_43860634.jpg!1)
飞翔的佩奇
- 粉丝: 4294
- 资源: 991
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
下载权益
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
C知道特权
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
VIP文章
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
课程特权
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
开通VIP
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)