SpringBoot整合Solr及其基本使用(含语法介绍)

SpringBoot整合Solr及其基本使用

SpringBoot整合Solr

  1. SpringBoot版本
    
    // SpringBoot 父版本 2.4.12
    
    
  2. dependency
    
    // maven依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    
  3. application.properties配置solr
    
    // 也可以在solr后添加core 如下;
    // 如果已经添加了,在solrClient中就无须指定core collection。
    // http://127.0.0.1:8983/solr/core
    #solr 相关配置
    spring.data.solr.host=http://127.0.0.1:8983/solr
    
    

Solr的基本使用

可以直接使用也可以定义SolrDocument对象进行操作。这里演示的是直接原生操作使用。

  1. 增加
    
    // 
    public void add() throws SolrServerException, IOException {
    
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        solrInputDocument.addField("id","200019311");
        solrInputDocument.addField("author","test");
        solrInputDocument.addField("title","test");
        solrClient.add(ProjectConstant.SOLR_COLLECTION, solrInputDocument);
        final UpdateResponse commit = solrClient.commit(ProjectConstant.SOLR_COLLECTION);
        System.out.println(commit.toString());
    
    }
    
    
  2. 修改
    
    // 
    public void update() throws SolrServerException, IOException {
    
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        solrInputDocument.addField("id","200019311");
        solrInputDocument.addField("author","test1");
        solrInputDocument.addField("title","test2");
        solrClient.add(ProjectConstant.SOLR_COLLECTION, solrInputDocument);
        final UpdateResponse commit = solrClient.commit(ProjectConstant.SOLR_COLLECTION);
        System.out.println(commit.toString());
    
    }
    
    
  3. 基本查询
    
    // 
    public void query() throws SolrServerException, IOException {
    
        // 指定字段查询
        final SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","id:2000193");
        // 返回行数
        solrQuery.setRows(20);
    
        // 查询solr的rlkteme集合数据索引库(Core)
        final QueryResponse query = solrClient.query(ProjectConstant.SOLR_COLLECTION,solrQuery);
        final SolrDocumentList results = query.getResults();
    
        for (SolrDocument result : results) {
            System.out.println(result.toString());
        }
    
    }
    
    
  4. 分页查询
    // 
    
    public void queryByField() throws SolrServerException, IOException {
    
       int pageNo = 2;
       int pageSize = 1;
    
       // *字代表通配符
       final SolrQuery solrQuery = new SolrQuery();
       solrQuery.set("q","id:102*3");
       // 一般分页使用
       // 从第几个开始 假定每页20条,现在即将要查询的为第二页(第一页:1-20 第二页:20-40)
       solrQuery.setStart((pageNo - 1) * pageSize);
       // 返回行数
       solrQuery.setRows(pageNo * pageSize);
    
       // 排序
       solrQuery.setSort("id", SolrQuery.ORDER.desc);
    
       // 查询solr的rlkteme集合数据索引库(Core)
       final QueryResponse query = solrClient.query(ProjectConstant.SOLR_COLLECTION,solrQuery);
       final SolrDocumentList results = query.getResults();
    
       for (SolrDocument result : results) {
           System.out.println(result.toString());
       }
    
    }
    
    
    
  5. 删除
    //
    
    public void delete() throws SolrServerException, IOException {
    
        final UpdateResponse updateResponse = solrClient.deleteById(ProjectConstant.SOLR_COLLECTION,"200019311");
        solrClient.commit(ProjectConstant.SOLR_COLLECTION);
        System.out.println(updateResponse.toString());
    
    }
    
    

Solr的语法介绍

查询的语法

官方文档:Solr官网链接

	// 本文参考了其他作者总结,进行了整合。感谢前人种树。
	// 参考作者博客:[solr语法](https://www.cnblogs.com/johnwood/p/3449118.html)
	// [solr基本使用](https://www.cnblogs.com/frankdeng/p/9615856.html)

一、 查询参数说明

 
在做solr查询的时候,solr提供了很多参数来扩展它自身的强大功能!以下是使用频率最高的一些参数! 


1、常用 

q - 查询字符串,这个是必须的。如果查询所有*:* ,根据指定字段查询(Name:张三 AND Address:北京)

fq - (filter query)过虑查询,作用:在q查询符合结果中同时是fq查询符合的,例如:q=Name:张三&fq=CreateDate:[20081001 TO 20091031],找关键字mm,并且CreateDate是20081001

fl - 指定返回那些字段内容,用逗号或空格分隔多个。 

start - 返回第一条记录在完整找到结果中的偏移位置,0开始,一般分页用。 

rows - 指定返回结果最多有多少条记录,配合start来实现分页。 

sort - 排序,格式:sort=<field name>+<desc|asc>[,<field name>+<desc|asc>]… 。示例:(score desc, price asc)表示先 “score” 降序, 再 “price” 升序,默认是相关性降序。 

wt - (writer type)指定输出格式,可以有 xml, json, php, phps。 

fl表示索引显示那些field( *表示所有field,如果想查询指定字段用逗号或空格隔开(如:Name,SKU,ShortDescription或Name SKU ShortDescription【注:字段是严格区分大小写的】)) 

q.op 表示q 中 查询语句的 各条件的逻辑操作 AND() OR() 

