京东搜索--es和springboot加前端vue
目录
1.创建一个springboot项目
2.添加依赖
3.创建vue项目
3.1.打开cmd输入命令
3.2.创建vue项目
3.3.添加依赖和插件
3.3.1添加element插件
3.3.2.添加axios依赖
4.使用webStorm打开vue项目
4.1.引入axios
5.前端代码
6.后端代码
6.1.运行es和kibana
6.2.写一个工具类--用于获取京东的数据
6.3.创建返回共同数据工具类
6.4.将获取到京东的数据,添加到es中
6.4.1.controller层
6.4.2.service层
6.4.3.访问
6.4.4.测试一下
6.5.查询数据从es中
6.5.1.controller
6.5.2.service
6.5.3.访问
7.用前端访问
7.1.报错
7.2.解决问题
8.添加高亮
9.添加分页
9.1.前端
9.2.后端
9.2.1.controller层
9.2.2.service
10.添加分词
10.1.添加分词前先把自己的写的京东的索引给删除
10.2.在添加索引的时候设置分词
10.3.将我们下载的分词放入到plugis中
10.4.重启es
10.5.运行项目,添加数
1.创建一个springboot项目
2.添加依赖
<!--转json--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.79</version></dependency><dependency><groupId>repMaven.org.jsoup</groupId><artifactId>jsoup</artifactId><version> 1.10.2</version></dependency>
3.创建vue项目
3.1.打开cmd输入命令
vue ui
3.2.创建vue项目
3.3.添加依赖和插件
3.3.1添加element插件
3.3.2.添加axios依赖
4.使用webStorm打开vue项目
4.1.引入axios
5.前端代码
这个代码会爆红,但是不影响我们运行
<template><div class="page"><div id="app" class=" mallist tmall- page-not-market "><!-- 头部搜索 --><div id="header" class=" header-list-app"><div class="headerLayout"><div class="headerCon "><div class="header-extra"><!--搜索--><div id="mallSearch" class="mall-search"><form name="searchTop" class="mallSearch-form clearfix"><fieldset><legend>天猫搜索</legend><div class="mallSearch-input clearfix"><div class="s-combobox" id="s-combobox-685"><div class="s-combobox-input-wrap"><input v-model="keyword" type="text" autocomplete="off" id="mq"class="s-combobox-input" aria-haspopup="true"></div></div><button type="submit" @click.prevent="searchKey" id="searchbtn">搜索</button></div></fieldset></form><ul class="relKeyTop"><li><a>老闫说Java</a></li><li><a>老闫说前端</a></li><li><a>老闫说Linux</a></li><li><a>老闫说大数据</a></li><li><a>老闫聊理财</a></li></ul></div></div></div></div></div><!-- 商品详情页面 --><div id="content"><div class="main"><!-- 品牌分类 --><form class="navAttrsForm"><div class="attrs j_NavAttrs" style="display:block"><div class="brandAttr j_nav_brand"><div class="j_Brand attr"><div class="attrKey">品牌</div><div class="attrValues"><ul class="av-collapse row-2"><li><a href="#"> 老闫说 </a></li><li><a href="#"> Java </a></li></ul></div></div></div></div></form><!-- 排序规则 --><div class="filter clearfix"><a class="fSort fSort-cur">综合<i class="f-ico-arrow-d"></i></a><a class="fSort">人气<i class="f-ico-arrow-d"></i></a><a class="fSort">新品<i class="f-ico-arrow-d"></i></a><a class="fSort">销量<i class="f-ico-arrow-d"></i></a><a class="fSort">价格<i class="f-ico-triangle-mt"></i><i class="f-ico-triangle-mb"></i></a></div><!-- 商品详情 --><div class="view grid-nosku" ><div class="product" v-for="item in results"><div class="product-iWrap"><!--商品封面--><div class="productImg-wrap"><a class="productImg"><img :src="item.imgUrl"></a></div><!--价格--><p class="productPrice"><em>{{item.price}}</em></p><!--标题--><p class="productTitle"><a v-html="item.title"> </a></p><!-- 店铺名 --><div class="productShop"><span>店铺: 老闫说Java </span></div><!-- 成交信息 --><p class="productStatus"><span>月成交<em>999笔</em></span><span>评价 <a>3</a></span></p></div></div></div></div></div></div></div>
</template><script>export default {name: "jd",data(){return {keyword: '', // 搜索的关键字results:[] // 后端返回的结果}},methods:{searchKey(){this.axios.get('/product/search/'+this.keyword).then(response=>{console.log(response.data.data);this.results=response.data.data;})}}}
</script><style>/*** uncss> filename: http://localhost:9090/css/global.css ***/body,button,fieldset,form,h1,input,legend,li,p,ul{margin:0;padding:0}body,button,input{font:12px/1.5 tahoma,arial,"\5b8b\4f53";-ms-overflow-style:scrollbar}button,h1,input{font-size:100%}em{font-style:normal}ul{list-style:none}a{text-decoration:none}a:hover{text-decoration:underline}legend{color:#000}fieldset,img{border:0}#content,#header{margin-left:auto;margin-right:auto}html{zoom:expression(function(ele){ ele.style.zoom = "1"; document.execCommand("BackgroundImageCache", false, true); }(this))}@font-face{font-family:mui-global-iconfont;src:url(//at.alicdn.com/t/font_1401963178_8135476.eot);src:url(//at.alicdn.com/t/font_1401963178_8135476.eot?#iefix) format('embedded-opentype'),url(//at.alicdn.com/t/font_1401963178_8135476.woff) format('woff'),url(//at.alicdn.com/t/font_1401963178_8135476.ttf) format('truetype'),url(//at.alicdn.com/t/font_1401963178_8135476.svg#iconfont) format('svg')}#mallPage{width:auto;min-width:990px;background-color:transparent}#content{width:990px;margin:auto}#mallLogo{float:left;z-index:9;padding-top:28px;width:280px;height:64px;line-height:64px;position:relative}.page-not-market #mallLogo{width:400px}.clearfix:after,.clearfix:before,.headerCon:after,.headerCon:before{display:table;content:"";overflow:hidden}#mallSearch legend{display:none}.clearfix:after,.headerCon:after{clear:both}.clearfix,.headerCon{zoom:1}#mallPage #header{margin-top:-30px;width:auto;margin-bottom:0;min-width:990px;background:#fff}#header{height:122px;margin-top:-26px!important;background:#fff;min-width:990px;width:auto!important;position:relative;z-index:1000}#mallSearch #mq,#mallSearch fieldset,.mallSearch-input{position:relative}.headerLayout{width:990px;padding-top:26px;margin:0 auto}.header-extra{overflow:hidden}#mallSearch{float:right;padding-top:25px;width:390px;overflow:hidden}.mallSearch-form{border:solid #FF0036;border-width:3px 0 3px 3px}.mallSearch-input{background:#fff;height:30px}#mallSearch #mq{color:#000;margin:0;z-index:2;width:289px;height:20px;line-height:20px;padding:5px 3px 5px 5px;outline:0;border:none;font-weight:900;background:url(data:image/gif;base64,R0lGODlhAQADAJEAAObm5t3d3ff39wAAACH5BAAAAAAALAAAAAABAAMAAAICDFQAOw==) repeat-x;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}#mallSearch button{position:absolute;right:0;top:0;width:90px;border:0;font-size:16px;letter-spacing:4px;cursor:pointer;color:#fff;background-color:#FF0036;height:30px;overflow:hidden;font-family:'\5FAE\8F6F\96C5\9ED1',arial,"\5b8b\4f53"}#mallSearch .s-combobox{height:30px}#mallSearch .s-combobox .s-combobox-input:focus{outline:0}button::-moz-focus-inner{border:0;padding:0;margin:0}.page-not-market #mallSearch{width:540px!important}.page-not-market #mq{width:439px!important}/*** uncss> filename: http://localhost:9090/css/test.css ***/#mallSearch{float:none}.page-not-market #mallLogo{width:280px}.header-list-app #mallSearch{width:448px!important}.header-list-app #mq{width:347px!important}@media (min-width:1210px){#header .headerCon,#header .headerLayout,.main{width:1190px!important}.header-list-app #mallSearch{width:597px!important}.header-list-app #mq{width:496px!important}}@media (min-width:600px) and (max-width:800px) and (orientation:portrait){.pg .page{min-width:inherit!important}.pg #mallPage,.pg #mallPage #header{min-width:740px!important}.pg #header .headerCon,.pg #header .headerLayout,.pg .main{width:740px!important}.pg #mallPage #mallLogo{width:260px}.pg #header{min-width:inherit}.pg #mallSearch .mallSearch-input{padding-right:95px}.pg #mallSearch .s-combobox{width:100%!important}.pg #mallPage .header-list-app #mallSearch{width:auto!important}.pg #mallPage .header-list-app #mallSearch #mq{width:100%!important;padding:5px 0 5px 5px}}i{font-style:normal}.main,.page{position:relative}.page{overflow:hidden}@font-face{font-family:tm-list-font;src:url(//at.alicdn.com/t/font_1442456441_338337.eot);src:url(//at.alicdn.com/t/font_1442456441_338337.eot?#iefix) format('embedded-opentype'),url(//at.alicdn.com/t/font_1442456441_338337.woff) format('woff'),url(//at.alicdn.com/t/font_1442456441_338337.ttf) format('truetype'),url(//at.alicdn.com/t/font_1442456441_338337.svg#iconfont) format('svg')}::selection{background:rgba(0,0,0,.1)}*{-webkit-tap-highlight-color:rgba(0,0,0,.3)}b{font-weight:400}.page{background:#fff;min-width:990px}#content{margin:0!important;width:100%!important}.main{margin:auto;width:990px}.main img{-ms-interpolation-mode:bicubic}.fSort i{background:url(//img.alicdn.com/tfs/TB1XClLeAY2gK0jSZFgXXc5OFXa-165-206.png) 9999px 9999px no-repeat}#mallSearch .s-combobox{width:auto}::-ms-clear,::-ms-reveal{display:none}.attrKey{white-space:nowrap;text-overflow:ellipsis}.attrs{border-top:1px solid #E6E2E1}.attrs a{outline:0}.attr{background-color:#F7F5F5;border-color:#E6E2E1 #E6E2E1 #D1CCC7;border-style:solid solid dotted;border-width:0 1px 1px}.attr ul:after,.attr:after{display:block;clear:both;height:0;content:' '}.attrKey{float:left;padding:7px 0 0;width:10%;color:#B0A59F;text-indent:13px}.attrKey{display:block;height:16px;line-height:16px;overflow:hidden}.attrValues{position:relative;float:left;background-color:#FFF;width:90%;padding:4px 0 0;overflow:hidden}.attrValues ul{position:relative;margin-right:105px;margin-left:25px}.attrValues ul.av-collapse{overflow:hidden}.attrValues li{float:left;height:22px;line-height:22px}.attrValues li a{position:relative;color:#806F66;display:inline-block;padding:1px 20px 1px 4px;line-height:20px;height:20px;white-space:nowrap}.attrValues li a:hover{color:#ff0036;text-decoration:none}.brandAttr .attr{border:2px solid #D1CCC7;margin-top:-1px}.brandAttr .attrKey{padding-top:9px}.brandAttr .attrValues{padding-top:6px}.brandAttr .av-collapse{overflow:hidden;max-height:60px}.brandAttr li{margin:0 8px 8px 0}.brandAttr li a{text-overflow:ellipsis;overflow:hidden}.navAttrsForm{position:relative}.relKeyTop{padding:4px 0 0;margin-left:-13px;height:16px;overflow:hidden;width:100%}.relKeyTop li{display:inline-block;border-left:1px solid #ccc;line-height:1.1;padding:0 12px}.relKeyTop li a{color:#999}.relKeyTop li a:hover{color:#ff0036;text-decoration:none}.filter i{display:inline-block;overflow:hidden}.filter{margin:10px 0;padding:5px;position:relative;z-index:10;background:#faf9f9;color:#806f66}.filter i{position:absolute}.filter a{color:#806f66;cursor:pointer}.filter a:hover{color:#ff0036;text-decoration:none}.fSort{float:left;height:22px;line-height:20px;line-height:24px\9;border:1px solid #ccc;background-color:#fff;z-index:10}.fSort{position:relative}.fSort{display:inline-block;margin-left:-1px;overflow:hidden;padding:0 15px 0 5px}.fSort:hover,a.fSort-cur{color:#ff0036;background:#F1EDEC}.fSort i{top:6px;right:5px;width:7px;height:10px;line-height:10px}.fSort .f-ico-arrow-d{background-position:-22px -23px}.fSort-cur .f-ico-arrow-d,.fSort:hover .f-ico-arrow-d{background-position:-30px -23px}i.f-ico-triangle-mb,i.f-ico-triangle-mt{border:4px solid transparent;height:0;width:0}i.f-ico-triangle-mt{border-bottom:4px solid #806F66;top:2px}i.f-ico-triangle-mb{border-top:4px solid #806F66;border-width:3px\9;right:6px\9;top:12px}:root i.f-ico-triangle-mb{border-width:4px\9;right:5px\9}i.f-ico-triangle-mb,i.f-ico-triangle-mt{border:4px solid transparent;height:0;width:0}i.f-ico-triangle-mt{border-bottom:4px solid #806F66;top:2px}i.f-ico-triangle-mb{border-top:4px solid #806F66;border-width:3px\9;right:6px\9;top:12px}:root i.f-ico-triangle-mb{border-width:4px\9;right:5px\9}.view:after{clear:both;content:' '}.productImg,.productPrice em b{vertical-align:middle}.product{position:relative;float:left;padding:0;margin:0 0 20px;line-height:1.5;overflow:visible;z-index:1}.product:hover{overflow:visible;z-index:3;background:#fff}.product-iWrap{position:absolute;background-color:#fff;margin:0;padding:4px 4px 0;font-size:0;border:1px solid #f5f5f5;border-radius:3px}.product-iWrap *{font-size:12px}.product:hover .product-iWrap{height:auto;margin:-3px;border:4px solid #ff0036;border-radius:0;-webkit-transition:border-color .2s ease-in;-moz-transition:border-color .2s ease-in;-ms-transition:border-color .2s ease-in;-o-transition:border-color .2s ease-in;transition:border-color .2s ease-in}.productPrice,.productShop,.productStatus,.productTitle{display:block;overflow:hidden;margin-bottom:3px}.view:after{display:block}.view{margin-top:10px}.view:after{height:0}.productImg-wrap{display:table;table-layout:fixed;height:210px;width:100%;padding:0;margin:0 0 5px}.productImg-wrap a,.productImg-wrap img{max-width:100%;max-height:210px}.productImg{display:table-cell;width:100%;text-align:center}.productImg img{display:block;margin:0 auto}.productPrice{font-family:arial,verdana,sans-serif!important;color:#ff0036;font-size:14px;height:30px;line-height:30px;margin:0 0 5px;letter-spacing:normal;overflow:inherit!important;white-space:nowrap}.productPrice *{height:30px}.productPrice em{float:left;font-family:arial;font-weight:400;font-size:20px;color:#ff0036}.productPrice em b{margin-right:2px;font-weight:700;font-size:14px}.productTitle{display:block;color:#666;height:14px;line-height:12px;margin-bottom:3px;word-break:break-all;font-size:0;position:relative}.productTitle *{font-size:12px;font-family:\5FAE\8F6F\96C5\9ED1;line-height:14px}.productTitle a{color:#333}.productTitle a:hover{color:#ff0036!important}.productTitle a:visited{color:#551A8B!important}.product:hover .productTitle{height:14px}.productShop{position:relative;height:22px;line-height:20px;margin-bottom:5px;color:#999;white-space:nowrap;overflow:visible}.productStatus{position:relative;height:32px;border:none;border-top:1px solid #eee;margin-bottom:0;color:#999}.productStatus span{float:left;display:inline-block;border-right:1px solid #eee;width:39%;padding:10px 1px;margin-right:6px;line-height:12px;text-align:left;white-space:nowrap}.productStatus a,.productStatus em{margin-top:-8px;font-family:arial;font-size:12px;font-weight:700}.productStatus em{color:#b57c5b}.productStatus a{color:#38b}.productImg-wrap{position:relative}.product-iWrap{min-height:98%;width:210px}.view{padding-left:5px;padding-right:5px}.view{width:1023px}.view .product{width:220px;margin-right:33px}@media (min-width:1210px){.view{width:1210px;padding-left:5px;padding-right:5px}.view .product{width:220px;margin-right:20px}}@media (min-width:600px) and (max-width:800px) and (orientation:portrait){.view{width:775px;padding-left:5px;padding-right:5px}.view .product{width:220px;margin-right:35px}}.product{height:372px}.grid-nosku .product{height:333px}</style>
6.后端代码
6.1.运行es和kibana
6.2.写一个工具类--用于获取京东的数据
public class HtmlParseUtil {public List<Product> parseJd (String keyword)throws Exception{String path="https://search.jd.com/Search?keyword="+keyword;//Document整个网页对象Document parse = Jsoup.parse(new URL(path), 30000);//获取商品Element j_goodsList = parse.getElementById("J_goodsList");//获取每一个商品Elements li = j_goodsList.getElementsByTag("li");List<Product> list=new ArrayList<>();//循环for (Element element:li) {//获取下边为0的价格String p_price = element.getElementsByClass("p-price").eq(0).text();//获取商品的名字String p_name = element.getElementsByClass("p-name").eq(0).text();//System.out.println(p_name+""+p_price);//获取商品的图片 attr--属性String img = element.getElementsByTag("img").eq(0).attr("data-lazy-img");list.add(new Product(p_name,p_price,img));}return list;}
}
6.3.创建返回共同数据工具类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommentResult {private int code;private String msg;private Object data;
}
6.4.将获取到京东的数据,添加到es中
6.4.1.controller层
@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;//将京东的数据导入到es中@GetMapping("/export/{keyword}")public CommentResult product(@PathVariable String keyword) throws Exception {return productService.export(keyword);}
}
6.4.2.service层
package com.guan.service;import com.alibaba.fastjson.JSON;
import com.guan.entity.Product;
import com.guan.util.CommentResult;
import com.guan.util.HtmlParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;import java.util.List;/*** TODO** @author lenovo* @version 1.0* @since 2022-08-17 11:42:53*/
@Service
public class ProductService {@Autowired//用来操作文档的private RestHighLevelClient client;//将京东的数据导入到es中public CommentResult export(String keyword) throws Exception{//调用工具类List<Product> list = HtmlParseUtil.parseJd(keyword);//数据量大,使用批量添加 bulk//先判断一下索引存在不存在GetIndexRequest getIndexRequest=new GetIndexRequest("jd");boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);System.out.println(exists);//如果没有索引,创建一个索引if (!exists){CreateIndexRequest createIndexRequest=new CreateIndexRequest("jd");client.indices().create(createIndexRequest,RequestOptions.DEFAULT);}//有索引就批量添加文档//创建一个bulkRequest对象BulkRequest bulkRequest=new BulkRequest("jd");//添加数据for (Product product:list) {IndexRequest indexRequest=new IndexRequest();indexRequest.source(JSON.toJSONString(product), XContentType.JSON);bulkRequest.add(indexRequest);}BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);//判断是否报错 报错返回的true 不报错返回的是falseif (bulk.hasFailures()) {return new CommentResult(5000,"添加失败",null);}return new CommentResult(2000,"添加成功",null);}
}
6.4.3.访问
注意不要写中文会乱码
6.4.4.测试一下
6.5.查询数据从es中
6.5.1.controller
//查询数据从es中@GetMapping("/search/{keywaord}")public CommentResult search(@PathVariable String keyword)throws Exception{return productService.search(keyword);}
6.5.2.service
//根据关键字搜索public CommentResult search(String keyword) throws Exception{//创建一个搜索请求对象SearchRequest searchRequest=new SearchRequest("jd");//创建一个条件对象SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//查询构造器TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword);searchSourceBuilder.query(termQueryBuilder);//将条件对象放入到搜索请求对象中searchRequest.source(searchSourceBuilder);SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);//将结果封装到hits中SearchHits hits = search.getHits();//获取总条数long value = hits.getTotalHits().value;//返回的是commentResult对象,但是在es中查询的时候是map类型List<Map<String,Object>> list=new ArrayList<>();SearchHit[] hits1 = hits.getHits();for (SearchHit searchHit:hits1) {//循环的时候拿到mapMap<String, Object> map = searchHit.getSourceAsMap();list.add(map);}return new CommentResult(2000,"查询成功",list);}
6.5.3.访问
7.用前端访问
7.1.报错
7.2.解决问题
在controller层添加@CrossOrigin注解
8.添加高亮
//根据关键字搜索public CommentResult search(String keyword) throws Exception{//创建一个搜索请求对象SearchRequest searchRequest=new SearchRequest("jd");//创建一个条件对象SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//查询构造器TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword);searchSourceBuilder.query(termQueryBuilder);//设置高亮HighlightBuilder highlightBuilder=new HighlightBuilder();//设置那个列高亮highlightBuilder.field("title");//设置高亮的前置标签highlightBuilder.preTags("<font color='red'>");//设置高亮的后置标签highlightBuilder.postTags("</font>");//将高亮给条件对象searchSourceBuilder.highlighter(highlightBuilder);//将条件对象放入到搜索请求对象中searchRequest.source(searchSourceBuilder);SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);//将结果封装到hits中SearchHits hits = search.getHits();//获取总条数long value = hits.getTotalHits().value;//返回的是commentResult对象,但是在es中查询的时候是map类型List<Map<String,Object>> list=new ArrayList<>();SearchHit[] hits1 = hits.getHits();for (SearchHit searchHit:hits1) {//循环的时候拿到mapMap<String, Object> map = searchHit.getSourceAsMap();//获取高亮的字段Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();//获取文本HighlightField title = highlightFields.get("title");Text[] fragments = title.getFragments();StringBuffer sb=new StringBuffer();for (Text text:fragments) {sb.append(text);}map.put("title",sb);list.add(map);}return new CommentResult(2000,"查询成功",list);}
9.添加分页
es默认每页显示10条
9.1.前端
<!--分页@size-change:页面显示条数改变是触发的事件@current-change:页码改变时触发的事件current-page:当期的页面page-sizes:接受一个整形的数组,数组元素为显示的选择每页显示的个数page-size:每页显示的页数total:总条数--><el-pagination@size-change="handleSizeChange"@current-change="handleCurrentChange":current-page="currentPage":page-sizes="pageSizes":page-size="pageSize":total="total"layout="total, sizes, prev, pager, next, jumper"></el-pagination>
<script>export default {name: "jd",data(){return {//当前显示页面变量currentPage: 1,//接受一个整形的数组,数组元素为显示的选择每页显示的个数pageSizes: [5, 10, 15, 20],//每页显示的页数pageSize: 5,//总条数total: 0,keyword: '', // 搜索的关键字results:[] // 后端返回的结果}},methods:{handleSizeChange(val) {//每页显示的条数大小改变时触发的方法this.pageSize = val;this.searchKey();},//页码改变时触发的方法handleCurrentChange(val) {this.currentPage = val;this.searchKey();},searchKey(){this.axios.get('/product/search/'+this.keyword+"/"+this.currentPage+"/"+this.pageSize).then(response=>{console.log(response.data)this.total=response.data.data[0].total;this.results=response.data.data;})}}}
9.2.后端
9.2.1.controller层
//查询数据从es中@GetMapping("/search/{keyword}/{currentPage}/{pageSize}")public CommentResult search(@PathVariable String keyword,@PathVariable Integer currentPage,@PathVariable Integer pageSize)throws Exception{return productService.search(keyword,currentPage,pageSize);}
9.2.2.service
//根据关键字搜索public CommentResult search(String keyword,Integer currentPage,Integer pageSize) throws Exception{//创建一个搜索请求对象SearchRequest searchRequest=new SearchRequest("jd");//创建一个条件对象SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//查询构造器TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword);searchSourceBuilder.query(termQueryBuilder);//设置高亮HighlightBuilder highlightBuilder=new HighlightBuilder();//设置那个列高亮highlightBuilder.field("title");//设置高亮的前置标签highlightBuilder.preTags("<font color='red'>");//设置高亮的后置标签highlightBuilder.postTags("</font>");//将高亮给条件对象searchSourceBuilder.highlighter(highlightBuilder);//设置分页//从第几个开始出查询searchSourceBuilder.from((currentPage-1)*pageSize);//每页显示多少条searchSourceBuilder.size(pageSize);//将条件对象放入到搜索请求对象中searchRequest.source(searchSourceBuilder);SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);//将结果封装到hits中SearchHits hits = search.getHits();//获取总条数long value = hits.getTotalHits().value;//返回的是commentResult对象,但是在es中查询的时候是map类型List<Map<String,Object>> list=new ArrayList<>();SearchHit[] hits1 = hits.getHits();for (SearchHit searchHit:hits1) {//循环的时候拿到mapMap<String, Object> map = searchHit.getSourceAsMap();//获取高亮的字段Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();//获取文本HighlightField title = highlightFields.get("title");Text[] fragments = title.getFragments();StringBuffer sb=new StringBuffer();for (Text text:fragments) {sb.append(text);}map.put("title",sb);//条件分页map.put("total",value);list.add(map);}return new CommentResult(2000,"查询成功",list);}
10.添加分词
10.1.添加分词前先把自己的写的京东的索引给删除
不删除的话,重启es的时候,es会打不开
10.2.在添加索引的时候设置分词
PUT /jd
{"mappings":{"properties": {"title":{"type": "text","analyzer": "ik_max_word"},"price":{"type": "keyword"},"imgUrl":{"type": "keyword"}}}}
注意:title里面的数据跟数据库的数据是一样的
10.3.将我们下载的分词放入到plugis中
10.4.重启es
10.5.运行项目,添加数据
京东搜索--es和springboot加前端vue相关推荐
- 京东搜索--es+springboot+vue
目录 1. 创建一个springboot项目编辑 2. 添加依赖 3. 创建vue项目 3.1 打开cmd输入命令 3.2 创建vue项目 3.3 添加依赖和插件 3.3.1 添加element插件 ...
- 后端SpringBoot和前端vue
前端框架:vue 前端的主流是js,vue是js库,js库还有angular.react,过时的有jQuery; 组件库也就是css样式的封装库,其中有element.Bootstrap 后端框架:S ...
- Java前端笔记-后端Springboot,前端vue,Nginx使前后端分离
目录 基本概念 代码实例 基本概念 这是一个很6的模式,以目前本人C++ Qt的技术,是做不出的,但Java已经有雏形了. 后端采用Spring Boot主要是回json数据,如下所示: 这里的数据都 ...
- Java项目:实现个人博客系统(java+springboot+mybatis+redis+vue+elementui+Mysql)
源码获取:博客首页 "资源" 里下载! springboot+mybatis+前端vue,使用前后端分离架构实现的个人博客系统,共7个模块,首页,写博客,博客详情页,评论管理,文章 ...
- ES整合SpringBoot并实现京东搜索
目录 1.springboot整合ES 1.1 添加依赖 1.2 创建一个配置,获取ES工具类对象. 1.3 进行相关对ES操作 1.3.1 操作索引---创建索引 1.3.2 操作索引--删除索引 ...
- SpringBoot集成ES+京东搜索
SpringBoot集成elasticsearch 引入依赖 <dependency><groupId>com.alibaba</groupId><artif ...
- 2021最新版 ElasticSearch 7.6.1 教程详解 爬虫jsoup+es模拟京东搜索(狂神说)
文章目录 一.ElasticSearch 简介 1.了解创始人 Doug Cutting 2.Lucene 简介 3.ElasticSearch 简介 4.ElasticSearch 和 Solr 的 ...
- SpringBoot后端框架整合前端Vue系统框架
SpringBoot后端框架整合前端Vue系统框架 本节内容服务于SpringBoot + Vue 搭建 JavaWeb 增删改查项目. 在IDEA中整合前后端框架,实现前后端分离,人不分离,方便项目 ...
- 前端vue js 高德地图实现雷达扫描加载,借鉴百度地图等方法,采用Canvas解决雷达背景透明度问题,解决Canvas动态指针扫描造成浏览器卡顿问题
前端vue js 高德地图实现雷达扫描加载,借鉴百度地图等方法,采用Canvas解决雷达背景透明度问题,解决Canvas动态指针扫描造成浏览器卡顿问题 经过3天的尝试,借鉴了好几个博客的思路,开发了一 ...
最新文章
- php使用pdo操作mysql数据库实例_php5使用pdo连接数据库实例
- 禁止解析某目录的php,限制访问user_agent,php相关配置
- SQL Server中常用的SQL语句
- map的key可以试一个数组吗?_【自考】数据结构第三章,数组,期末不挂科指南,第5篇...
- IDEA全文搜索排除无用的文件夹
- Eclipse2020版本:pom.xml第一行报错:Could not initialize class org.apache.maven.plugin.war.
- python-基础回顾
- Asp.NET的DESAES加密算法(转载)
- spark:SparkUI界面
- 装双系统?不需要!教你在iMac上流畅使用Windows!
- 【故障处理】ORA-19809错误处理
- nsga2 matlab,NSGA2算法特征选择MATLAB实现(多目标)
- STM32之红外接收
- 海康威视存储服务器的作用,海康存储服务器CVR存储方式配置说明
- Unmapped Spring configuration files found. Please configure Spring facet
- 轻松处理 针式打印机故障解决方法
- 2022大淘宝技术工程师推荐书单
- Fabric官方教程(release 2.2)翻译及总结——使用CouchDB
- 【陪客户领导吃饭四部曲】
- 在win10更新的时候出现0x80240004错误代码怎么解决。
热门文章
- centos7默认网卡配置文件_centos 7 bootproto CentOS 7下修改默认网卡名为eth0的两种方法...
- 许多大学生喜欢玩计算机游戏用英语怎么说,2017年12月英语四级作文范文:学生迷恋游戏...
- set 有序吗js_JavaScript Set 集合
- 怎样查询某一支股票历史分红情况
- html背景对联效果-恭喜新禧,web前端开发 —— 一个对联效果
- 游戏蓝牙耳机哪款比较好用?延迟超低的游戏蓝牙耳机推荐
- 相机标定 - (01) - 相机标定简介
- 如何在产品开发中讨论概念设计?
- 华为鸿蒙发布现场,华为P50系列亮相鸿蒙发布会!但发布时间仍未确定
- 腾讯云 - 第三方服务商 接口, 对接人脸识别接口