赞
踩
链接:https://pan.baidu.com/s/11ALrK8xiSWNLYzedu5H8FQ?pwd=y4z6
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.7.3</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.es</groupId>
- <artifactId>es</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>es</name>
- <description>es</description>
- <properties>
- <java.version>1.8</java.version>
- <elasticsearch.version>7.17.4</elasticsearch.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.alibaba.fastjson2</groupId>
- <artifactId>fastjson2</artifactId>
- <version>2.0.12</version>
- </dependency>
- <dependency>
- <!-- jsoup HTML parser library @ https://jsoup.org/ -->
- <groupId>org.jsoup</groupId>
- <artifactId>jsoup</artifactId>
- <version>1.15.3</version>
- </dependency>
- <!-- thymeleaf -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </exclude>
- </excludes>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- </project>
- package com.es.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 EsConfig {
-
- @Bean
- public RestHighLevelClient restHighLevelClient() {
- RestHighLevelClient client = new RestHighLevelClient(
- RestClient.builder(
- new HttpHost("192.168.1.102", 9200, "http")
- )
- );
- return client;
- }
-
- }
- package com.es.entity;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class Good {
- private String name;
- private String price;
- private String img;
- }
- package com.es.controller;
-
- import com.es.service.GoodService;
- 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 GoodController {
-
- @Autowired
- private GoodService goodService;
-
- @GetMapping("/createGood/{goodName}")
- public Boolean createGood(@PathVariable("goodName") String goodName) throws Exception {
- return goodService.createGood(goodName);
- }
-
- @GetMapping("/searchGood/{goodName}/{pageNumber}/{pageSize}")
- public List<Map<String, Object>> searchGood(@PathVariable("goodName") String goodName,
- @PathVariable("pageNumber") Integer pageNumber,
- @PathVariable("pageSize") Integer pageSize) throws Exception {
- List<Map<String, Object>> goodList = goodService.searchGood(goodName, pageNumber, pageSize);
- if (goodList.size()<pageSize) { // 如果查到的数据小于pageSize则拉取数据后重新查询
- boolean ok = goodService.createGood(goodName);
- if (ok) {
- goodList = goodService.searchGood(goodName,pageNumber,pageSize);
- }
- }
- return goodList;
- }
-
- }
- package com.es.service;
-
-
- import com.alibaba.fastjson2.JSON;
- import com.es.entity.Good;
- 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.core.TimeValue;
- 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.elasticsearch.xcontent.XContentType;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- 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 GoodService {
-
- @Autowired
- RestHighLevelClient client;
-
- /**
- * jsoup获取网页数据并存入ES
- */
- public boolean createGood(String goodName) throws Exception {
- ArrayList<Good> goodList = new ArrayList<>();
- // jsoup获取数据并存入集合
- Document document = Jsoup.connect("https://search.jd.com/Search?keyword="+goodName).get();
- Elements elements = document.getElementById("J_goodsList").getElementsByTag("li");
- for (Element e : elements) {
- String name = e.getElementsByClass("p-name").eq(0).text();
- String price = e.getElementsByClass("p-price").eq(0).text();
- String img = e.getElementsByTag("img").eq(0).attr("data-lazy-img");
- goodList.add(new Good(name, price, img));
- }
- // 集合数据存入ES的good_index索引下
- BulkRequest request = new BulkRequest();
- request.timeout("1m");
- for (Good good : goodList) {
- request.add(new IndexRequest("good_index").source(JSON.toJSONString(good), XContentType.JSON));
- }
- BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
- return !response.hasFailures();
- }
-
-
- /**
- * ES查询Good
- */
- public List<Map<String,Object>> searchGood(String goodName,int pageNumber,int pageSize) throws Exception {
- SearchRequest request = new SearchRequest("good_index");
-
- SearchSourceBuilder builder = new SearchSourceBuilder();
- // 超时时间
- builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
- // 设置分页
- builder.from((pageNumber-1)*pageSize);
- builder.size(pageSize);
- // 精确匹配
- builder.query(QueryBuilders.matchPhraseQuery("name", goodName));
- // 高亮显示
- HighlightBuilder hBuild = new HighlightBuilder();
- hBuild.field("name");
- hBuild.requireFieldMatch(false); // 单条结果只高亮一个
- hBuild.preTags("<span style='color:red'>");
- hBuild.postTags("</span>");
- builder.highlighter(hBuild);
-
- request.source(builder);
-
- SearchResponse response = client.search(request, RequestOptions.DEFAULT);
- // 高亮数据解析到结果数据中
- List<Map<String,Object>> goodList = new ArrayList<>();
- for (SearchHit hit : response.getHits()) {
- Map<String, Object> map = hit.getSourceAsMap();
- Map<String, HighlightField> highlightFields = hit.getHighlightFields();
- HighlightField field = highlightFields.get("name");
- if (field != null) {
- Text[] fragments = field.fragments();
- StringBuilder sb = new StringBuilder();
- for (Text fragment : fragments) {
- sb.append(fragment);
- }
- map.put("name", sb.toString());
- }
- goodList.add(map);
- }
-
- return goodList;
- }
-
- }
- package com.es.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
-
- @Controller
- public class IndexController {
-
- @GetMapping({"/","/index"})
- public String index(){
- return "index";
- }
-
- }
搜索不到结果会自动拉取数据
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。