栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合Solr

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合Solr

SpringBoot整合Solr 一、添加依赖

    org.springframework.boot
    spring-boot-starter-data-solr
    2.4.11

二、添加配置

必须指定solr的某个core

错误写法:http://localhost:8983/solr/

spring:
  data:
    solr:
      host: http://localhost:8983/solr/hotle_core
三、测试、

需要注入SolrClient

	//注入SolrClient
	@Resource
    private SolrClient solrClient;

    //添加操作
    @Test
    void contextLoads() throws IOException, SolrServerException {
        //新建索引
        SolrInputdocument document = new SolrInputdocument();
        //创建索引
        document.setField("id","123411");
        document.setField("name","小明");
        //添加索引
        solrClient.add(document);
        solrClient.commit();
    }

测试查询

result.get(“id”)获取的是solr配置文件中映射entity中的值

通过SolrQuery对象进行设置筛选条件进行查询

@Test
void querydocument() throws IOException, SolrServerException {
    SolrQuery query = new SolrQuery();
    //查询条件,默认所有查询
    query.setQuery("*:*");
    //查询结果的封装对象
    QueryResponse queryResponse = solrClient.query(query);
    //查询结果
    SolrdocumentList results = queryResponse.getResults();
    //查询到的数量
    long numFound = results.getNumFound();
    System.out.println(numFound);
    for (Solrdocument result : results) {
        System.out.println("id=>"+result.get("id"));
        System.out.println("article_abstract=>"+result.get("article_abstract"));
    }
}

安装使用Solr

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325464.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号