ElasticSearch 实现分词全文检索 - delete-by-query

目录

ElasticSearch 实现分词全文检索 - 概述
ElasticSearch 实现分词全文检索 - ES、Kibana、IK安装
ElasticSearch 实现分词全文检索 - Restful基本操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - 测试数据准备
ElasticSearch 实现分词全文检索 - term、terms查询
ElasticSearch 实现分词全文检索 - match、match_all、multimatch查询
ElasticSearch 实现分词全文检索 - id、ids、prefix、fuzzy、wildcard、range、regexp 查询
ElasticSearch 实现分词全文检索 - Scroll 深分页
ElasticSearch 实现分词全文检索 - delete-by-query
ElasticSearch 实现分词全文检索 - 复合查询
ElasticSearch 实现分词全文检索 - filter查询
ElasticSearch 实现分词全文检索 - 高亮查询
ElasticSearch 实现分词全文检索 - 聚合查询 cardinality
ElasticSearch 实现分词全文检索 - 经纬度查询
ElasticSearch 实现分词全文检索 - 搜素关键字自动补全(suggest)
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码

数据准备

ElasticSearch 实现分词全文检索 - 测试数据准备

delete-by-query

根据 term、match 等查询方式去删除大量的文档

如果需要删除的内容,是index下的大部分数据,不建议使用,因为去匹配文档时还是一个一个的拿到文档ID,去删除
推荐创建一个全新的index,将保留的文档内容,添加到全新的索引中

# 查询出有一条数据,删除后再查询,数据已不存在
GET /sms-logs-index/_search/
{
  "query": {
    "range": {
      "fee": {
        "lt": 15
      }
    }
  }
}

# delete-by-query
POST /sms-logs-index/_delete_by_query
{
  "query": {
    "range": {
      "fee": {
        "lt": 15
      }
    }
  }
}

Java

@Test
void deleteByQuery() throws Exception {
    String indexName = "sms-logs-index";
    RestHighLevelClient client = ESClient.getClient();

    //1. 创建DeleteByQueryRequest对象
    DeleteByQueryRequest request = new DeleteByQueryRequest(indexName);

    //2. 指定查询条件 和 SearchRequest 指定Query的方式不一样
    request.setQuery(QueryBuilders.rangeQuery("fee").lt(16));

    //3. 执行删除
    BulkByScrollResponse resp = client.deleteByQuery(request, RequestOptions.DEFAULT);

    //4. 输出返回值
    System.out.println(resp.toString());
}
posted @ 2023-03-15 13:04  VipSoft  阅读(188)  评论(0编辑  收藏  举报