查找功能技术实现由es的模糊查找替换原先的mysql模糊查找
parent
84443fa32a
commit
1d6a5556a8
6
pom.xml
6
pom.xml
|
@ -147,6 +147,12 @@
|
|||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- elasticsearch -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,22 +1,8 @@
|
|||
package com.example.jieyue.common.controller;
|
||||
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
@RequestMapping("test")
|
||||
public String test() throws FileNotFoundException {
|
||||
|
||||
return ResourceUtils.getFile("classpath:/static").getPath()+"\\";
|
||||
}
|
||||
|
||||
@RequestMapping("/test/check")
|
||||
public String check(int id){
|
||||
System.out.println("id="+id);
|
||||
return "This is test/check";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.example.jieyue.common.index;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* <p>goods表对应的es索引</p>
|
||||
* @author Bosen
|
||||
* @date 2021/8/8 15:23
|
||||
*/
|
||||
@Document(indexName = "jieyue_sys_goods")
|
||||
@Data
|
||||
public class GoodsIndex {
|
||||
@Id
|
||||
private int id;
|
||||
@Field
|
||||
private String name;
|
||||
@Field
|
||||
private String describe;
|
||||
@Field
|
||||
private String img;
|
||||
@Field
|
||||
private BigDecimal price;
|
||||
@Field
|
||||
private int state;
|
||||
@Field
|
||||
private int merchant;
|
||||
@Field
|
||||
private int stock;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.example.jieyue.common.repository;
|
||||
|
||||
import com.example.jieyue.common.index.GoodsIndex;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>goods索引操作对象</p>
|
||||
* @author Bosen
|
||||
* @date 2021/8/8 15:28
|
||||
*/
|
||||
@Repository
|
||||
public interface GoodsIndexRepository extends ElasticsearchRepository<GoodsIndex, Integer> {
|
||||
/**
|
||||
* <p>通过名字或描述匹配</p>
|
||||
* @param keyword1 String
|
||||
* @param keyword2 String
|
||||
* @return List<GoodsIndex>
|
||||
*/
|
||||
List<GoodsIndex> findByNameOrDescribe(String keyword1, String keyword2);
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.example.jieyue.user.controller;
|
||||
|
||||
import com.example.jieyue.common.entity.SysGoods;
|
||||
import com.example.jieyue.common.index.GoodsIndex;
|
||||
import com.example.jieyue.user.service.UserSearchService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -22,7 +23,7 @@ public class UserSearchController {
|
|||
@RequestMapping("/user/search")
|
||||
public ModelAndView index(ModelAndView modelAndView,String keyword){
|
||||
// 获取返回的商品列表
|
||||
List<SysGoods> goodsList = searchService.searchGoods(keyword);
|
||||
List<GoodsIndex> goodsList = searchService.esSearchGoods(keyword);
|
||||
modelAndView.addObject("goodsList",goodsList);
|
||||
|
||||
modelAndView.setViewName("user/search/index");
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package com.example.jieyue.user.service;
|
||||
|
||||
import com.example.jieyue.common.entity.SysGoods;
|
||||
import com.example.jieyue.common.index.GoodsIndex;
|
||||
import com.example.jieyue.common.mapper.SysGoodsMapper;
|
||||
import com.example.jieyue.common.repository.GoodsIndexRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -11,10 +13,21 @@ import java.util.List;
|
|||
public class UserSearchService {
|
||||
@Autowired
|
||||
SysGoodsMapper goodsMapper;
|
||||
/*
|
||||
* 通过关键字模糊查找商品
|
||||
|
||||
@Autowired
|
||||
GoodsIndexRepository goodsIndexRepository;
|
||||
|
||||
/**
|
||||
* <p>mysql通过关键字模糊查找商品</p>
|
||||
*/
|
||||
public List<SysGoods> searchGoods(String keyword){
|
||||
return goodsMapper.search(keyword);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>es通过关键字模糊查找商品</p>
|
||||
*/
|
||||
public List<GoodsIndex> esSearchGoods(String keyword) {
|
||||
return goodsIndexRepository.findByNameOrDescribe(keyword, keyword);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,12 @@ spring:
|
|||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://${site-url}:3306/jieyue?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
|
||||
data:
|
||||
elasticsearch:
|
||||
cluster-nodes: 127.0.0.1:9200
|
||||
cluster-name: elasticsearch
|
||||
repositories:
|
||||
enabled: true
|
||||
mvc:
|
||||
static-path-pattern: /*/**
|
||||
mail:
|
||||
|
|
Loading…
Reference in New Issue