ElasticSearch7.3 JAVA API查询 删除

本文介绍了在ElasticSearch 7.3版本中,由于TransportClient不再推荐使用,转而采用RestHighLevelClient进行数据操作。通过示例展示了如何使用RestHighLevelClient进行查询和删除操作,包括查询原始表结构、Elasticsearch的index定义、SpringBoot工程中的Controller和Service实现,以及POM配置。更多API操作可参照官方文档。
摘要由CSDN通过智能技术生成

在7.3版本中,已经不推荐使用TransportClient这个client,官网说在8.0以后的版本TransportClient将会被删除,并且推荐大家使用高阶版本的REST CLIENT -> RestHighLevelClient,本文使用的是7.3的RestHighLevelClient。

关于RestHighLevelClient,官网给的文档非常的详细,SO EASY。

本文只给出常用的查询和删除操作实例。

查询原始表结构如下:

CREATE TABLE `article` (
  `id` varchar(255) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `content` longtext,
  `createdate` datetime DEFAULT NULL,
  `editdate` datetime DEFAULT NULL,
  `viewnums` int(11) DEFAULT '0',
  `likenums` int(11) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

elasticsearch中的index如下

{
  "mapping": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "@version": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "content": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "createdate": {
        "type": "date"
      },
      "editdate": {
        "type": "date"
      },
      "id": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "likenums": {
        "type": "long"
      },
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "type": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "viewnums": {
        "type": "long"
      }
    }
  }
}

下面是SpringBoot工程中的Controller和Service,Service中包含了删除和查询的方法,以及注释。

Controller

package com.zzj.cloud.esclient.controller;


import com.zzj.cloud.esclient.services.ESRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class ESController {

    @Autowired
    private ESRestService esRestService;
    @GetMapping(
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值