• springboot整合ES 综合案例--京东搜索


    目录

    1.springboot整合ES

    2.进行相关对ES操作

    3. 综合案例--京东搜索


    1.springboot整合ES

    1.1创建一个Springboot工程并加入相关的依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>com.alibabagroupId>
    4. <artifactId>fastjsonartifactId>
    5. <version>1.2.75version>
    6. dependency>
    7. <dependency>
    8. <groupId>org.springframework.bootgroupId>
    9. <artifactId>spring-boot-starter-data-elasticsearchartifactId>
    10. dependency>
    11. <dependency>
    12. <groupId>org.springframework.bootgroupId>
    13. <artifactId>spring-boot-starter-webartifactId>
    14. dependency>
    15. <dependency>
    16. <groupId>org.projectlombokgroupId>
    17. <artifactId>lombokartifactId>
    18. <optional>trueoptional>
    19. dependency>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-testartifactId>
    23. <scope>testscope>
    24. dependency>
    25. dependencies>

    1.2 创建一个配置,获取ES工具类对象

    1. @Configuration
    2. public class ESConfig {
    3. //该对象可以对我们的ES进行相关的操作
    4. @Bean
    5. public RestHighLevelClient restHighLevelClient(){
    6. RestHighLevelClient client = new RestHighLevelClient(
    7. RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
    8. return client;
    9. }
    10. }

    2.进行相关对ES操作

    2.1 操作索引---创建索引

    1. //创建索引
    2. @Test
    3. void contextLoads() throws Exception{
    4. //该类把创建索引的信息都封装到该类中
    5. CreateIndexRequest createIndexRequest=new CreateIndexRequest("qy151-index");
    6. CreateIndexResponse createIndexResponse=client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    7. System.out.println(createIndexResponse.isShardsAcknowledged());
    8. }

    2.2操作索引--删除索引

    1. @Test
    2. //删除索引
    3. public void testDeleteIndex()throws Exception{
    4. DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("qy151-index");
    5. AcknowledgedResponse delete=client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
    6. System.out.println(delete.isAcknowledged());
    7. }

    2.3 索引操作--判断索引是否存在

    1. @Test
    2. //判断索引是否存在
    3. public void testIndexExists()throws Exception{
    4. GetIndexRequest getIndexRequest=new GetIndexRequest("qy151-index");
    5. boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
    6. System.out.println(exists);
    7. }

    2.4 操作文档---添加文档

    1. //添加文档
    2. public void testInsertDoc() throws Exception{
    3. IndexRequest indexRequest=new IndexRequest("qy151-index");
    4. indexRequest.id("1");//指定文档的id
    5. //指定文档的内容:String文档的json内容,XContentType xContentType:以什么格式
    6. indexRequest.source(JSON.toJSONString(new User("张三","北京",22)), XContentType.JSON);
    7. IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
    8. System.out.println(index.getResult());
    9. }

    2.5 查询文档--id

    1. //查询文档--id
    2. @Test
    3. public void testGetDoc() throws Exception{
    4. GetRequest getRequest=new GetRequest("qy151-index");
    5. getRequest.id("1");
    6. GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    7. String string = getResponse.getSourceAsString();
    8. User user=JSON.parseObject(string,User.class);
    9. Map sourceAsMap = getResponse.getSourceAsMap();
    10. System.out.println(sourceAsMap.get("name"));
    11. }

    2.6 判断索引文档是否存在

    1. //判断索引文档是否存在
    2. @Test
    3. public void testDocExist() throws Exception{
    4. GetRequest indexRequest=new GetRequest("qy151-index");
    5. indexRequest.id("2");
    6. boolean exists = client.exists(indexRequest, RequestOptions.DEFAULT);
    7. System.out.println(exists);
    8. }

    2.7 删除文档

    1. @Test
    2. //删除文档
    3. public void testDeleteDoc()throws Exception{
    4. DeleteRequest deleteRequest= new DeleteRequest("qy151-index");
    5. deleteRequest.id("1");
    6. DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
    7. System.out.println(delete.getResult());
    8. }

    2.8 修改文档

    1. @Test
    2. //修改文档
    3. public void testUpdateDoc() throws Exception{
    4. UpdateRequest updateRequest=new UpdateRequest("qy151-index","1");
    5. User user=new User();
    6. user.setName("黎明");
    7. updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
    8. UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
    9. System.out.println(update);
    10. }

    2.9 批量添加文档

    1. @Test
    2. //批量添加
    3. public void TestBuck()throws Exception{
    4. BulkRequest bulk=new BulkRequest("qy151-index");
    5. List list=new ArrayList<>();
    6. list.add(new User("2","张三","北京",22));
    7. list.add(new User("3","李四","上海",22));
    8. list.add(new User("4","王五","杭州",22));
    9. list.add(new User("5","赵六","广州",22));
    10. list.add(new User("6","孙琪","南京",22));
    11. //list.stream().forEach(item->bulk.add(new IndexRequest().id(item.getId()).source(JSON.toJSONString(item),XContentType.JSON)));
    12. for(User user:list){
    13. IndexRequest indexRequest=new IndexRequest();
    14. indexRequest.id(user.getId());
    15. indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
    16. bulk.add(indexRequest);
    17. }
    18. BulkResponse bulkResponse = client.bulk(bulk,RequestOptions.DEFAULT);
    19. System.out.println(bulkResponse.hasFailures());
    20. }

    2.10 复杂查询

    1. @Test
    2. //复杂查询
    3. public void testSearch() throws Exception{
    4. SearchRequest searchRequest=new SearchRequest("qy151-index");
    5. //创建条件对象
    6. SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
    7. //查询条件 使用TermQueryBuilder工具类创建
    8. TermQueryBuilder matchQuery = QueryBuilders.termQuery("name", "张");
    9. sourceBuilder.query(matchQuery);
    10. //分页
    11. sourceBuilder.from(0);
    12. sourceBuilder.size(1);
    13. //排序
    14. // sourceBuilder.sort("age");
    15. //高亮
    16. HighlightBuilder highlightBuilder=new HighlightBuilder();
    17. highlightBuilder.field("name");
    18. highlightBuilder.preTags("");
    19. highlightBuilder.postTags("");
    20. sourceBuilder.highlighter(highlightBuilder);
    21. searchRequest.source(sourceBuilder);
    22. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    23. System.out.println("总条数:"+searchResponse.getHits().getTotalHits().value);
    24. SearchHit[] hits = searchResponse.getHits().getHits();
    25. Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));
    26. Arrays.stream(hits).forEach(item-> System.out.println(item.getHighlightFields()));
    27. }

    3. 综合案例--京东搜索

    1. package com.wjk.utils;
    2. import com.wjk.entity.Product;
    3. import org.jsoup.Jsoup;
    4. import org.jsoup.nodes.Document;
    5. import org.jsoup.nodes.Element;
    6. import org.jsoup.select.Elements;
    7. import java.net.URL;
    8. import java.util.ArrayList;
    9. import java.util.List;
    10. public class HtmlParseUtil {
    11. public static void main(String[] args)throws Exception {
    12. String path="https://search.jd.com/Search?keyword=java";
    13. //Document整个网页
    14. Document document = Jsoup.parse(new URL(path), 30000);
    15. // System.out.println(document);
    16. Element j_goodsList = document.getElementById("J_goodsList");
    17. //System.out.println(j_goodsList);
    18. Elements li = j_goodsList.getElementsByTag("li");
    19. // List list=new ArrayList<>();
    20. for (Element element:li){
    21. //拿到所有价格
    22. String text = element.getElementsByClass("p-price").eq(0).text();
    23. String pname = element.getElementsByClass("p-name").eq(0).text();
    24. String img = element.getElementsByTag("img").eq(0).attr("data-lazy-img");
    25. System.out.println(img);
    26. }
    27. }
    28. }

     

  • 相关阅读:
    SpringFramework:Spring AOP
    nlp中如何数据增强
    手把手带你刷好题(牛客刷题④)
    YOLO家族再度升级——阿里达摩院DAMO-YOLO重磅来袭
    yolov7从环境配置到训练自己的数据集及人体姿态估计AlexeyAB版本
    主流接口测试框架对比
    《低代码指南》——维格云低代码应用管理
    Spring的创建和使用
    PostgreSQL位居第四,SQL Server表现差强人意,11月份数据库排行榜出炉
    小程序微信支付实践案例-JAVA
  • 原文地址:https://blog.csdn.net/qq_55682798/article/details/126369496