diff --git a/pom.xml b/pom.xml
index fb6c52e..3d89f0b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -147,6 +147,12 @@
tomcat-embed-jasper
provided
+
+
+ org.springframework.boot
+ spring-boot-starter-data-elasticsearch
+ 2.5.2
+
diff --git a/src/main/java/com/example/jieyue/common/controller/TestController.java b/src/main/java/com/example/jieyue/common/controller/TestController.java
index c328613..73180c6 100644
--- a/src/main/java/com/example/jieyue/common/controller/TestController.java
+++ b/src/main/java/com/example/jieyue/common/controller/TestController.java
@@ -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";
- }
}
diff --git a/src/main/java/com/example/jieyue/common/index/GoodsIndex.java b/src/main/java/com/example/jieyue/common/index/GoodsIndex.java
new file mode 100644
index 0000000..2f615a5
--- /dev/null
+++ b/src/main/java/com/example/jieyue/common/index/GoodsIndex.java
@@ -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;
+
+/**
+ * goods表对应的es索引
+ * @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;
+}
diff --git a/src/main/java/com/example/jieyue/common/repository/GoodsIndexRepository.java b/src/main/java/com/example/jieyue/common/repository/GoodsIndexRepository.java
new file mode 100644
index 0000000..4978c47
--- /dev/null
+++ b/src/main/java/com/example/jieyue/common/repository/GoodsIndexRepository.java
@@ -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;
+
+/**
+ * goods索引操作对象
+ * @author Bosen
+ * @date 2021/8/8 15:28
+ */
+@Repository
+public interface GoodsIndexRepository extends ElasticsearchRepository {
+ /**
+ * 通过名字或描述匹配
+ * @param keyword1 String
+ * @param keyword2 String
+ * @return List
+ */
+ List findByNameOrDescribe(String keyword1, String keyword2);
+}
diff --git a/src/main/java/com/example/jieyue/user/controller/UserSearchController.java b/src/main/java/com/example/jieyue/user/controller/UserSearchController.java
index c3bec64..561673a 100644
--- a/src/main/java/com/example/jieyue/user/controller/UserSearchController.java
+++ b/src/main/java/com/example/jieyue/user/controller/UserSearchController.java
@@ -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 goodsList = searchService.searchGoods(keyword);
+ List goodsList = searchService.esSearchGoods(keyword);
modelAndView.addObject("goodsList",goodsList);
modelAndView.setViewName("user/search/index");
diff --git a/src/main/java/com/example/jieyue/user/service/UserSearchService.java b/src/main/java/com/example/jieyue/user/service/UserSearchService.java
index 60e42cc..3088ea2 100644
--- a/src/main/java/com/example/jieyue/user/service/UserSearchService.java
+++ b/src/main/java/com/example/jieyue/user/service/UserSearchService.java
@@ -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;
+
+ /**
+ * mysql通过关键字模糊查找商品
*/
public List searchGoods(String keyword){
return goodsMapper.search(keyword);
}
+
+ /**
+ * es通过关键字模糊查找商品
+ */
+ public List esSearchGoods(String keyword) {
+ return goodsIndexRepository.findByNameOrDescribe(keyword, keyword);
+ }
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 82b1939..742bcc3 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -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: