成都创新互联网站制作重庆分公司

Day82-基于ElasticSearch的实战-仿京东搜素-创新互联

基于ElasticSearch的实战-仿京东搜素 1、创建springboot项目,添加相关依赖

在这里插入图片描述

创新互联公司主要从事网站设计制作、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务营山,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:189808205752、导入相关Maven依赖
4.0.0com.fues-jd0.0.1-SNAPSHOTes-jdes-jd1.87.6.2UTF-8UTF-82.3.7.RELEASEcom.alibabafastjson1.2.83org.springframework.bootspring-boot-starter-data-elasticsearchorg.springframework.bootspring-boot-starter-thymeleaforg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-devtoolsruntimetrueorg.springframework.bootspring-boot-configuration-processortrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtest  org.junit.vintage junit-vintage-engine  org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1  1.8 1.8 UTF-8  org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE  com.fu.EsJdApplication    repackage  repackage   
3、编写代码
  • config
package com.fu.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 ElasticSearchClientConfig {@Bean
    public RestHighLevelClient restHighLevelClient() {RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}
  • controller层
    • ContentController
package com.fu.controller;

import com.fu.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class ContentController {@Autowired
    private ContentService contentService;

    @GetMapping("/parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keyword) throws Exception {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 {//        return contentService.searchPage(keyword, pageNo, pageSize);
        return contentService.searchPageHighlightBuilder(keyword, pageNo, pageSize);
    }

}
  • IndexController
package com.fu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {@GetMapping({"/","/index"})
    public String index(){return "index";
    }
}
  • service层
package com.fu.service;

import com.alibaba.fastjson.JSON;
import com.fu.entity.Content;
import com.fu.utils.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.beans.factory.annotation.Qualifier;
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
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient Client;


    // 1.解析数据放入 es索引中
    public Boolean parseContent(String keywords) throws Exception {Listcontents = new HtmlParseUtil().parseJD(keywords);

        //把查询到的数据放入es索引库中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("5m");
        for (int i = 0; i< contents.size(); i++) {bulkRequest.add(
                    new IndexRequest("jd_goods")
                            .source(JSON.toJSONString(contents.get(i)), XContentType.JSON));
        }
        BulkResponse bulk = Client.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }



    // 2.获取这些数据实现搜索高亮功能
    public List>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("");//设置前缀
        highlightBuilder.postTags("");                //设置后缀
        sourceBuilder.highlighter(highlightBuilder);


        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = Client.search(searchRequest, RequestOptions.DEFAULT);
        //解析结果
        ArrayList>list = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {MaphighlightFields = hit.getHighlightFields();
            HighlightField title = highlightFields.get("title");
            MapsourceAsMap = 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;
    }

}
  • utils工具类
package com.fu.utils;

import com.fu.entity.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HtmlParseUtil {public ListparseJD(String keywords) throws Exception {//获取请求 https://search.jd.com/Search?keyword=java
        String url = "https://search.jd.com/Search?keyword="+keywords+"&enc=utf-8";
        //解析网页,Jsoup返回的document就是浏览器Document对象
        Document document = Jsoup.parse(new URL(url), 30000);
//        System.out.println(document.html());
        //获取商品列表
        Element j_goodsList = document.getElementById("J_goodsList");
        //获取所有的li元素
        Elements elements = document.getElementsByTag("li");
        //创建一个集合存储商品内容
        Listcontents = new ArrayList<>();
        //获取元素中的内容
        for (Element el : elements ){//由于图片是延迟加载
            //date-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();
//            String commit = el.getElementsByClass("p-commit").eq(0).text();
            String commit = "10万+";
            String shopnum = el.getElementsByClass("p-shop").eq(0).text();
            contents.add(new Content(img,price,title,commit,shopnum));
        }
        return contents;

    }

}
  • 实体类
package com.fu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content {private String img;      //图片
    private String price;    //价格
    private String title;    //标题
    private String commit;   //评价数量
    private String shopnum;  //商品编号
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享名称:Day82-基于ElasticSearch的实战-仿京东搜素-创新互联
分享地址:http://cxhlcq.com/article/dhhpoh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部