当前位置:   article > 正文

ES入门开发_es 开发

es 开发

链接:https://pan.baidu.com/s/11ALrK8xiSWNLYzedu5H8FQ?pwd=y4z6

  1. 知识准备
    https://blog.csdn.net/tongxin_tongmeng/article/details/126896009
  2. pom.xml
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.7.3</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.es</groupId>
    12. <artifactId>es</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>es</name>
    15. <description>es</description>
    16. <properties>
    17. <java.version>1.8</java.version>
    18. <elasticsearch.version>7.17.4</elasticsearch.version>
    19. </properties>
    20. <dependencies>
    21. <dependency>
    22. <groupId>org.springframework.boot</groupId>
    23. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.springframework.boot</groupId>
    27. <artifactId>spring-boot-starter-web</artifactId>
    28. </dependency>
    29. <dependency>
    30. <groupId>org.springframework.boot</groupId>
    31. <artifactId>spring-boot-devtools</artifactId>
    32. <scope>runtime</scope>
    33. <optional>true</optional>
    34. </dependency>
    35. <dependency>
    36. <groupId>org.springframework.boot</groupId>
    37. <artifactId>spring-boot-configuration-processor</artifactId>
    38. <optional>true</optional>
    39. </dependency>
    40. <dependency>
    41. <groupId>org.projectlombok</groupId>
    42. <artifactId>lombok</artifactId>
    43. <optional>true</optional>
    44. </dependency>
    45. <dependency>
    46. <groupId>org.springframework.boot</groupId>
    47. <artifactId>spring-boot-starter-test</artifactId>
    48. <scope>test</scope>
    49. </dependency>
    50. <dependency>
    51. <groupId>com.alibaba.fastjson2</groupId>
    52. <artifactId>fastjson2</artifactId>
    53. <version>2.0.12</version>
    54. </dependency>
    55. <dependency>
    56. <!-- jsoup HTML parser library @ https://jsoup.org/ -->
    57. <groupId>org.jsoup</groupId>
    58. <artifactId>jsoup</artifactId>
    59. <version>1.15.3</version>
    60. </dependency>
    61. <!-- thymeleaf -->
    62. <dependency>
    63. <groupId>org.springframework.boot</groupId>
    64. <artifactId>spring-boot-starter-thymeleaf</artifactId>
    65. </dependency>
    66. </dependencies>
    67. <build>
    68. <plugins>
    69. <plugin>
    70. <groupId>org.springframework.boot</groupId>
    71. <artifactId>spring-boot-maven-plugin</artifactId>
    72. <configuration>
    73. <excludes>
    74. <exclude>
    75. <groupId>org.projectlombok</groupId>
    76. <artifactId>lombok</artifactId>
    77. </exclude>
    78. </excludes>
    79. </configuration>
    80. </plugin>
    81. </plugins>
    82. </build>
    83. </project>
  3. 配置类
    1. package com.es.config;
    2. import org.apache.http.HttpHost;
    3. import org.elasticsearch.client.RestClient;
    4. import org.elasticsearch.client.RestHighLevelClient;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. @Configuration
    8. public class EsConfig {
    9. @Bean
    10. public RestHighLevelClient restHighLevelClient() {
    11. RestHighLevelClient client = new RestHighLevelClient(
    12. RestClient.builder(
    13. new HttpHost("192.168.1.102", 9200, "http")
    14. )
    15. );
    16. return client;
    17. }
    18. }
  4. 实体类
    1. package com.es.entity;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. @Data
    6. @NoArgsConstructor
    7. @AllArgsConstructor
    8. public class Good {
    9. private String name;
    10. private String price;
    11. private String img;
    12. }
  5. 控制层
    1. package com.es.controller;
    2. import com.es.service.GoodService;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.PathVariable;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import java.io.IOException;
    8. import java.util.List;
    9. import java.util.Map;
    10. @RestController
    11. public class GoodController {
    12. @Autowired
    13. private GoodService goodService;
    14. @GetMapping("/createGood/{goodName}")
    15. public Boolean createGood(@PathVariable("goodName") String goodName) throws Exception {
    16. return goodService.createGood(goodName);
    17. }
    18. @GetMapping("/searchGood/{goodName}/{pageNumber}/{pageSize}")
    19. public List<Map<String, Object>> searchGood(@PathVariable("goodName") String goodName,
    20. @PathVariable("pageNumber") Integer pageNumber,
    21. @PathVariable("pageSize") Integer pageSize) throws Exception {
    22. List<Map<String, Object>> goodList = goodService.searchGood(goodName, pageNumber, pageSize);
    23. if (goodList.size()<pageSize) { // 如果查到的数据小于pageSize则拉取数据后重新查询
    24. boolean ok = goodService.createGood(goodName);
    25. if (ok) {
    26. goodList = goodService.searchGood(goodName,pageNumber,pageSize);
    27. }
    28. }
    29. return goodList;
    30. }
    31. }
  6. 业务层
    1. package com.es.service;
    2. import com.alibaba.fastjson2.JSON;
    3. import com.es.entity.Good;
    4. import org.elasticsearch.action.bulk.BulkRequest;
    5. import org.elasticsearch.action.bulk.BulkResponse;
    6. import org.elasticsearch.action.index.IndexRequest;
    7. import org.elasticsearch.action.search.SearchRequest;
    8. import org.elasticsearch.action.search.SearchResponse;
    9. import org.elasticsearch.client.RequestOptions;
    10. import org.elasticsearch.client.RestHighLevelClient;
    11. import org.elasticsearch.common.text.Text;
    12. import org.elasticsearch.core.TimeValue;
    13. import org.elasticsearch.index.query.QueryBuilders;
    14. import org.elasticsearch.index.query.TermQueryBuilder;
    15. import org.elasticsearch.search.SearchHit;
    16. import org.elasticsearch.search.builder.SearchSourceBuilder;
    17. import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
    18. import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
    19. import org.elasticsearch.xcontent.XContentType;
    20. import org.jsoup.Jsoup;
    21. import org.jsoup.nodes.Document;
    22. import org.jsoup.nodes.Element;
    23. import org.jsoup.select.Elements;
    24. import org.springframework.beans.factory.annotation.Autowired;
    25. import org.springframework.stereotype.Service;
    26. import java.io.IOException;
    27. import java.util.ArrayList;
    28. import java.util.List;
    29. import java.util.Map;
    30. import java.util.concurrent.TimeUnit;
    31. @Service
    32. public class GoodService {
    33. @Autowired
    34. RestHighLevelClient client;
    35. /**
    36. * jsoup获取网页数据并存入ES
    37. */
    38. public boolean createGood(String goodName) throws Exception {
    39. ArrayList<Good> goodList = new ArrayList<>();
    40. // jsoup获取数据并存入集合
    41. Document document = Jsoup.connect("https://search.jd.com/Search?keyword="+goodName).get();
    42. Elements elements = document.getElementById("J_goodsList").getElementsByTag("li");
    43. for (Element e : elements) {
    44. String name = e.getElementsByClass("p-name").eq(0).text();
    45. String price = e.getElementsByClass("p-price").eq(0).text();
    46. String img = e.getElementsByTag("img").eq(0).attr("data-lazy-img");
    47. goodList.add(new Good(name, price, img));
    48. }
    49. // 集合数据存入ES的good_index索引下
    50. BulkRequest request = new BulkRequest();
    51. request.timeout("1m");
    52. for (Good good : goodList) {
    53. request.add(new IndexRequest("good_index").source(JSON.toJSONString(good), XContentType.JSON));
    54. }
    55. BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
    56. return !response.hasFailures();
    57. }
    58. /**
    59. * ES查询Good
    60. */
    61. public List<Map<String,Object>> searchGood(String goodName,int pageNumber,int pageSize) throws Exception {
    62. SearchRequest request = new SearchRequest("good_index");
    63. SearchSourceBuilder builder = new SearchSourceBuilder();
    64. // 超时时间
    65. builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    66. // 设置分页
    67. builder.from((pageNumber-1)*pageSize);
    68. builder.size(pageSize);
    69. // 精确匹配
    70. builder.query(QueryBuilders.matchPhraseQuery("name", goodName));
    71. // 高亮显示
    72. HighlightBuilder hBuild = new HighlightBuilder();
    73. hBuild.field("name");
    74. hBuild.requireFieldMatch(false); // 单条结果只高亮一个
    75. hBuild.preTags("<span style='color:red'>");
    76. hBuild.postTags("</span>");
    77. builder.highlighter(hBuild);
    78. request.source(builder);
    79. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    80. // 高亮数据解析到结果数据中
    81. List<Map<String,Object>> goodList = new ArrayList<>();
    82. for (SearchHit hit : response.getHits()) {
    83. Map<String, Object> map = hit.getSourceAsMap();
    84. Map<String, HighlightField> highlightFields = hit.getHighlightFields();
    85. HighlightField field = highlightFields.get("name");
    86. if (field != null) {
    87. Text[] fragments = field.fragments();
    88. StringBuilder sb = new StringBuilder();
    89. for (Text fragment : fragments) {
    90. sb.append(fragment);
    91. }
    92. map.put("name", sb.toString());
    93. }
    94. goodList.add(map);
    95. }
    96. return goodList;
    97. }
    98. }
  7. 访问入口
    1. package com.es.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. @Controller
    5. public class IndexController {
    6. @GetMapping({"/","/index"})
    7. public String index(){
    8. return "index";
    9. }
    10. }
  8. 搜索页面
    搜索不到结果会自动拉取数据

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/919814
推荐阅读
相关标签
  

闽ICP备14008679号