hl 是否高亮 ,如hl=true

hl.fl 高亮field ,hl.fl=Name,SKU

hl.snippets :默认是1,这里设置为3个片段

hl.simple.pre 高亮前面的格式 

hl.simple.post 高亮后面的格式 

facet 是否启动统计 

facet.field  统计field 

【注:以上是比较常用的参数,当然具体的参数使用还是多看Solr官方的技术文档以及一些大神的博文日志,这里只是抛砖引玉】

 

二、 Solr运算符

1.:” 指定字段查指定值,如返回所有值*:*

2.?” 表示单个任意字符的通配

3.*” 表示多个任意字符的通配(不能在检索的项开始使用*或者?符号)

4.~” 表示模糊检索,如检索拼写类似于”roam”的项这样写:roam~将找到形如foam和roams的单词;roam~0.8,检索返回相似度在0.8以上的记录。

5. 邻近检索,如检索相隔10个单词的”apache”和”jakarta”,”jakarta apache”~10

6.^” 控制相关度检索,如检索jakarta apache,同时希望去让”jakarta”的相关度更加好,那么在其后加上”^”符号和增量值,即jakarta^4 apache

7. 布尔操作符AND||

8. 布尔操作符OR&&

9. 布尔操作符NOT!- (排除操作符不能单独与项使用构成查询)
10.+” 存在操作符,要求符号”+”后的项必须在文档相应的域中存在
11. ( ) 用于构成子查询
12. [] 包含范围检索,如检索某时间段记录,包含头尾,date:[200707 TO 200710]
13. {} 不包含范围检索,如检索某时间段记录,不包含头尾
date:{200707 TO 200710}
14. / 转义操作符,特殊字符包括+ - && || ! ( ) { } [ ] ^~ * ? : /

 注:①“+”和”-“表示对单个查询单元的修饰,and 、or 、 not 是对两个查询单元是否做交集或者做差集还是取反的操作的符号

   比如:AB:china +AB:america ,表示的是AB:china忽略不计可有可无,必须满足第二个条件才是对的,而不是你所认为的必须满足这两个搜索条件

   如果输入:AB:china AND AB:america ,解析出来的结果是两个条件同时满足,即+AB:china AND +AB:america或+AB:china +AB:america

  总而言之,查询语法:  修饰符 字段名:查询关键词 AND/OR/NOT 修饰符 字段名:查询关键词

三、 Solr查询语法

1.最普通的查询,比如查询姓张的人( Name:张),如果是精准性搜索相当于SQL SERVER中的LIKE搜索这需要带引号("",比如查询含有北京的(Address:"北京"2.多条件查询,注:如果是针对单个字段进行搜索的可以用(Name:搜索条件加运算符(ORANDNOT) Name:搜索条件),比如模糊查询( Name:OR Name:李 )单个字段多条件搜索不建议这样写,一般建议是在单个字段里进行条件筛选,如( Name:OR 李),多个字段查询(Name:+ Address:北京 )

3.排序,比如根据姓名升序(Name asc),降序(Name desc)

SolrClient与SolrTemplate区别

两者均可进行Solr的操作,SolrTemplate只是对SolrClient进行了操作的封装,具有更复杂的功能。


	Below is the description from apache documentation for SolrClient
	
	Abstraction through which all communication with a Solr server may be routed
	which means all your solr calls will be route via solrClient so we need to configure solr server address, port(few other also) to solrClient .
	
	where as solrTemplate is for solr operations like query, count, etc.. solrTemplate will use solrClient that's why while configuring solrTemplate , solrClient is passed.
	
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤使用Java代码获取Solr的客户端: 1. 导入SolrJ包:在Java项目中导入SolrJ包,可以通过Maven或手动下载的方式获取。 2. 创建SolrClient对象:使用SolrJ包中提供的SolrClient类创建SolrClient对象,SolrClient是SolrJ包中的核心类,它可以用来与Solr服务器进行通信。 ```java // 创建SolrClient对象 SolrClient solrClient = new HttpSolrClient.Builder("http://localhost:8983/solr/").build(); ``` 3. 执行查询操作:使用SolrClient对象执行查询操作,可以使用SolrJ包提供的QueryResponse类获取查询结果。 ```java // 创建查询对象 SolrQuery query = new SolrQuery(); query.setQuery("*:*"); // 执行查询操作 QueryResponse response = solrClient.query(query); // 获取查询结果 SolrDocumentList documentList = response.getResults(); ``` 4. 关闭SolrClient对象:在使用SolrClient对象后,需要调用close()方法关闭该对象,释放资源。 ```java // 关闭SolrClient对象 solrClient.close(); ``` 完整的代码示例: ```java import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.client.solrj.SolrQuery; public class SolrClientExample { public static void main(String[] args) throws Exception { // 创建SolrClient对象 SolrClient solrClient = new HttpSolrClient.Builder("http://localhost:8983/solr/").build(); // 创建查询对象 SolrQuery query = new SolrQuery(); query.setQuery("*:*"); // 执行查询操作 QueryResponse response = solrClient.query(query); // 获取查询结果 SolrDocumentList documentList = response.getResults(); System.out.println("查询结果数量:" + documentList.getNumFound()); // 关闭SolrClient对象 solrClient.close(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值