在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(