solr8.0 springboot整合solr(四)


引言:

  solr搭建起后,就該應用到java后台開發里了,接下來就用springboot整合應用solr

 

一:引入jar包

  

<!--solr-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>

        <!--操作solr的工具-->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>8.0.0</version>
        </dependency>

 

二:對application配置文件進行配置(mycore1是我創建的核心,具體名字改為你所創建的核心)

spring:
  data:
    solr:
      host: http://127.0.0.1:8983/solr/mycore1

 

三:接下來就是代碼操作了(詳細解釋看注解),這個只是我的服務層

@Service
public class SearchService {

    @Autowired
    private SolrClient solrClient;
//search就是搜索的內容,currentpage是因為我做了分頁,如果沒做分頁可忽略此參數
    public PageResult searchNews(String search,int currentPage) throws IOException, SolrServerException {
//        創建solr查詢對象
        SolrQuery query = new SolrQuery();
        if(null != search && !"".equals(search)){
//            設置查詢關鍵詞
            query.setQuery(search);
//            設置默認查詢域
            query.set("df", "news_keywords");
        }
//        高亮顯示
        query.setHighlight(true);
//        設置高亮顯示字段
        query.addHighlightField("newsTitle,newsAbstract");
        query.setHighlightSimplePre("<span style='color:red'>");
        query.setHighlightSimplePost("</span>");
//        設置排序規則
        query.setSort("newsTime",SolrQuery.ORDER.desc);
//        設置返回格式
        query.set("wt","json");
//        設置分頁
        query.set("start", (currentPage - 1) * 10);
        query.set("rows", 10);
//        進行查詢得到返回結果
        QueryResponse queryResponse = solrClient.query(query);
//        取出高亮部分
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
//        得到主體數據部分
        SolrDocumentList results = queryResponse.getResults();

        ArrayList<NewsWithBLOBs> newsList = new ArrayList<>();
//        對主體數據進行遍歷,將數據依次保存到news對象中,然后將news對象加入list集合就是查詢到的所有新聞
        for (SolrDocument result : results){
            NewsWithBLOBs news = new NewsWithBLOBs();
            news.setNewsId(result.get("id").toString());
            news.setNewsCover(result.get("newsCover").toString());
            news.setNewsTime((Date) result.get("newsTime"));
            news.setNewsBrowse((Integer) result.get("newsBrowse"));
            news.setNewsSchoolid(result.get("newsSchoolid").toString());
            news.setNewsCategoryid(result.get("newsCategoryid").toString());
            news.setNewsAbstract(result.get("newsAbstract").toString());
            news.setNewsContent(result.get("newsContent").toString());
//            設置高亮部分,下邊是得到指定新聞id的高亮部分,並且將高亮部分設置進入對象中
            Map<String, List<String>> map = highlighting.get(result.get("id"));
            List<String> list = map.get("newsAbstract");
            if(null != list && list.size() > 0){
                String newsAbstract = list.get(0);
                news.setNewsAbstract(newsAbstract);
            }
            List<String> list1 = map.get("newsTitle");
            if(null != list1 && list1.size() > 0){
                String newsTitle = list1.get(0);
                news.setNewsTitle(newsTitle);
            }
            newsList.add(news);
         }

//        得到所獲得的新聞條數
        long numFound = results.getNumFound();
//        下邊是我自己的分頁封裝,可忽略,上邊的到的newslist就是獲得的所有新聞
        PageResult result = new PageResult();
        result.setRecordCount(numFound);
        System.out.println(numFound);
        result.setTotalPages((int) (numFound%10 == 0 ? numFound/10 : numFound/10+1));
        result.setList(newsList);
        return result;
    }
}

  

通過上邊就能獲取到指定的查詢對象了,並且高亮顯示也正常


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM