spring boot集成elasticsearch,
spring boot集成elasticsearch
- 温馨链接
- 搭建spring-boot项目
- 配置elasticsearch
- 查询接口
- 添加接口
- 删除接口
- 更新接口
- 复合查询接口
温馨链接
- elasticsearch入门之下载安装
- elasticsearch入门之基本用法
- elasticsearch入门之高级查询
- spring boot集成elasticsearch
- spring boot集成elasticsearch项目地址
搭建spring-boot项目
- 快速构建:https://start.spring.io/
- 导入项目,添加elasticsearch依赖
配置elasticsearch
后续对elasticsearch的开发,都依赖这个配置
@Configuration
public class MyConfig {
@Bean
public TransportClient client() throws UnknownHostException{
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName("localhost"),// 节点地址
9300// TCP端口,不是http端口9200
);
Settings settings = Settings.builder()
.put("cluster.name","yukang")// 节点名
.build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddresses(node);// 找到节点,可添加多个
return client;
}}
查询接口
- prepareGet:三个参数索引、类型、文档id
@Autowired
private TransportClient client;
@GetMapping("get/people/man")
@ResponseBody
public ResponseEntity get(@RequestParam(name="id",defaultValue = "")String id){
if(id.isEmpty()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
// 通过配置操作
GetResponse response = this.client.prepareGet("people","man",id)
.get();
if(!response.isExists()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(response.getSource(),HttpStatus.OK);
}
- 请求示例:
- resquest:http://localhost:8080/get/people/man?id=1
- response:
{
“country”: “China”,
“data”: “2000-03-09”,
“name”: “浴缸”,
“age”: “20”
}
添加接口
- prepareIndex:两个参数索引、类型
@PostMapping("/add/people/man")
@ResponseBody
public ResponseEntity add(
@RequestParam(name = "name") String name,
@RequestParam(name = "country") String country,
@RequestParam(name = "age") int age,
@RequestParam(name = "data")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
Date data){
try {
XContentBuilder content = XContentFactory.jsonBuilder()
.startObject()// 开始构建文档
.field("name",name)
.field("country",country)
.field("age",age)
.field("data",data.getTime())
.endObject();// 结束构建文档
IndexResponse response = this.client.prepareIndex("people","man")
.setSource(content)
.get();
return new ResponseEntity(response.getId(),HttpStatus.OK);// 返回文档id
}catch (IOException e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);// 内部错误
}
}
刷新elasticsearch-head看看,data为毫秒,因 为我们取的是getTime()
删除接口
- prepareDelete:三个参数索引、类型、文档id
@DeleteMapping(“delete/people/man”)
@ResponseBody
public ResponseEntity delete(@RequestParam(name = "id")String id){
DeleteResponse response = this.client.prepareDelete("people","man",id)
.get();
return new ResponseEntity(response.getResult().toString(),HttpStatus.OK);
}
- response:DELETED
更新接口
- UpdateRequest:三个参数索引、类型、文档id
@PutMapping(“update/people/man”)
@ResponseBody
public ResponseEntity update(
@RequestParam(name = "id") String id,
@RequestParam(name = "country",required = false) String country,
@RequestParam(name = "age",required = false) String age
){
UpdateRequest update = new UpdateRequest("people","man",id);
try {
// 构建文档
XContentBuilder content = XContentFactory.jsonBuilder()
.startObject();
if(country != null){
content.field("country",country);
}
if(age != null){
content.field("age",age);
}
content.endObject();
update.doc(content);
}catch (IOException e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);// 内部错误
}
try {
UpdateResponse response = this.client.update(update).get();
return new ResponseEntity(response.getResult().toString(),HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);// 内部错误
}
}
- rsponse:UPDATED
复合查询接口
- BoolQueryBuilder:boolean查询条件构建
- RangeQueryBuilder:范围查询条件构建
@PostMapping(“query/people/man”)
@ResponseBody
public ResponseEntity query(
@RequestParam(name = "name",required = false) String name,
@RequestParam(name = "country",required = false) String country,
@RequestParam(name = "gt_age",defaultValue = "0") int gt_age,
@RequestParam(name = "lt_age",required = false) Integer lt_age){
// boolean条件
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if(name != null)
boolQuery.must(QueryBuilders.matchQuery("name",name));
if(country != null)
boolQuery.must(QueryBuilders.matchQuery("country",country));
// 范围条件
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age")
.from(gt_age);
if(lt_age != null && lt_age > 0)
rangeQuery.to(lt_age);
// boolean条件结合范围条件
boolQuery.filter(rangeQuery);
// 构建builder
SearchRequestBuilder builder = this.client.prepareSearch("people")// 节点
.setTypes("man")// 类型
.setSearchType(SearchType.DEFAULT)
.setQuery(boolQuery)// 设置查询条件
.setFrom(0)// 开始下标
.setSize(10);// 查询大小
// 发送请求
SearchResponse response = builder.get();
// 接收返回
List<Map<String,Object>> result = new ArrayList<>();
for (SearchHit hit : response.getHits())
result.add(hit.getSource());
return new ResponseEntity(result,HttpStatus.OK);
}
- response
[
{
“country”: “China”,
“data”: “2000-03-09”,
“name”: “浴池”,
“age”: 18
},
{
“country”: “China”,
“data”: “2000-03-09”,
“name”: “浴板”,
“age”: 18
}]
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。