搭建solr环境并使用

2021-02-05  本文已影响0人  代瑶

https://www.sohu.com/a/281422518_796914
关于它的介绍

https://archive.apache.org/dist/lucene/solr/8.0.0/ 下载

到solr-8.0.0\bin目录下执行
./solr start //启动服务
./solr stop -all //停止服务

环境搭建配置
https://blog.csdn.net/weixin_44253204/article/details/104326273

讲解查询语句
https://blog.csdn.net/magicpenta/article/details/82354106

增删改查
https://blog.csdn.net/viplisong/article/details/81036051

image.png

对搜索数据进行关键字高亮


    public static List<ResponseObject> search(SolrQueryUtil q) throws IOException, SolrServerException {
        //创建查询对象
        SolrQuery query = new SolrQuery();

        //q查询条件
        query.set("q", q.getQuery());
        //根据id排序
        query.setSort(q.getSort(), SolrQuery.ORDER.asc);
        query.setStart(q.getStart());
        query.setRows(q.getRows());

        query.setHighlight(true); // 开启高亮组件或用query.setParam("hl", "true");
        query.addHighlightField("content");// 设置高亮字段
        query.setHighlightSimplePre("<font color='red'>");//标记,高亮关键字前缀
        query.setHighlightSimplePost("</font>");//后缀

        //查询响应对象
        QueryResponse response = solrClient.query(query);

        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        List<ResponseObject> _responseObjs = new ArrayList<>();

        //获取查询结果列表
        SolrDocumentList docs = response.getResults();  
        Set<String> highlightSet = highlighting.keySet();
        int highlightIndex = 0;
        for (String itemHighlight : highlightSet) {
            Map<String, List<String>> stringListMap = highlighting.get(itemHighlight);

            ResponseObject _responseObj = new ResponseObject();

            SolrDocument _itemDoc = docs.get(highlightIndex);
            _responseObj.content =  stringListMap.get("content").toString().replaceAll("\"", "'");
            _responseObj.id = (String) _itemDoc.get("id");
            _responseObj.title =  _itemDoc.get("title");

            _responseObjs.add(_responseObj);

            highlightIndex++;
        }
        //获取查询出的数量
//        long count = docs.getNumFound();
        //关闭客户端
        return _responseObjs;
    }

上一篇下一篇

猜你喜欢

热点阅读