赞
踩
es的基础知识
功能:爬取商城真实数据;存储到es中;拿去es中的数据并渲染到页面;搜索字段高亮显示
实体类:
package com.huang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content {
private String img;
private String price;
private String title;
}
工具类:
package com.huang.util; import com.huang.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.URL; import java.util.ArrayList; import java.util.List; @Component public class HtmlParseUtil { public List<Content> parseJD(String keywords) throws IOException { //获取请求: https://search.jd.com/Search?keyword=java&enc=utf-8&wq=java&pvid=f807c58b66dc4baab4c7ed71834c36be //前提,联网,ajax不能获得 String url = "https://search.jd.com/Search?keyword="+keywords+"&enc=utf-8"; //解析网页。(JIsoup返 回Document就是浏览器Document对象) Document document = Jsoup.parse(new URL(url), 30000); //所有你在js中可以使用的方法,这里都能用! Element element = document.getElementById("J_goodsList"); //System.out.println(element.html()); //获取所有的Li标签元素 Elements elements = element.getElementsByTag("li"); //实体类Content中封装所需要的值 List<Content> contents = new ArrayList<>(); //获取元素中的内容,这el就是每一 个Li标签了! for (Element el : elements) { //由于图片是延迟加载 //data-lazy-img String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img"); String price = el.getElementsByClass("p-price").eq(0).text(); String title = el.getElementsByClass("p-name").eq(0).text(); /*System.out.println("==============================="); System.out.println(img); System.out.println(price); System.out.println(title);*/ contents.add(new Content(img,price,title)); } return contents; } }
业务层:
package com.huang.service; import com.alibaba.fastjson.JSON; import com.huang.pojo.Content; import com.huang.util.HtmlParseUtil; 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; //业务编写 @Service public class ContentService { @Autowired //es客户端 private RestHighLevelClient restHighLevelClient; // 1.解析数据放入 es索引中 public Boolean parseContent(String keywords) throws Exception { //自定义爬虫工具,contents是我们获取到的页面值 List<Content> contents = new HtmlParseUtil().parseJD(keywords); /*// 1、创建索引请求 CreateIndexRequest request = new CreateIndexRequest("jd_goods"); // 2、客户端执行请求 IndicesClient,请求后获得响应 System.out.println("创建开始"); restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); System.out.println("创建成功");*/ BulkRequest bulkRequest = new BulkRequest(); //2秒后获取不到则失败 bulkRequest.timeout("2s"); 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(); } // 2.获取这些数据实现搜索功能 public List<Map<String, Object>> searchPage(String keyword, int pageNo, int pageSize) throws IOException { if (pageNo <= 1) { pageNo = 1; } //条件搜索 SearchRequest searchRequest = new SearchRequest("jd_goods"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //分页 sourceBuilder.from(pageNo); sourceBuilder.size(pageSize); //精准匹配 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword); sourceBuilder.query(termQueryBuilder); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //执行搜索 searchRequest.source(sourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); //解析结果 ArrayList<Map<String, Object>> list = new ArrayList<>(); for (SearchHit documentFields : searchResponse.getHits().getHits()) { list.add(documentFields.getSourceAsMap()); } return list; } // 2.获取这些数据实现搜索高亮功能 public List<Map<String, Object>> searchPageHighlightBuilder(String keyword, int pageNo, int pageSize) throws IOException { if (pageNo <= 1) { pageNo = 1; } //条件搜索 SearchRequest searchRequest = new SearchRequest("jd_goods"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //分页 sourceBuilder.from(pageNo); sourceBuilder.size(pageSize); //精准匹配 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword); sourceBuilder.query(termQueryBuilder); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //高亮 HighlightBuilder highlightBuilder = new HighlightBuilder(); //高亮的字段 highlightBuilder.field("title"); highlightBuilder.requireFieldMatch(false); //多个高亮显示 highlightBuilder.preTags("<span style='color:red'>"); highlightBuilder.postTags("</span>"); sourceBuilder.highlighter(highlightBuilder); //执行搜索 searchRequest.source(sourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); //解析结果 ArrayList<Map<String, Object>> list = new ArrayList<>(); for (SearchHit hit : searchResponse.getHits().getHits()) { //下面是进行高亮解析 //获取高亮字段 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); //获取是哪一个字段 HighlightField title = highlightFields.get("title"); Map<String, Object> sourceAsMap = hit.getSourceAsMap(); //这是原来的结果! //将原来的字段换为我们高亮的字段即可! if (title != null) { Text[] fragments = title.fragments(); String n_title = ""; for (Text text : fragments) { n_title += text; sourceAsMap.put("title", n_title); //高亮字段替换掉原来的内容即可! } list.add(sourceAsMap); } } return list; } }
部分视图层(这里是集成了vue)
<script th:src="@{/js/axios.min.js}"></script> <script th:src="@{/js/vue.min.js}"></script> <script> new Vue({ el: '#app', data: { keyword: "", //搜索的关键字 results: [] //搜索的结果 }, methods: { searchKey() { var keyword = this.keyword; console.log(keyword); //对接后端的接口 axios.get('search/' + keyword + "/1/10").then(response => { console.log(response); this.results = response.data;//绑定数据 }) } } }) </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。