SpringBoot整合Elasticsearch并实现CRUD操作
程序员文章站
2022-04-14 15:05:54
配置准备
在build.gradle文件中添加如下依赖:
compile "org.elasticsearch.client:transport...
配置准备
在build.gradle文件中添加如下依赖:
compile "org.elasticsearch.client:transport:5.5.2" compile "org.elasticsearch:elasticsearch:5.5.2" //es 5.x的内部使用的 apache log4日志 compile "org.apache.logging.log4j:log4j-core:2.7" compile "org.apache.logging.log4j:log4j-api:2.7"
这里spring boot使用的是1.5.4版,前些天spring boot 2正式版已经发布,spring boot 2新特性中有一条是支持kotlin,spring boot 2基于spring 5,spring 5也支持了koltin,所以spring也开始支持函数式编程。
关于版本兼容
配置访问elasticsearch的客户端,这里都使用原生es javaapi。
@configuration public class elasticsearchconfig { @bean(name = "client") public transportclient getclient() { inetsockettransportaddress node = null; try { node = new inetsockettransportaddress(inetaddress.getbyname("192.168.124.128"), 9300); } catch (unknownhostexception e) { e.printstacktrace(); } settings settings = settings.builder().put("cluster.name", "my-es").build(); transportclient client = new prebuilttransportclient(settings); client.addtransportaddress(node); return client; } }
sockettransport端口可以使用方式查看,这里默认使用的是9300端口。
crud操作
新建一个控制器elasticsearchcontroller,使用原生的es javaapi。
@restcontroller public class elasticsearchcontroller { @autowired transportclient client; }
在控制器中添加增删查改方法
增加操作
@postmapping("add/book/novel") public responseentity add( @requestparam(name = "title") string title, @requestparam(name = "authro") string author, @requestparam(name = "word_count") int wordcount, @requestparam(name = "publish_date") @datetimeformat(pattern = "yyyy-mm-dd hh:mm:ss")date publishdate ) { try { xcontentbuilder content = xcontentfactory.jsonbuilder().startobject() .field("title", title) .field("author", author) .field("word_count", wordcount) .field("publish_date", publishdate.gettime()) .endobject(); indexresponse result = this.client.prepareindex("book", "novel").setsource(content).get(); return new responseentity(result.getid(), httpstatus.ok); } catch (ioexception e) { e.printstacktrace(); return new responseentity(httpstatus.internal_server_error); } }
删除操作
@deletemapping("/delete/book/novel") public responseentity delete(@requestparam(name = "id") string id) { deleteresponse result = client.preparedelete("book", "novel", id).get(); return new responseentity(result.getresult().tostring(), httpstatus.ok); }
查找操作
@getmapping("/get/book/novel") public responseentity get(@requestparam(name = "id", defaultvalue="") string id) { if (id.isempty()) { return new responseentity(httpstatus.not_found); } getresponse result = this.client.prepareget("book", "novel", id).get(); if (!result.isexists()) { return new responseentity(httpstatus.not_found); } return new responseentity(result.getsource(), httpstatus.ok); }
更新操作
@putmapping("/put/book/novel") public responseentity update(@requestparam(name = "id") string id, @requestparam(name = "title", required = false) string title, @requestparam(name = "author", required = false) string author ) { try { xcontentbuilder builder = xcontentfactory.jsonbuilder().startobject(); if (title!= null) { builder.field("title", title); } if (author != null) { builder.field("author", author); } builder.endobject(); updaterequest updaterequest = new updaterequest("book", "novel", id); updaterequest.doc(builder); updateresponse result = client.update(updaterequest).get(); return new responseentity(result.getresult().tostring(), httpstatus.ok); } catch (exception e) { e.printstacktrace(); return new responseentity(httpstatus.internal_server_error); } }
复合查找
@getmapping("/query/book/novel") public responseentity query(@requestparam(name = "author", required = false) string author, @requestparam(name = "title", required = false) string title, @requestparam(name = "gt_word_count", defaultvalue = "0") int gtwordcount, @requestparam(name = "lt_word_count", required = false) integer ltwordcount) { boolquerybuilder boolquerybuilder = querybuilders.boolquery(); if (author != null) { boolquerybuilder.must(querybuilders.matchquery("author",author)); } if (title != null) { boolquerybuilder.must(querybuilders.matchquery("title", title)); } rangequerybuilder rangequerybuilder = querybuilders.rangequery("word_count").from(gtwordcount); if (ltwordcount != null && ltwordcount > 0) { rangequerybuilder.to(ltwordcount); } boolquerybuilder.filter(rangequerybuilder); searchrequestbuilder searchrequestbuilder = this.client.preparesearch("book") .settypes("novel") .setsearchtype(searchtype.dfs_query_then_fetch) .setquery(boolquerybuilder) .setfrom(0) .setsize(10); system.out.println(searchrequestbuilder); //调试用 searchresponse response = searchrequestbuilder.get(); list<map<string, object>> result = new arraylist<>(); for (searchhit hit : response.gethits()) { result.add(hit.getsource()); } return new responseentity(result, httpstatus.ok); }
上面的代码组织的复合查询类似下面的query dsl:
{ "query":{ "bool":{ "must":[ {"match":{"author":"张三"}}, {"match":{"title":"elasticsearch"}} ], "filter":[ {"range": {"word_count":{ "gt":"0", "lt":"3000" } } } ] } } }
总结
以上所述是小编给大家介绍的springboot整合elasticsearch并实现crud操作,希望对大家有所帮助
推荐阅读
-
SpringBoot整合Elasticsearch7.2.0的实现方法
-
springboot整合Elasticsearch实现增删改查
-
SpringBoot整合Mybatis(CRUD的实现)
-
SpringBoot整合Mybatis实现CRUD
-
SpringBoot整合Elasticsearch并实现CRUD操作
-
SpringBoot2.x整合MybatisPlus3.x,实现数据的简单CRUD操作+****代码生成
-
SpringBoot整合Elasticsearch7.2.0的实现方法
-
SpringBoot模拟员工数据库并实现增删改查操作
-
【原】无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页
-
SpringBoot整合Redis使用Restful风格实现CRUD功能