Spring Boot & ES 实战,值得参考!
作者:废物大师兄
1. 前言
1.1. 集成方式
REST Client
Jest
Spring Data
Spring Data Elasticsearch Repositories
1.2. 环境与配置
network.host: 192.168.1.134
http.port: 9200
cheng hard nofile 65536
1.3. 版本
java.lang.NoClassDefFoundError: org/elasticsearch/common/transport/InetSocketTransportAddress
2. 依赖
<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-nodes=192.168.1.134:9300
4. 操作
4.1. 使用Spring Data Elasticsearch Repositories操作Elasticsearch
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;
}
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> {
}
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);
}
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);
}
}
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());
}
}
4.2. 使用ElasticsearchTemplate方式操作Elasticsearch
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);
}
}
5. 演示
6. 工程结构
7. 参考
https://docs.spring.io/spring-data/elasticsearch/docs/3.1.0.RELEASE/reference/html/#repositories.query-methods.details
- END -
关注Java技术栈微信公众号,在后台回复关键字:Java,可以获取一份栈长整理的 Java 最新技术干货。
最近干货分享
点击「阅读原文」加入栈长的战队~
Spring Boot & ES 实战,值得参考!的更多相关文章
- Spring Boot 项目实战(五)集成 Dubbo
一.前言 上篇介绍了 Redis 的集成过程,可用于解决热点数据访问的性能问题.随着业务复杂度的提高,单体应用越来越庞大,就好比一个类的代码行数越来越多,分而治之,切成多个类应该是更好的解决方法,所以 ...
- “Spring Boot+Marklogic实战应用(1)”
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议.本文链接:http://www.blbk.info Spring Boot+Marklogic应用 摘要: 在前一节的介绍,相信 ...
- Spring Boot GraphQL 实战 02_增删改查和自定义标量
hello,大叫好,我是小黑,又和大家见面啦~ 今天我们来继续学习 Spring Boot GraphQL 实战,我们使用的框架是 https://github.com/graphql-java-ki ...
- Spring Boot GraphQL 实战 03_分页、全局异常处理和异步加载
hello,大家好,我是小黑,又和大家见面啦~ 今天我们来继续学习 Spring Boot GraphQL 实战,我们使用的框架是 https://github.com/graphql-java-ki ...
- spring boot插件开发实战和原理
本文转载自spring boot插件开发实战和原理 实战:编写spring boot插件 为什么要编写boot插件 因为我们在开发的时候需要提供一些共同的功能,所以我们编写个共同的jar包.开发人员在 ...
- 【建议收藏】缺少 Vue3 和 Spring Boot 的实战项目经验?我这儿有啊!
缺少 Vue3 和 Spring Boot 的实战项目经验?缺少学习项目和练手项目?我这儿有啊! 从 2019 年到 2021 年,空闲时间里陆陆续续做了一些开源项目,推荐给大家啊!记得点赞和收藏噢! ...
- Spring Boot 2 实战:利用Redis的Geo功能实现查找附近的位置
1. 前言 老板突然要上线一个需求,获取当前位置方圆一公里的业务代理点.明天上线!当接到这个需求的时候我差点吐血,这时间也太紧张了.赶紧去查相关的技术选型.经过一番折腾,终于在晚上十点完成了这个需求. ...
- Github 上热门的 Spring Boot 项目实战推荐
最近经常被读者问到有没有 Spring Boot 实战项目可以学习,于是,我就去 Github 上找了 10 个我觉得还不错的实战项目.对于这些实战项目,有部分是比较适合 Spring Boot 刚入 ...
- Spring Boot 2 实战:如何自定义 Servlet Filter
1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter 来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等 ...
随机推荐
- python基础练习题3
01:有1,2,3,4个数字,能组成多少个互不相同且无重复的三位数,都是多少思路:可填写在百位,十位,个位的数字都是1,2,3,4.组成所有的排列后再去掉不满足条件的排列 list =[,,,] li ...
- 手机能连接上ipsec
手机能连接上,但是FQ不行,路由也正常,iptables也设置,内核转发也弄了,暂时需要看看日志才行了,不知道什么情况,其他类型的没问题
- Proxy + Reflect 实现 响应的数据变化
Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等) let p = new Proxy(target, handler); get(target, propKey, r ...
- 前端之HTML:HTML
前端基础之html 一.初始html 1.web服务本质 import socket sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) soc ...
- 用电脑Python控制Arduino
python指令: import serial #导入串口通讯库 import time ser=serial.Serial("com4",9600,timeout=1) demo ...
- margin与padding的区别是什么?
margin与padding的区别是什么? 目录 1.背景介绍 2.知识剖析 3.常见问题 4.解决方案 5.编码实战 6.扩展思考 7.参考文献 8.更多讨论 1.背景介绍 什么是margin 什么 ...
- mysql 语句积累
show tables; 显示表 DROP TABLE IF EXISTS emp;删除表
- php日志托管给apache处理
php.ini配置: log_errors = On;不显示错误display_startup_errors = Offdisplay_errors = Off ;除了notice级别错误外,报告所有 ...
- kohana 语言资源国际化、本地化
语言配置开关: root@DESKTOP-I4OIMJC /cygdrive/e/html/tproject/framebota/platform # grep -n2 'I18n::lang' bo ...
- 一本通&&洛谷 ——靶型数独——题解
题目传送 主要是搜索顺序不同导致效率千差万别. 联想人做数独的策略,总是先填可填数最少的那个空,再填选择第二少的...其实这种策略就造就了一个深度浅时分支也较少的搜索树.合适的搜索顺序再配合剪枝==A ...