springboot 整合 elasticsearch
Posted IT人生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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> searchCity(@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系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQSpringBoot 整合 RabbitMQSpringBoot 整合 Kafka)
[SpringBoot系列]SpringBoot如何整合SSMP