es批量根据查询结果删除并实时刷新数据

最近产品提了个新需求,需要按照查询筛选结果,支持批量删除es数据,es的版本号是5.6.x

下面内容是根据前端传参ids进行的批量删除

@RequestMapping(value = "/delete ", method = RequestMethod.DELETE)
public Object  delete (@RequestBody List<String> ids){
  BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
int deleted= 0;
if(ids !=null){
   BoolQueryBuilder subQuery = QueryBuilders.boolQuery();
   for(int i = 0; i< ids.size(); i++){
      subQuery.should(QueryBuilders.termQuery("_id",ids.get(i)));
   }
   queryBuilder.must(subQuery);

   BulkByScrollResponse response =
         DeleteByQueryAction.INSTANCE.newRequestBuilder(esClient.getESClient())
               .filter(queryBuilder)
               .source("这填索引名称").refresh(true)// refresh true是因为es删除有延迟,此处立即刷新就是为了从页面上 ,删除后立马就看不见数据
               .get();
    deleted = (int) response.getDeleted();//返回具体删除的条数
}
return deleted;
 }
@Component
public class EsClient{

   @ApiObjectField(description = "esName")
   @Value("${es.name}")
   private String esName;
   
   @ApiObjectField(description = "esAddress")
   @Value("${es.address}")
   private String esAddress;

   @ApiObjectField(description = "esPort")
   @Value("${es.port}")
   private Integer port;
   
   private static TransportClient client = null;

    /**
    * 获取ESClient,如果Client为空则调用refreshClient来生成。
    * @return
    */
   @PostConstruct
   public TransportClient getESClient(){
      if(null == client){
         try {
            refreshClient(esName, esAddress, port);
         } catch (UnknownHostException unknownEx) {

         }

      }
      return client;
   }

   /**
    * 静态方法,类加载时调用此方法
    * @param clusterName
    * @param hosts
    * @param esPort
    * @throws RuntimeException
    */
   public void refreshClient(String clusterName, String hosts,int esPort) throws RuntimeException, UnknownHostException{
      hosts = hosts.trim();
      clusterName = clusterName.trim();
      if(StringUtils.isEmpty(clusterName)){
         throw new RuntimeException("Elastic Search Cluster Name is empty.");
      }
      if(StringUtils.isEmpty(hosts)){
         throw new RuntimeException("Elastic Search Hosts is empty.");
      }

      Settings settings = Settings.builder().put("client.transport.sniff", false)
            .put("cluster.name", clusterName).build();
      client = new PreBuiltTransportClient(settings);

      if(hosts.endsWith(";")){
         hosts = hosts.substring(0, hosts.length() - 1);
      }
      String[] hostsArray = hosts.split(";");
      for(String host : hostsArray){
         client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), esPort));
      }
   }

   
}


完成啦~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Elasticsearch 查询实时更新数据,你可以按照以下步骤进行: 1. 安装 Elasticsearch:你需要在本地或服务器上安装 Elasticsearch,并启动它。你可以从官方网站下载并安装 Elasticsearch。 2. 安装 Elasticsearch PHP 客户端:你需要安装 Elasticsearch PHP 客户端,以便能够与 Elasticsearch 进行交互。你可以使用 composer 进行安装,使用以下命令: ``` composer require elasticsearch/elasticsearch ``` 3. 编写 PHP 代码:你需要编写 PHP 代码来连接 Elasticsearch查询数据。以下是一个示例代码: ```php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build(); $params = [ 'index' => 'myindex', 'type' => 'mytype', 'body' => [ 'query' => [ 'match' => [ 'title' => 'php' ] ] ] ]; $response = $client->search($params); foreach ($response['hits']['hits'] as $hit) { echo $hit['_source']['title'] . "\n"; } $lastUpdated = $response['hits']['hits'][0]['_source']['updated_at']; $params = [ 'index' => 'myindex', 'type' => 'mytype', 'body' => [ 'query' => [ 'range' => [ 'updated_at' => [ 'gt' => $lastUpdated ] ] ] ] ]; while (true) { $response = $client->search($params); foreach ($response['hits']['hits'] as $hit) { echo $hit['_source']['title'] . "\n"; } $lastUpdated = $response['hits']['hits'][0]['_source']['updated_at']; sleep(10); $params['body']['query']['range']['updated_at']['gt'] = $lastUpdated; } ``` 以上代码会查询 Elasticsearch 中包含 "php" 的文档,并输出它们的标题。然后代码会每 10 秒查询 Elasticsearch 中更新时间大于上次查询的文档,并输出它们的标题。 以上是使用 PHP 代码实现 Elasticsearch 查询实时更新数据的基本步骤。你可以根据自己的需求进行调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值