elasticsearch|07-仿京东搜索

京东搜索:项目搭建 1、new springboot 导入依赖

com.alibaba fastjson 1.2.61 org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.experimental spring-native ${spring-native.version} org.springframework.boot spring-boot-devtools >runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test >test

2、application.yml
server.port=9090 #关闭 thymeleaf spring.thymeleaf.cache=false

3、引入前端素材(可以去gitee搜索)
4、controller
@Controller public class IndexController { @GetMapping({"/","/index"}) public String index(){ return "index"; } }

5、启动访问lcoalhost:9090
京东搜索:爬取数据
数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫!
1、导入依赖
org.jsoup jsoup 1.10.2

实体类
@Data @NoArgsConstructor @AllArgsConstructor @ToString public class Content {private String src; private String price; private String name; }

工具类
package com.kuang.utils; import com.kuang.pojo.Content; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.springframework.stereotype.Component; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class HtmlParseUtil{ public static List parseJd(String keyword) throws IOException { List list = new ArrayList<>(); String url = "https://search.jd.com/Search?keyword="+keyword+"&enc=utf-8"; Document root = Jsoup.parse(new URL(new String(url.getBytes(),"utf-8")), 30000); Element j_goodsList = root.getElementById("J_goodsList"); Elements li = j_goodsList.getElementsByTag("li"); for (Element element : li) { String src = https://www.it610.com/article/element.getElementsByTag("img").eq(0).attr("data-lazy-img"); String price = element.getElementsByClass("p-price").eq(0).text(); String name = element.getElementsByClass("p-name").eq(0).text(); list.add(new Content(src,price,name)); } return list; }public static void main(String[] args) throws IOException { List list = parseJd("java"); list.forEach(System.out::println); }}

京东搜索:业务编写 1、连接elasticsearch
package com.kuang.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; @Configuration public class ElasticSerachClientConfig {@Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1",9200,"http"))); return client; } }

2、controller
  • contentController
@RestController public class ContentController {@Autowired private ContentService contentService; @GetMapping("/parse/{keyword}") public Boolean parse(@PathVariable("keyword") String keyword) throws IOException { return contentService.parseContent(keyword); }@GetMapping("/search/{keyword}/{pageNo}/{pageSize}") public List> search(@PathVariable("keyword") String keyword ,@PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize ) throws IOException { returncontentService.searchPage(keyword, pageNo, pageSize); } }

3、service
package com.kuang.service; import com.alibaba.fastjson.JSON; import com.kuang.pojo.Content; import com.kuang.utils.HtmlParseUtil; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class ContentService {@Autowired private RestHighLevelClient restHighLevelClient; public Boolean parseContent(String keyword) throws IOException {List contents = HtmlParseUtil.parseJd(keyword); BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("2m"); for (int i = 0; i < contents.size(); i++) { bulkRequest.add(new IndexRequest("jd_goods") .source(JSON.toJSONString(contents.get(i)), XContentType.JSON)); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); return !bulk.hasFailures(); }public static void main(String[] args) throws IOException { new ContentService().parseContent("java"); }public List> searchPage(String keyword,int pageNo, int pageSize) throws IOException { if (pageNo <= 0) { pageNo = 1; }SearchRequest searchRequest = new SearchRequest("jd_goods"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.from(pageNo); sourceBuilder.size(pageSize); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", keyword); sourceBuilder.query(termQueryBuilder); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(sourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); List> list = new ArrayList<>(); for (SearchHit documentFields : searchResponse.getHits().getHits()) { list.add(documentFields.getSourceAsMap()); } return list; } }

4、utils
public class HtmlParseUtil{ public static List parseJd(String keyword) throws IOException { List list = new ArrayList<>(); String url = "https://search.jd.com/Search?keyword="+keyword+"&enc=utf-8"; Document root = Jsoup.parse(new URL(new String(url.getBytes(),"utf-8")), 30000); Element j_goodsList = root.getElementById("J_goodsList"); Elements li = j_goodsList.getElementsByTag("li"); for (Element element : li) { String src = https://www.it610.com/article/element.getElementsByTag("img").eq(0).attr("data-lazy-img"); String price = element.getElementsByClass("p-price").eq(0).text(); String name = element.getElementsByClass("p-name").eq(0).text(); list.add(new Content(src,price,name)); } return list; }public static void main(String[] args) throws IOException { List list = parseJd("java"); list.forEach(System.out::println); }}

新建索引jd_goods
访问localhost:9090/parse/java 把java数据填入进去
访问http://localhost:9090/search/java/1/10 展示java前10条数据
京东搜索:前后端交互 导入axios.js
vue.js
狂神说Java-ES仿京东实战 - 锐客网
{{item.price}}

>店铺: 狂神说Java
>月成交999笔 >评价 3
th:src="https://www.it610.com/article/@{/js/vue.min.js}"> th:src="https://www.it610.com/article/@{/js/axios.min.js}"> > new Vue({ el:'#app', data:{ keyword:'',//搜索关键字 results:[],//搜索结果 }, methods:{ searchPage(){ var keyword = this.keyword; console.log(keyword) axios.get("/search/"+keyword+"/1/20").then(res=>{ console.log(res.data) this.results = res.data; }) } } })

京东搜索:关键字高亮实现 升级版ContentService
记得修改前端的显示
@Service public class ContentService {@Autowired private RestHighLevelClient restHighLevelClient; public Boolean parseContent(String keyword) throws IOException {List contents = HtmlParseUtil.parseJd(keyword); BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("2m"); for (int i = 0; i < contents.size(); i++) { bulkRequest.add(new IndexRequest("jd_goods") .source(JSON.toJSONString(contents.get(i)), XContentType.JSON)); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); return !bulk.hasFailures(); }public static void main(String[] args) throws IOException { new ContentService().parseContent("java"); }public List> searchPage(String keyword,int pageNo, int pageSize) throws IOException { if (pageNo <= 0) { pageNo = 1; }SearchRequest searchRequest = new SearchRequest("jd_goods"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.from(pageNo); sourceBuilder.size(pageSize); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", keyword); sourceBuilder.query(termQueryBuilder); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //高亮 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field("name"); highlightBuilder.preTags(""); highlightBuilder.postTags(""); highlightBuilder.requireFieldMatch(false); //多个高亮显示 sourceBuilder.highlighter(highlightBuilder); searchRequest.source(sourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); List> list = new ArrayList<>(); for (SearchHit hit : searchResponse.getHits().getHits()) { //解析高亮的字段 Map highlightFields = hit.getHighlightFields(); HighlightField name = highlightFields.get("name"); Map sourceAsMap = hit.getSourceAsMap(); //原来的结果! //解析高亮的字段,将原来的字段换为我们高亮的字段即可! /* if (name!=null){ Text[] fragments = name.fragments(); String n_title=""; for (Text text : fragments) { n_title+=text; } sourceAsMap.put("name",n_title); } */ if (name!=null){ Text[] fragments = name.fragments(); StringBuilder stringBuilder = new StringBuilder(); for (Text fragment : fragments) { stringBuilder.append(fragment); } sourceAsMap.put("name",stringBuilder.toString()); } list.add(sourceAsMap); } return list; } }

【elasticsearch|07-仿京东搜索】完结撒花~

    推荐阅读