上一篇我们对elasticsearch的基本概念有了一些了解,然后我们需要懂一些基本操作。
在请求URL中输入:http://127.0.0.1:9200/secisland?pretty。在请求的方法中选择PUT,
创建索引库示意图点击Request按钮后可以在右边看到返回的内容如下,表示建库成功:
执行完建库后查询一下库的状态,有两种方式查看,如果用命令查看,可以在浏览器中执行
http://127.0.0.1:9200/_cat/indices?v 查询索引库列表
索引库状态查询结果表示已经建成了一个索引secisland,主分片是5个,健康度是黄色,状态是活动,文档数为0。
请求:PUT http://127.0.0.1:9200/secisland/secilog/1/
参数:
{
"computer": "secisland",
"message": "secisland is a computer"
}
返回值:
{
"_index": "secisland",
"_type": "secilog",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
请求:POST http://127.0.0.1:9200/secisland/secilog/1/_update
参数:
{
"doc": {
"computer": "secisland",
"message": "secisland is a computer computer computer"
}
}
返回值:
{
"_index": "secisland",
"_type": "secilog",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
请求:GET http://127.0.0.1:9200/secisland/secilog/1/
返回值:
{
"_index": "secisland",
"_type": "secilog",
"_id": "1",
"_version": 2,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"computer": "secisland",
"message": "secisland is a computer computer computer"
}
}
请求:DELETE http://127.0.0.1:9200/secisland/secilog/1/
返回值:
{
"_index": "secisland",
"_type": "secilog",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
请求:DELETE http://127.0.0.1:9200/secisland/
返回值:
{
"acknowledged": true
}
Elasticsearch本身是Java开发的,天生对Java的支持能力是最好的,所以用Java来开发Elasticsearch应该是一个不错的选择。
关于TransportClient,elastic计划在Elasticsearch 7.0中弃用TransportClient,并在8.0中完全删除它。后面,应该使用Java高级REST客户端,它执行HTTP请求而不是序列化的Java请求。Java客户端主要用途有:
(1)在现有集群上执行标准索引,获取,删除和搜索操作
(2)在正在运行的集群上执行管理任务
获取Elasticsearch客户端最常用方法是创建连接到群集的TransportClient。
所以我们这里对TransportClient和Java高级REST客户端都要有一些了解,这里主要使用Java高级REST客户端。
org.elasticsearch
elasticsearch
7.2.1
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.2.1
高级客户端的初始化是基于RestHighLevelClient实现的,而RestHighLevelClient是基于初级客户端生成器构建的。高级客户端在内部创建执行请求的初级客户端,该初级客户端会维护一个连接池并启动一些线程,因此当对高级客户端的接口调用完成时,应该关闭它,因为它将同步关闭内部初级客户端,以释放这些资源。
客户端初始化
package com.example.luu.es_demo.es;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.io.IOException;
@RestController
@RequestMapping(value = "/rest/")
public class EsRestController {
/**
* 高级客户端
*/
private RestHighLevelClient restHighLevelClient;
/**
* 客户端初始化
*/
@PostConstruct
public void initClient() {
restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("8.129.210.66", 9200, "http")));
System.out.println(String.format("客户端初始化 restHighLevelClient=(%s)", restHighLevelClient.toString()));
}
public void closeEs() {
try {
restHighLevelClient.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println(String.format("关闭客户端异常 ex=(%s)", e.getMessage()));
}
}
}