SpringBoot2.6.1 elasticsearch7.1.5 Vue

SpringBoot2.6.1 elasticsearch7.1.5 Vue,第1张

SpringBoot2.6.1 elasticsearch7.1.5 Vue



文章目录
            • 1. 版本兼容
            • 2. 导入依赖
            • 3. 配置
            • 4. 主页面
            • 5. 控制层
            • 6. 逻辑处理层
            • 7. pojo
            • 8. 工具类
            • 9. 常量类
            • 10. 前端页面
            • 项目开源地址

1. 版本兼容 框架/组件版本SpringBoot2.6.1elasticsearch7.1.5 2. 导入依赖

        org.springframework.boot
        spring-boot-starter-parent
        2.6.1
         
    
    
        
        
            org.jsoup
            jsoup
            1.14.3
        
        
            com.alibaba
            fastjson
            1.2.78
        
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
3. 配置
server.port=9090
spring.thymeleaf.cache=false

ElasticsearchClientConfig

package com.gblfy.es7jdvue.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;
    }
}

4. 主页面
package com.gblfy.es7jdvue.controller;

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


@Controller
public class IndexController {

    @GetMapping({"/index"})
    public String index() {
        return "index";
    }
}

5. 控制层
package com.gblfy.es7jdvue.controller;

import com.gblfy.es7jdvue.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 IOException {
        return contentService.parseContent(keyword);
    }

    
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List> searchPage(@PathVariable("keyword") String keyword,
                                                @PathVariable("pageNo") int pageNo,
                                                @PathVariable("pageSize") int pageSize) throws IOException {
        return contentService.searchPageHighlight(keyword, pageNo, pageSize);
    }

}

6. 逻辑处理层
package com.gblfy.es7jdvue.service;

import com.alibaba.fastjson.JSON;
import com.gblfy.es7jdvue.consts.ESConst;
import com.gblfy.es7jdvue.pojo.Content;
import com.gblfy.es7jdvue.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.xcontent.XContentType;
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.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 ContentService {


    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //1.解析数据放入es索引中
    public Boolean parseContent(String keyword) throws IOException {

        List contentList = new HtmlParseUtil().parseJD(keyword);
        // 把查询道德数据放入es
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout(ESConst.BULK_REQUEST_TIMEOUT);

        for (int i = 0; i < contentList.size(); i++) {
            bulkRequest.add(new IndexRequest(ESConst.JD_SEARCH_INDEX)
                    .source(JSON.toJSONString(contentList.get(i)), XContentType.JSON));
        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();

    }

    //    2. 获取es中的数据,实现基本搜索功能
    public List> searchPage(String keyword, int pageNo, int pageSize) throws IOException {
        if (pageNo <= 1) {
            pageNo = 1;
        }
        //    条件搜索
        SearchRequest searchRequest = new SearchRequest(ESConst.JD_SEARCH_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //    分页
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);

        //    精准匹配
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(ESConst.SEARCH_CONDITION_FIELD, keyword);
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(new Timevalue(60, TimeUnit.SECONDS));

        //    执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //    解析结果
        ArrayList> list = new ArrayList<>();
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            list.add(documentFields.getSourceAsMap());
        }
        return list;
    }

    //    2. 获取es中的数据,实现基本搜索高亮功能
    public List> searchPageHighlight(String keyword, int pageNo, int pageSize) throws IOException {
        if (pageNo <= 1) {
            pageNo = 1;
        }
        //    条件搜索
        SearchRequest searchRequest = new SearchRequest(ESConst.JD_SEARCH_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //    分页
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);

        //    精准匹配
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(ESConst.SEARCH_CONDITION_FIELD, keyword);
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(new Timevalue(60, TimeUnit.SECONDS));

        //构建高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field(ESConst.HIGHLIGHT_TITLE);
        highlightBuilder.requireFieldMatch(false);//多个高亮 显示
        highlightBuilder.preTags(ESConst.HIGHLIGHT_PRE_TAGS);
        highlightBuilder.postTags(ESConst.HIGHLIGHT_POST_TAGS);
        searchSourceBuilder.highlighter(highlightBuilder);


        //    执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //    解析结果
        ArrayList> list = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            //  解析高亮的字段,将原来的字段置换为我们高亮的字段即可!
            Map highlightFields = hit.getHighlightFields();
            HighlightField title = highlightFields.get(ESConst.HIGHLIGHT_TITLE);
            //    获取原来的结果
            Map sourceAsMap = hit.getSourceAsMap();
            if (title != null) {
                Text[] fragments = title.fragments();
                String newTitle = "";
                for (Text text : fragments) {
                    newTitle += text;
                }
                //高亮字段替换掉原来的内容即可
                sourceAsMap.put(ESConst.SEARCH_CONDITION_FIELD, newTitle);
            }
            // 将结果放入list容器返回
            list.add(sourceAsMap);
        }
        return list;
    }

}

7. pojo
package com.gblfy.es7jdvue.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Content {

    private String title;
    private String img;
    private String price;
}

8. 工具类
package com.gblfy.es7jdvue.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Content {

    private String title;
    private String img;
    private String price;
}

9. 常量类
package com.gblfy.es7jdvue.consts;


public class ESConst {

    //拉取数据url前缀
    public static final String PULL_DATA_baseURL = "https://search.jd.com/Search?keyword=";
    //拉取商品数据标签
    public static final String PULL_GOOD_DATA_TAG ="J_goodsList";
    //商品数据标签中元素标签
    public static final String PULL_GOOD_DATA_CHILD_TAG ="li";


    //京东搜索数据索引
    public static final String JD_SEARCH_INDEX = "jd_goods";
    //高亮标题
    public static final String HIGHLIGHT_TITLE = "title";
    //高亮标签前缀
    public static final String HIGHLIGHT_PRE_TAGS = "";
    //高亮标签后缀
    public static final String HIGHLIGHT_POST_TAGS = "";
    //搜索挑条件字段
    public static final String SEARCH_CONDITION_FIELD = "title";
    public static final String BULK_REQUEST_TIMEOUT = "2m";

}

10. 前端页面




  
  gblfyJava-ES仿京东实战
  





  

    
    
      
        
          
          
            
          

          

            
            
              
天猫搜索

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5638843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存