springboot整合bboss操作elasticsearch
·
elaticsearch有很多的java客户端,像transportclient,jestclient,springdata。但是还有一种bboss可以让我们像操作mybatis一样的来操作elasticsearch,并且可以和springboot无缝整合,非常的方便快捷。
1.首先引入依赖。
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
<version>5.7.5</version>
</dependency>
2.添加一个配置类,指明mapper位置,类似于mybatis的mapperScan
@Configuration
public class EsConfig {
@Bean
public ClientInterface generateClient(){
return ElasticSearchHelper.getConfigRestClientUtil("esmapper/dsl.xml");
}
}
3.mapper中写入查询语句
<property name="searchList">
<![CDATA[
{
"query": {
"match": {
"name":#[name]
}
},
"size":10
}
]]>
</property>
4.开始调用
@RestController
public class EsController {
@Autowired
private ClientInterface clientInterface;
@GetMapping("demos")
public ResponseEntity<List<Demo>> getDemos(@RequestParam String name){
Map<String, Object> params = new HashMap<>();
params.put("name", name);
ESDatas<Demo> searchList = clientInterface.searchList("demo/_doc/_search", "searchList", params, Demo.class);
List<Demo> datas = searchList.getDatas();
return ResponseEntity.ok(datas);
}
}
5.postman调用

完毕,真的像mybatis那样来操作es,真的是非常的清爽。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)