SpringBoot+Elasticsearch
1. 前言
1.1. 集成方式
spring boot中集成elasticsearch有4种方式:
- rest client
- jest
- spring data
- spring data elasticsearch repositories
本文用后面两种方式来分别连接并操作elasticsearch
1.2. 环境与配置
服务端:elasticsearch-6.3.2 1台
客户端:elasticsearch 6.4.1
服务端配置文件:elasticsearch.yml
cluster.name: my-application network.host: 192.168.1.134 http.port: 9200
/etc/security/limits.conf
cheng soft nofile 65536 cheng hard nofile 65536
/etc/sysctl.conf
vm.max_map_count=262144
1.3. 版本
spring boot 2.0.5默认的elasticsearch版本很低,这里我们用最新版本6.4.1
如果启动过程中出现
java.lang.noclassdeffounderror: org/elasticsearch/common/transport/inetsockettransportaddress
则说明,elasticsearch依赖的jar包版本不一致,统一改成6.4.1即可
另外,spring boot 2.0.5依赖的spring-data-elasticsearch版本是3.0.1,需要升级到3.1.0
2. 依赖
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.cjs.example</groupid> <artifactid>cjs-elasticsearch-example</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>cjs-elasticsearch-example</name> <description></description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.5.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <elasticsearch.version>6.4.1</elasticsearch.version> <spring.data.elasticsearch.version>3.1.0.release</spring.data.elasticsearch.version> </properties> <dependencies> <dependency> <groupid>org.elasticsearch</groupid> <artifactid>elasticsearch</artifactid> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupid>org.elasticsearch.client</groupid> <artifactid>transport</artifactid> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupid>org.elasticsearch.client</groupid> <artifactid>elasticsearch-rest-client</artifactid> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupid>org.elasticsearch.plugin</groupid> <artifactid>transport-netty4-client</artifactid> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-elasticsearch</artifactid> <version>${spring.data.elasticsearch.version}</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-elasticsearch</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <optional>true</optional> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
3. application.properties
spring.data.elasticsearch.cluster-name=my-application spring.data.elasticsearch.cluster-nodes=192.168.1.134:9300
也许,大家会疑惑,配置文件中明明写的端口是9200,为何这里配置文件中连接的时候写的端口是9300呢?
因为,配置9200是通过http连接的端口,9300是tcp连接的端口
4. 操作
4.1. 使用spring data elasticsearch repositories操作elasticsearch
首先,定义一个实体类
package com.cjs.example.entity; import lombok.data; import org.springframework.data.annotation.id; import org.springframework.data.elasticsearch.annotations.document; import java.io.serializable; @data @document(indexname = "commodity") public class commodity implements serializable { @id private string skuid; private string name; private string category; private integer price; private string brand; private integer stock; }
这里定义了commodity实例,表示商品。在elasticsearch 6.x 版本中,不建议使用type,而且在7.x版本中将会彻底废弃type,所以此处我只指定了indexname,没有指定type。这里,一个commodity代表一个商品,同时代表一条索引记录。
类比关系型数据库的话,index相当于表,document相当于记录
然后,需要自己定义一个接口,并继承elasticsearchrepository
package com.cjs.example.dao; import com.cjs.example.entity.commodity; import org.springframework.data.elasticsearch.repository.elasticsearchrepository; import org.springframework.stereotype.repository; @repository public interface commodityrepository extends elasticsearchrepository<commodity, string> { }
这里的repository相当于dao,操作mysql还是elasticsearch都是一样的
接下来,定义service接口
package com.cjs.example.service; import com.cjs.example.entity.commodity; import org.springframework.data.domain.page; import java.util.list; public interface commodityservice { long count(); commodity save(commodity commodity); void delete(commodity commodity); iterable<commodity> getall(); list<commodity> getbyname(string name); page<commodity> pagequery(integer pageno, integer pagesize, string kw); }
实现类
package com.cjs.example.service.impl; import com.cjs.example.entity.commodity; import com.cjs.example.dao.commodityrepository; import com.cjs.example.service.commodityservice; import org.elasticsearch.index.query.matchquerybuilder; import org.elasticsearch.index.query.querybuilders; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.domain.page; import org.springframework.data.domain.pagerequest; import org.springframework.data.elasticsearch.core.query.nativesearchquerybuilder; import org.springframework.data.elasticsearch.core.query.searchquery; import org.springframework.stereotype.service; import java.util.arraylist; import java.util.list; @service public class commodityserviceimpl implements commodityservice { @autowired private commodityrepository commodityrepository; @override public long count() { return commodityrepository.count(); } @override public commodity save(commodity commodity) { return commodityrepository.save(commodity); } @override public void delete(commodity commodity) { commodityrepository.delete(commodity); // commodityrepository.deletebyid(commodity.getskuid()); } @override public iterable<commodity> getall() { return commodityrepository.findall(); } @override public list<commodity> getbyname(string name) { list<commodity> list = new arraylist<>(); matchquerybuilder matchquerybuilder = new matchquerybuilder("name", name); iterable<commodity> iterable = commodityrepository.search(matchquerybuilder); iterable.foreach(e->list.add(e)); return list; } @override public page<commodity> pagequery(integer pageno, integer pagesize, string kw) { searchquery searchquery = new nativesearchquerybuilder() .withquery(querybuilders.matchphrasequery("name", kw)) .withpageable(pagerequest.of(pageno, pagesize)) .build(); return commodityrepository.search(searchquery); } }
在这个service中演示了增删查改操作,还有分页查询
最后,写一个测试类测试其中的方法
package com.cjs.example; import com.cjs.example.entity.commodity; import com.cjs.example.service.commodityservice; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.data.domain.page; import org.springframework.test.context.junit4.springrunner; import java.util.list; @runwith(springrunner.class) @springboottest public class cjselasticsearchexampleapplicationtests { @autowired private commodityservice commodityservice; @test public void contextloads() { system.out.println(commodityservice.count()); } @test public void testinsert() { commodity commodity = new commodity(); commodity.setskuid("1501009001"); commodity.setname("原味切片面包(10片装)"); commodity.setcategory("101"); commodity.setprice(880); commodity.setbrand("良品铺子"); commodityservice.save(commodity); commodity = new commodity(); commodity.setskuid("1501009002"); commodity.setname("原味切片面包(6片装)"); commodity.setcategory("101"); commodity.setprice(680); commodity.setbrand("良品铺子"); commodityservice.save(commodity); commodity = new commodity(); commodity.setskuid("1501009004"); commodity.setname("元气吐司850g"); commodity.setcategory("101"); commodity.setprice(120); commodity.setbrand("百草味"); commodityservice.save(commodity); } @test public void testdelete() { commodity commodity = new commodity(); commodity.setskuid("1501009002"); commodityservice.delete(commodity); } @test public void testgetall() { iterable<commodity> iterable = commodityservice.getall(); iterable.foreach(e->system.out.println(e.tostring())); } @test public void testgetbyname() { list<commodity> list = commodityservice.getbyname("面包"); system.out.println(list); } @test public void testpage() { page<commodity> page = commodityservice.pagequery(0, 10, "切片"); system.out.println(page.gettotalpages()); system.out.println(page.getnumber()); system.out.println(page.getcontent()); } }
以上,便是使用elasticsearch repositories的方式
4.2. 使用elasticsearchtemplate方式操作elasticsearch
package com.cjs.example; import com.cjs.example.entity.commodity; import org.elasticsearch.index.query.querybuilders; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.data.elasticsearch.core.elasticsearchtemplate; import org.springframework.data.elasticsearch.core.query.*; import org.springframework.test.context.junit4.springrunner; import java.util.list; @runwith(springrunner.class) @springboottest public class elasticsearchtemplatetest { @autowired public elasticsearchtemplate elasticsearchtemplate; @test public void testinsert() { commodity commodity = new commodity(); commodity.setskuid("1501009005"); commodity.setname("葡萄吐司面包(10片装)"); commodity.setcategory("101"); commodity.setprice(160); commodity.setbrand("良品铺子"); indexquery indexquery = new indexquerybuilder().withobject(commodity).build(); elasticsearchtemplate.index(indexquery); } @test public void testquery() { searchquery searchquery = new nativesearchquerybuilder() .withquery(querybuilders.matchquery("name", "吐司")) .build(); list<commodity> list = elasticsearchtemplate.queryforlist(searchquery, commodity.class); system.out.println(list); } }
elasticsearchtemplate是自动配置的
5. 演示
6. 工程结构
7. 参考