SpringBoot集成solr

1、windows10下配置solr环境,将solr官网下载好的压缩包解压到指定目录,solr的项目结构如下

  

cmd进入server/solr目录下启动solr服务:

浏览器访问:http://localhost:8983/solr

创建核心文档

2、项目集成:

     pom.xml文件添加依赖

 <dependency>
      <groupId>org.springframework.data</groupId>
       <artifactId>spring-data-solr</artifactId>
  </dependency>

   application.yml配置


data:
    solr:
      host: http://192.168.168.224:8983/solr/searchList

   业务代码:

     solr工具类

@Component
public class SolrUtils {
    private SolrClient solrClient;
    
    private SolrQuery  solrQuery;
    @Autowired
    public SolrUtils(SolrClient solrClient) {
        this.solrClient = solrClient;
        solrQuery = new SolrQuery();
    }

    /**
     * 向solr文档中添加对象
     * @param data
     * @throws IOException
     * @throws SolrServerException
     */
    public void addSolrPO(SolrSearchPO data) throws IOException, SolrServerException {
        solrClient.addBean(data);
        solrClient.commit();
    }

    /**
     * solr查询
     * @param key  关键字
     * @param page  
     * @param row
     * @return
     * @throws IOException
     * @throws SolrServerException
     */
    public List<SolrSearchPO> queryList(String key,int page,int  row) throws IOException, SolrServerException {
        List<SolrSearchPO> list=new ArrayList<>();
        solrQuery.setQuery(key);
        solrQuery.setStart(page);
        solrQuery.setRows(row);
        //设置高亮字段
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("name,title");
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");
        QueryResponse query = solrClient.query(solrQuery);
        // 文档结果集
        SolrDocumentList docs = query.getResults();
        SolrSearchPO solrSearchPO;
        for (SolrDocument s : docs) {
            solrSearchPO = new SolrSearchPO();
            solrSearchPO.setId(s.getFirstValue("id").toString());
            solrSearchPO.setName(s.getFirstValue("name").toString());
            solrSearchPO.setTitle(s.getFirstValue("title").toString());
            solrSearchPO.setContent(s.getFirstValue("content").toString());
            list.add(solrSearchPO);
        }
        return list;
    }

    /**
     * 删除
     * @throws IOException
     * @throws SolrServerException
     */
    public void delDoc() throws IOException, SolrServerException {
        solrClient.deleteByQuery("*:*");
        solrClient.commit();
    }
}

测试

 

@RunWith(SpringRunner.class)
@SpringBootTest
public class SolrTest {

    private Logger logger = LoggerFactory.getLogger(SolrTest.class);

    @Autowired
    private SolrUtils solrUtils;

    @Autowired
    private ITestService testService;

    @Test
    public void addSolr() {
        SolrSearchPO solrSearchPO;
        List<TestPO> allTestPo;
        try {
            allTestPo = testService.findAllTestPo(3);
            for (TestPO t : allTestPo) {
                solrSearchPO = new SolrSearchPO();
                solrSearchPO.setId(String.valueOf(t.getId()));
                solrSearchPO.setName(t.getName());
                solrSearchPO.setContent(t.getContent());
                solrSearchPO.setTitle(t.getTitle());
                solrUtils.addSolrPO(solrSearchPO);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加错误--->"+e.toString());
        }


    }

    @Test
    public void queryList() {
        try {
            List<SolrSearchPO> solrSearchPOS = solrUtils.queryList("*:*", 0, 10);
            logger.info("搜索结果集长度---------->size=" + solrSearchPOS.size());
            for (SolrSearchPO s : solrSearchPOS) {
                logger.info(s.toString());
            }
        } catch (IOException | SolrServerException e) {
            e.printStackTrace();
            logger.info("搜索失败------>e=" + e.toString());
        }
    }

    @Test
    public void del() {
        try {
            solrUtils.delDoc();
            logger.info("删除solr索引成功");
        } catch (IOException | SolrServerException e) {
            e.printStackTrace();
            logger.info("删除solr索引失败");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值