查找功能技术实现由es的模糊查找替换原先的mysql模糊查找

master
Bosen 2021-08-08 16:05:20 +08:00
parent 84443fa32a
commit 1d6a5556a8
7 changed files with 86 additions and 17 deletions

View File

@ -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>

View File

@ -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";
}
}

View File

@ -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>goodses</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;
}

View File

@ -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);
}

View File

@ -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");

View File

@ -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);
}
}

View File

@ -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: