一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

  • 搜索系列

    • Solr

      • 【搜索系列】Solr环境搭建与简单测试
      • 【搜索系列】Solr之文档新增与修改使用姿势
      • 【搜索系列】Solr文档删除
        • I. 配置
        • II. 删除
          • 1. 根据主键删除
          • 2. 查询删除
          • 3. 测试
        • II. 其他
          • 0. 系列博文&项目源码
          • 1. 一灰灰Blog
      • 【搜索系列】Solr查询使用姿势小结
      • 【搜索系列】Solr身份认证与授权更新异常解决方案
    • ElasticSearch

  • MQ系列

  • WEB系列

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • 搜索系列
  • Solr
一灰灰
2020-01-14

【搜索系列】Solr文档删除

之前的搜索教程开了个头就没有继续了,现在重新捡回来,至少也把CURD的基本操作姿势补全了;本篇主要介绍如何删除数据

# I. 配置

在介绍demo之前,需要先安装solr环境,搭建SpringBoot项目工程,具体的环境搭建过程不细说,推荐参考文档

  • 190510-SpringBoot高级篇搜索之Solr环境搭建与简单测试 (opens new window)

在application.yml 配置文件中红,指定solr的域名

spring:
  data:
    solr:
      host: http://127.0.0.1:8983/solr
1
2
3
4

然后在solr中,写入一些数据,供我们删除使用,可以通过控制台的方式写入,也可以通过190526-SpringBoot高级篇搜索Solr之文档新增与修改使用姿势 (opens new window) 这篇文档的case添加

{
  "id":"1",
  "content_id":1,
  "title":"一灰灰blog",
  "content":"这是一灰灰blog的内容",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609540674060288},
{
  "id":"2",
  "content_id":2,
  "title":"一灰灰",
  "content":"这是一灰灰的内容",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609550229733376},
{
  "id":"3",
  "content_id":3,
  "title":"solrTemplate 修改之后!!!",
  "create_at":1578912072,
  "publish_at":1578912072,
  "type":0,
  "_version_":1655609304941592576},
{
  "id":"4",
  "content_id":4,
  "type":1,
  "create_at":0,
  "publish_at":0,
  "_version_":1655609305022332928},
{
  "id":"5",
  "content_id":5,
  "title":"addBatchByBean - 1",
  "content":"新增一个测试文档",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609304836734976},
{
  "id":"6",
  "content_id":6,
  "title":"addBatchByBean - 2",
  "content":"新增又一个测试文档",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655684018701598720
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

# II. 删除

我们依然是使用SolrTemplate来操作solr的正删改查,它整合了solr的各种基本操作

# 1. 根据主键删除

请注意,这种case是根据主键id进行删除的,支持批量删除,需要solrTemplate.commit("yhh");这一行来提交修改

private void deleteById() {
    solrTemplate.deleteByIds("yhh", Arrays.asList("4"));
    solrTemplate.commit("yhh");
}
1
2
3
4

# 2. 查询删除

上面根据主键删除适合精准的删除操作,但是适用性有限;下面介绍查询删除的方式,将满足查询条件的数据都删除掉

private void deleteByQuery() {
    SolrDataQuery query = new SimpleQuery();
    query.addCriteria(Criteria.where("content").startsWith("新增"));
    solrTemplate.delete("yhh", query);
    solrTemplate.commit("yhh");
}
1
2
3
4
5
6

上面提供了一个简单的查询条件,删除content内容以新增开头的文档,至于查询语句的使用姿势在下一篇介绍Solr的查询姿势时详细说明

# 3. 测试

接下来测试一下上面的两种case

首先我们提供一个输出所有文档的方法,用于对比删除前后的数据变化

private void printAll(String tag) {
    System.out.println("\n---------> query all " + tag + " start <------------\n");
    List<DocDO> list = solrTemplate.query("yhh", new SimpleQuery("*:*").addSort(Sort.by("content_id").ascending()), DocDO.class)
                    .getContent();
    list.forEach(System.out::println);
    System.out.println("\n---------> query all " + tag + " over <------------\n");
}
1
2
3
4
5
6
7

接下来是方法调用

@Autowired
private SolrTemplate solrTemplate;

public void delete() {
    printAll("init");
    this.deleteById();
    this.deleteByQuery();
    printAll("afterDelete");
}
1
2
3
4
5
6
7
8
9

输出结果如下,id为4,5,6的都被删除了

---------> query all init start <------------

DocDO(id=1, contentId=1, title=一灰灰blog, content=这是一灰灰blog的内容, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=2, contentId=2, title=一灰灰, content=这是一灰灰的内容, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=3, contentId=3, title=solrTemplate 修改之后!!!, content=null, type=0, createAt=1578988256, publishAt=1578988256)
DocDO(id=4, contentId=4, title=null, content=null, type=1, createAt=0, publishAt=0)
DocDO(id=5, contentId=5, title=addBatchByBean - 1, content=新增一个测试文档, type=1, createAt=1578988256, publishAt=1578988256)
DocDO(id=6, contentId=6, title=addBatchByBean - 2, content=新增又一个测试文档, type=1, createAt=1578988256, publishAt=1578988256)

---------> query all init over <------------


---------> query all afterDelete start <------------

DocDO(id=1, contentId=1, title=一灰灰blog, content=这是一灰灰blog的内容, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=2, contentId=2, title=一灰灰, content=这是一灰灰的内容, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=3, contentId=3, title=solrTemplate 修改之后!!!, content=null, type=0, createAt=1578988256, publishAt=1578988256)

---------> query all afterDelete over <------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# II. 其他

# 0. 系列博文&项目源码

系列博文

  • 190526-SpringBoot高级篇搜索Solr之文档新增与修改使用姿势 (opens new window)
  • 190510-SpringBoot高级篇搜索之Solr环境搭建与简单测试 (opens new window)

项目源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/140-search-solr (opens new window)

# 1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#Solr
上次更新: 2021/10/15, 19:56:22
【搜索系列】Solr之文档新增与修改使用姿势
【搜索系列】Solr查询使用姿势小结

← 【搜索系列】Solr之文档新增与修改使用姿势 【搜索系列】Solr查询使用姿势小结→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×