赞
踩
第一步、添加依赖(注意版本对应关系)根据spring boot版本选择合适的版本
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>7.6.2</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- <version>2.3.12.RELEASE</version>
- </dependency>
第二步、添加配置
spring: elasticsearch: rest: uris: localhost:9200 connection-timeout: 1000 request-timeout: 1000 read-timeout: 1000 username: elastic password: elastic
第三步、编码
- /**
- * 商城商品
- * @Author: 陈礼林
- * @CreateTime: 2023-04-20 09:28
- */
- @Document(indexName = "mall_commodity")
- @Data
- public class MallCommodityDtoRsp {
-
- /**
- * 主键
- */
- @Id
- @Field(store = true, index = false, type = org.springframework.data.elasticsearch.annotations.FieldType.Text)
- private String id;
-
- /**
- * 商品名称
- */
- private String name;
-
- /**
- * 分类名称
- */
- private String classifyName;
-
- /**
- * 分类名称
- */
- private String attributeName;
-
- /**
- * 商品缩略图
- */
- private String picture;
-
- /**
- * 商品规格类型 1单规格 2多规格 3套餐
- */
- private Integer specType;
-
- /**
- * 商品编号
- */
- private String commodityNo;
-
- /**
- * 生产批号
- */
- private String batchNo;
- /**
- * 商品类型(1普通,2赠送)
- */
- private Integer commodityType;
-
- /**
- * 销售价格(单位分)
- */
- private Integer minPrice;
-
- /**
- * 销售价格(单位分)
- */
- private Integer maxPrice;
-
- /**
- * 单位
- */
- private String unit;
-
- /**
- * 排序
- */
- private Integer sort;
-
- /**
- * 商品数量
- */
- private Integer commodityCount;
-
- /**
- * 商品状态 1上架 2下架
- */
- private Integer status;
-
- @Field(store = true, index = false, type = FieldType.Object)
- private List<MallCommodityDetailed> detailedList;
-
- }
同步mysql数据到elasticsearch服务器
- /**
- * @Description: 同步mysql数据到es定时任务
- * @Author: 陈礼林
- * @CreateTime: 2024-03-16 10:56
- */
- @Slf4j
- @Component
- public class CommoditySyncEsTask {
-
- @Resource
- private MallCommodityService service;
- @Resource
- private ElasticsearchRestTemplate elasticsearchRestTemplate;
-
-
- @XxlJob("commoditySyncEsHandler")
- public void commoditySyncEs(){
- log.info("开始执行:商品同步定时任务");
- MallCommodityDtoPageReq req = new MallCommodityDtoPageReq();
- List<MallCommodityDtoRsp> records = service.page(req).getRecords();
- if (ListUtil.isNotNull(records)) {
- elasticsearchRestTemplate.save(records);
- }
- log.info("执行结束:商品同步定时任务");
- }
- }
- /**
- * 商城-商品表控制器
- *
- * @author 陈礼林
- * @date 2022-04-12 14:39:41
- */
- @RestController
- @RequestMapping("/mmServer/mall/commodity")
- public class MallCommodityController {
-
- @Resource
- private ElasticsearchRestTemplate elasticsearchRestTemplate;
-
- /**
- * 分页
- *
- * @param req 分页参数
- * @return 查询结果
- */
- @PostMapping("/page")
- public R page(@RequestBody MallCommodityDtoPageReq req){
- //查询条件
- // 查询全部数据
- // QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
-
- // 精确查询 =
- // QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "lisi");
-
- // 精确查询 多个 in
- // QueryBuilder queryBuilder = QueryBuilders.termsQuery("name", "张三", "lisi");
-
- // match匹配,会把查询条件进行分词,然后进行查询,多个词条之间是 or 的关系,可以指定分词
- // QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "张三");
- // QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "张三").analyzer("ik_max_word");
-
- // match匹配 查询多个字段
- // QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("男", "name", "sex");
-
- // fuzzy 模糊查询,返回包含与搜索字词相似的字词的文档。
- // QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("name","lisx");
-
- // prefix 前缀检索
- // QueryBuilder queryBuilder = QueryBuilders.prefixQuery("name","张");
-
- // wildcard 通配符检索
- // QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name","张*");
-
- // regexp 正则查询
- // QueryBuilder queryBuilder = QueryBuilders.regexpQuery("name", "(张三)|(lisi)");
-
- // boost 评分权重,令满足某个条件的文档的得分更高,从而使得其排名更靠前。
- // queryBuilder.boost(2);
- // 多条件构建
- BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
- // 并且 and
- // queryBuilder.must(QueryBuilders.termQuery("name", "张三"));
- if (req.getSpecType() != null) {queryBuilder.must(QueryBuilders.termQuery("specType", req.getSpecType()));}
- if (req.getName() != null) {queryBuilder.should(QueryBuilders.fuzzyQuery("name", req.getName()));}
-
- // 或者 or
- // queryBuilder.should(QueryBuilders.fuzzyQuery("name", "测试"));
- // queryBuilder.should(QueryBuilders.termQuery("name", "lisi"));
-
- // 不等于,去除
- // queryBuilder.mustNot(QueryBuilders.termQuery("name", "lisi"));
-
- // 过滤数据
- // queryBuilder.filter(QueryBuilders.matchQuery("name", "张三"));
-
- // 范围查询
- /*
- gt 大于 >
- gte 大于等于 >=
- lt 小于 <
- lte 小于等于 <=
- */
- // queryBuilder.filter(new RangeQueryBuilder("age").gt(10).lte(50));
-
- // 构建分页,page 从0开始
- Pageable pageable = PageRequest.of(req.getPageNumber(), req.getPageSize());
-
- Query query = new NativeSearchQueryBuilder()
- .withQuery(queryBuilder)
- .withPageable(pageable)
- //排序
- // .withSort(SortBuilders.fieldSort("_score").order(SortOrder.DESC))
- //投影
- // .withFields("name")
- .build();
- SearchHits<MallCommodityDtoRsp> search = elasticsearchRestTemplate.search(query, MallCommodityDtoRsp.class);
- return R.ok(search.getSearchHits());
- // return R.ok(service.page(req));
- }
-
- /**
- * 通过id集合查询列表
- *
- * @param id id
- * @return 查询结果
- */
- @PostMapping("/get")
- public R<MallCommodityDtoRsp> get(@Param("id") Long id){
- MallCommodityDtoRsp search = elasticsearchRestTemplate.get(id.toString(),MallCommodityDtoRsp.class);
- return R.ok(search);
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。