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 攻击等 ...
随机推荐
- Android Studio的安装
下载Android Studio(需要翻墙才能安装得快):直接到官网进行下载就可以了.下载地址:https://developer.android.com/ “Android Virtual Devi ...
- hdu 3572 : Task Schedule (网络流)
题目链接 题意: 有M个机器,N个任务 对第i个任务,需要在[Si,Ei]这段时间内恰有Pi天被process 每天最多有M个机器同时工作 每一天,一个任务若被process,那么它恰占用一个机器. ...
- zrender笔记----(数字Number组件)出现的问题和解决办法
1.期望的效果是这样子的(这也是最终结果): 2.开始是用json假数据,开始没考虑null的问题,导致在判断传值处,判断有误. 导致在对接接口时,凌乱了,后来修改了下变成后面图C的逻辑,json数据 ...
- BZOJ 3357: [Usaco2004]等差数列 动态规划
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- Oulipo【Hash】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45515 Accepted: 18181 Descript ...
- 6.10&&6.12考试反思
考试结果:6.10AK 6.12:100(评测机)200(本地&&兼容评测机版) OI的考试做题流程无非是: 通读全部题目——>找一个最有把握/最简单的题——>分析思考—— ...
- 从源码编译UE4,加快Setup.bat下载文件的环节
之前很傻,每次运行这个setup.bat都要等很久很久才能把4g多的东西下载完成,知道有一天突然发现了世外桃源…… 从命令行运行setup.bat -help,可以看到参数的说明(没错,参数可选,之前 ...
- Kaggle 房价预测问题参考资料
作者的 Kaggle 主页:https://www.kaggle.com/pavansanagapati Tutorial - Housing Prices Model Prediction http ...
- iOS环境搭建
Xcode安装 一定要在App Store上下载XCode . git config常用配置 设置lg命令 查看分支图 git config --global alias.lg "log - ...
- HDU6715 算术(莫比乌斯反演)
HDU6715 算术 莫比乌斯反演的变形. 对 \(\mu(lcm(i,j))\) 变换,易得 \(\mu(lcm(i,j)) = \mu(i)\cdot\mu(j)\cdot \mu(gcd(i,j ...