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 在全文检 ...
随机推荐
- DataContract with Json.Net
https://www.newtonsoft.com/json/help/html/DataContractAndDataMember.htm 如果class使用了DataContract,name没 ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)
http://codeforces.com/contest/761/problem/E 题意:给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行.输出可行解,即 ...
- python 字符串的反转
def string_reverse(str1): rstr1 = '' index = len(str1) : rstr1 += str1[ index - ] index = index - re ...
- org.springframework.transaction 包改成 spring-tx
org.springframework.transaction 包改成 spring-tx org.springframework.transaction 3.2.2以后的版本,全改到 spring ...
- WPF特效和例子
https://www.cnblogs.com/AaronYang/p/4710428.html
- UVA-10779 Collectors Problem (网络流建模)
题目大意:有n个人,已知每人有ki个糖纸,并且知道每张糖纸的颜色.其中,Bob希望能和同伴交换使得手上的糖纸数尽量多.他的同伴只会用手上的重复的交换手上没有的,并且他的同伴们之间不会产生交换.求出Bo ...
- POJ 2409 Let it Bead (Polya定理)
题意 用k种颜色对n个珠子构成的环上色,旋转翻转后相同的只算一种,求不等价的着色方案数. 思路 Polya定理 X是对象集合{1, 2, --, n}, 设G是X上的置换群,用M种颜色染N种对象,则不 ...
- Swagger使用总结(十九)
1. Swagger是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. 官方说法:Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTfu ...
- bzoj4811
题解: 对于每一个节点,我们建立v0,v1 v0表示0进过会怎么样 v1表示1进过会怎么样 然后线段树合并 代码: #include <cstdio> #include <cstri ...
- ubuntu16.04 NVIDIA CUDA8.0 以及cuDNN安装
下载CUDA 官网下载按照自己的实际情况进行选择,下载合适的版本. 官方安装指南 注意这里下载的是cuda8.0的runfile(local)文件. 安装CUDA 下载完成后,解压到当前目录,切换到该 ...