springboot 整合 elasticsearch
1引入jar包
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2实体类
package com.miracle.config.elasticsearch.entity; import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document; import java.io.Serializable; /**
* Coding makes me happy.
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ☆☆☆ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┗━┓ 史 ┏━┛
* ┃ 诗 ┃神兽保佑
* ┃ 之 ┃代码无BUG!
* ┃ 宠 ┗━━━┓
* ┃Author: ┣┓
* ┃ liu.Q ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ----------------------
*
* @Date : 2018/3/7 下午2:35
* @Description :elasticsearch 商品信息库
*/
@Data
@Document(indexName = "goodsindex",type = "goods")
//indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
//type类型 可以理解为表名
public class GoodsInfo implements Serializable {
private int id; //商品规格id
private int goods_id;//商品id
private String goods_name;//商品名称
private String goods_sku_name;//商品规格名称
private String goods_class_code;//商品分类编号
private String goods_class_name;//商品分类名称
private double price;//价格
private int sales_num;//销量(初始销量+真实销量)
private int stock;//库存 private String description;//搜索关键字逗号(,)分隔 ==(分类名称,商品名称,规格名称) public GoodsInfo() {
} public GoodsInfo(int id, int goods_id, String goods_name, String goods_sku_name, String goods_class_code, String goods_class_name, double price, int sales_num, int stock, String description) {
this.id = id;
this.goods_id = goods_id;
this.goods_name = goods_name;
this.goods_sku_name = goods_sku_name;
this.goods_class_code = goods_class_code;
this.goods_class_name = goods_class_name;
this.price = price;
this.sales_num = sales_num;
this.stock = stock;
this.description = description;
}
}
3接口
package com.miracle.config.elasticsearch.service; import com.miracle.config.elasticsearch.entity.GoodsInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component; /**
* Coding makes me happy.
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ☆☆☆ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┗━┓ 史 ┏━┛
* ┃ 诗 ┃神兽保佑
* ┃ 之 ┃代码无BUG!
* ┃ 宠 ┗━━━┓
* ┃Author: ┣┓
* ┃ liu.Q ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ----------------------
*
* @Date : 2018/3/7 下午2:35
* @Description :商品信息 接口
*/
@Component
public interface GoodsRepository extends ElasticsearchRepository<GoodsInfo,Long> {
}
4 controller测试
package com.miracle.controller.wx; import com.alibaba.fastjson.JSONObject;
import com.miracle.config.elasticsearch.entity.GoodsInfo;
import com.miracle.config.elasticsearch.service.GoodsRepository;
import com.miracle.config.redis.RedisService;
import com.miracle.controller.wx.util.AesCbcUtil;
import com.miracle.controller.wx.util.WXUtil;
import com.miracle.mapper.WxUserMapper;
import com.miracle.model.WxUser;
import com.miracle.util.ReturnVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; /**
* Coding makes me happy.
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ☆☆☆ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┗━┓ 史 ┏━┛
* ┃ 诗 ┃神兽保佑
* ┃ 之 ┃代码无BUG!
* ┃ 宠 ┗━━━┓
* ┃Author: ┣┓
* ┃ liu.Q ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ----------------------
*
* @Date : 2018/3/7 下午2:35
* @Description :elasticsearch测试
*/
@Slf4j
@EnableTransactionManagement // 需要事务的时候加上
@RestController
@Api("elasticsearch测试")
@RequestMapping("/wx")
public class ElasticsearchTestController { @Autowired
private GoodsRepository goodsRepository; @ApiOperation(value = "添加/更新", notes = "id一样即可实现更新")
@RequestMapping(value="/save",method = RequestMethod.GET)
public String save(@ModelAttribute GoodsInfo goodsInfo){
goodsRepository.save(goodsInfo);
return "success";
} @ApiOperation(value = "搜索", notes = "搜索")
@RequestMapping(value="/getList",method = RequestMethod.GET)
public List<GoodsInfo> getList(@ApiParam(name = "s",value = "关键字",defaultValue = "商品") String s) {
//创建builder minimumShouldMatch - 匹配到次数
QueryBuilder builder = QueryBuilders.multiMatchQuery(s,"goods_name").minimumShouldMatch(s.length()+""); //builder下有must、should以及mustNot 相当于sql中的and、or以及not
//设置模糊搜索,multiMatchQuery 匹配多字段
// builder.must(QueryBuilders.multiMatchQuery(s,"goods_name"));
// builder.must(QueryBuilders.termQuery(s,"goods_name"));
// builder.should(QueryBuilders.matchQuery("description", s));
//模糊查询
// builder.should(QueryBuilders.fuzzyQuery("goods_name", s));
//设置名称为商品
// builder.must(new QueryStringQueryBuilder("商品").field("goods_name")); //排序
FieldSortBuilder sort = SortBuilders.fieldSort("id").order(SortOrder.DESC); //设置分页
//====注意!es的分页和Hibernate一样api是从第0页开始的=========
PageRequest pageRequest = new PageRequest(0, 10); //构建查询
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//将搜索条件设置到构建中
nativeSearchQueryBuilder.withQuery(builder);
//将分页设置到构建中
nativeSearchQueryBuilder.withPageable(pageRequest);
//将排序设置到构建中
nativeSearchQueryBuilder.withSort(sort);
//生产NativeSearchQuery
NativeSearchQuery query = nativeSearchQueryBuilder.build(); //执行,返回包装结果的分页
Page<GoodsInfo> resutlList = goodsRepository.search(query);
return resutlList.getContent();
} @ApiOperation(value = "查询全部", notes = "查询全部")
@RequestMapping(value="/getAll",method = RequestMethod.GET)
public List<GoodsInfo> searchCity() {
//构建查询
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//生产NativeSearchQuery
NativeSearchQuery query = nativeSearchQueryBuilder.build();
//执行,返回包装结果的分页
Page<GoodsInfo> resutlList = goodsRepository.search(query); return resutlList.getContent();
}
@ApiOperation(value = "删除全部", notes = "删除全部")
@RequestMapping(value="/deleteAll",method = RequestMethod.GET)
public String deleteAll() {
goodsRepository.deleteAll();
return "success";
}
}
5 官方api参考地址
https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html
springboot 整合 elasticsearch的更多相关文章
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- ElasticSearch(2)---SpringBoot整合ElasticSearch
SpringBoot整合ElasticSearch 一.基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elast ...
- springboot整合elasticsearch入门例子
springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...
- Springboot整合elasticsearch以及接口开发
Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...
- SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)
准备工作 环境准备 JAVA版本 java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121 ...
- Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]
Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...
- Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看
一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...
- 😊SpringBoot 整合 Elasticsearch (超详细).md
SpringBoot 整合 Elasticsearch (超详细) 注意: 1.环境搭建 安装es Elasticsearch 6.4.3 下载链接 为了方便,环境使用Windows 配置 解压后配置 ...
- SpringBoot整合elasticsearch
在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...
- springboot整合elasticsearch(基于es7.2和官方high level client)
前言 最近写的一个个人项目(传送门:全终端云书签)中需要用到全文检索功能,目前 mysql,es 都可以做全文检索,mysql 胜在配置方便很快就能搞定上线(参考这里),不考虑上手难度,es 在全文检 ...
随机推荐
- 自定义Git【转】
本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 自定义Git 在安装G ...
- NOIP 2018 兔纸旅游记
今年是第一次参加tg呢... Day0 早上出发去中旅坐大巴,走有 lz 特色的OI比赛道路. 车上谈笑风生,看 jw 的 GDOI 的小本本. 到动车站取票入站,看 lmh 和 zn 的爱恨情 ...
- nil 作比较时应该加上双引号 "
> type(X) nil > type(X)==nil false > type(X)=="nil" true >
- Qt532_QWebView做成DLL供VC/Delphi使用_Bug
Qt5.3.2 vs2010 OpenGL ,VC6.0,Delphi7 1.自己继承 类QWebView,制作成DLL 供 VC6/Delphi7 使用 2.测试下来,DLL供VC6使用: 加载&q ...
- 【源码学习之spark streaming 1.6.1 】
说明:个人原创,转载请说明出处 http://www.cnblogs.com/piaolingzxh/p/5634577.html 未完待续
- [sql]SET NOCOUNT ON 的作用
使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息. 说明: 场景1:(不使用SET NOCOUNT) 场景2:(使用SET NOCOUNT ON ) 场景3:(使用SET N ...
- 微信小程序引入md5.js
今天给大家安利一下微信小程序引入md5.js的方法,不多说 md5.js在下面 直接复制到项目的utils/md5.js即可 /* * A JavaScript implementation of t ...
- 使用POI导入小数变成浮点数异常
例如 我在Excel中的数据为17.2, 导入到表中就变成了17.1999999或者17.20000001 原因是我用double接收了17.2,然后直接用了String去转换,精度就丢失了. 代 ...
- Hibernate主键生成器
主键生成器负责生成数据表记录的主键:increment:为long,short或者int类型主键生成唯一标识.只有在没有其他进程往同一张表中插入数据时才能使用.在集群下不能使用! identity:在 ...
- 007PHP文件处理—— 判断文件与操作文件fopen fread fclose is_executable is_readable is_writeable
<?php /** * 判断文件与操作文件fopen fread fclose * is_executable is_readable is_writeable */ //判断一个文件是不是一个 ...