1、 本篇目标
我们在一些电商网站搜索商品时,会搜索到许多相关商品,例如在某宝和某东搜索“Java”,会出现以下结果
某宝
某东
可以看到,两边都顺利的搜到符合要求的商品,某东甚至对每个商品第一个匹配的关键字进行了高亮处理。
我们接下来先把某东商品数据扒拉下来,将数据post到我们本地的ES中,再进行搜索。
在本篇中,最终我们达到以下效果
2、 准备工作 2.1 工程创建
在上一篇创建的空项目中创建一个子模块,在子模块中创建必要的package,删除这里用不上的文件
这里我偷懒不写dao、serviceImplement等,实际开发中一定要严格按照要求编写。
2.2 pom文件
4.0.0
com.goodwin
es-jingdong
0.0.1-SNAPSHOT
org.apache.maven.plugins
maven-compiler-plugin
7
7
es-jingdong
Demo project for Spring Boot
1.8
UTF-8
UTF-8
2.3.7.RELEASE
7.6.2
org.jsoup
jsoup
1.15.3
com.alibaba
fastjson
1.2.75
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-configuration-processor
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
3、 代码实现 修改 application.preperties配置文件
应用名称
spring.application.name=es-jingdong
应用服务 WEB 访问端口
server.port=8888
spring.thymeleaf.cache=false
访问测试
package com.goodwin.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@GetMapping({"/", "/index","/main"})
public String index(){
return "index";
}
}
RestHighLevelClient注入
根据上一篇,我们操作ES的API需要一个RestHighLevelClient 对象。
package com.goodwin.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Con