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;
    }
}

  

通过上边就能获取到指定的查询对象了,并且高亮显示也正常

posted @ 2019-04-20 10:49  Jarbein  阅读(2061)  评论(0编辑  收藏  举报