es
与mysql
对比
MySQL | ES |
---|---|
database | index |
table | doc |
row | document |
一、索引操作
前置信息:
es
安装在192.168.1.51
机器上
String baseUrl = "http://192.168.1.51:9200/";
1.1 创建索引
在kibana
中,执行以下请求,就可以创建索引:
PUT /<index>
PUT /test-index-01
@Test
public void createIndex() {
String indexName = "test-index-01";
String url = baseUrl + indexName;
String result = RestClient.putWithJson(url, JSON.toJSONString(new HashMap<String, String>()), null);
System.out.println(result);
}
在创建索引时,指定一些配置项(执行主分片数为3,副分片数为2):
PUT /test-index-01
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
}
@Test
public void createIndex02() {
String content = "{\"settings\":{\"index\":{\"number_of_shards\":3,\"number_of_replicas\":2}}}";
String indexName = "test-index-01";
String url = baseUrl + indexName;
String result = RestClient.putWithJson(url, content, null);
System.out.println("result = " + result);
}
或者:
PUT /test-index-01
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
在创建索引时,指定document
中的字段类型等信息(注意,elasticsearch
在高版本中,不在支持指定索引类型,相当于一个mysql
中,一个database
中只能有一个table
):
PUT /test-index-01
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
}
@Test
public void createIndex03() throws IOException {
String content = new String(Files.readAllBytes(Paths.get(basePath + "index.json")));
String indexName = "test-index-01";
String url = baseUrl + indexName;
String result = RestClient.putWithJson(url, content, null);
System.out.println("result = " + result);
}
索引创建成功后,会返回以下信息:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test-index-01"
}
1.2 查询索引
查询单个索引信息
GET /test-index-03
@Test
public void queryIndex() {
String indexName = "test-index-03";
String url = baseUrl + indexName;
String result = RestClient.get(url, null, null);
System.out.println("result = " + result);
}
查询多个索引
GET /test-index-02,test-index-03
@Test
public void queryIndex() {
String indexName = "test-index-02,test-index-03";
String url = baseUrl + indexName;
String result = RestClient.get(url, null, null);
System.out.println("result = " + result);
}
查询所有索引
GET /_cat/indices
@Test
public void queryAllIndex() {
String url = BASE_URL + "/_cat/indices";
String result = RestClient.get(url);
System.out.println(result);
}
1.3 删除索引
删除单个索引
DELETE /test-index-01
@Test
public void deleteIndex() {
String indexName = "test-index-01";
String url = baseUrl + indexName;
String result = RestClient.delete(url, null, null);
System.out.println("result = " + result);
}
删除多个索引
DELETE /test-index-01,test-index-02,test-index-03
@Test
public void deleteIndex() {
String indexName = "test-index-01,test-index-02,test-index-03";
String url = baseUrl + indexName;
String result = RestClient.delete(url, null, null);
System.out.println("result = " + result);
}
1.4 判断索引是否存在
判断单个索引是否存在,如果存在,返回200;不存在,返回404
HEAD test-index-01
@Test
public void existTest() {
String indexName = "test-index-01";
String url = baseUrl + indexName;
String result = RestClient.head(url, null, null);
System.out.println("result = " + result);
}
判断多个索引是否存在,其中某一个不存在,则返回404;全部存在,则返回200
HEAD test-index-01,test-index-02
@Test
public void existTest() {
String indexName = "test-index-01,test-index-02";
String url = baseUrl + indexName;
String result = RestClient.head(url, null, null);
System.out.println("result = " + result);
}
1.5 关闭索引
关闭索引后,不能往向该索引写入数据,也不能从该索引中读取数据
POST /test-index-01/_close
@Test
public void closeIndex() {
String indexName = "test-index-01";
String url = baseUrl + indexName + "/_close";
String result = RestClient.postWithJson(url, null, null);
System.out.println("result = " + result);
}
1.6 打开索引
POST /test-index-01/_open
@Test
public void closeIndex() {
String indexName = "test-index-01";
String url = baseUrl + indexName + "/_open";
String result = RestClient.postWithJson(url, null, null);
System.out.println("result = " + result);
}
1.7 克隆索引
在克隆索引之前,需要确保源索引是只读状态,使用以下请求能修改索引的读写状态:
PUT /test-index-03/_settings
{
"settings": {
"index.blocks.write": true
}
}
@Test
public void updateIndexConfig() throws IOException {
String content = readFile("indexConfig.json");
String indexName = "test-index-03";
String url = baseUrl + indexName + "/_settings";
String result = RestClient.putWithJson(url, content, null);
System.out.println("result = " + result);
}
克隆索引
POST /originIndex/_clone/targetIndex
@Test
public void cloneIndexTest() {
String originIndex = "test-index-03";
String targetIndex = "test-index-04";
String url = baseUrl + originIndex + "/_clone/" + targetIndex;
String result = RestClient.postWithJson(url, null, null);
System.out.println("result = " + result);
}
克隆索引时,能复制大部分的配置信息,但是也有一些配置不能复制(例如:index.number_of_replicas
,index.auto_expand_replicas
),可以在克隆时,指定新索引的配置信息
对于配置index.number_of_shards
配置,如果指定,必须和源index
相同
POST /originIndex/_clone/targetIndex
{
"settings": {
"index.number_of_shards": 3
},
"aliases": {
"my_search_indices": {}
}
}
@Test
public void cloneIndexTest() {
String originIndex = "test-index-03";
String targetIndex = "test-index-04";
String url = baseUrl + originIndex + "/_clone/" + targetIndex;
String indexConfig = readFile("indexConfig.json");
String result = RestClient.postWithJson(url, indexConfig, null);
System.out.println("result = " + result);
}
1.8 修改索引配置
修改最大查询数量,默认情况下,es最大能查询10000条数据
PUT /test-index-03/_settings
{
"settings": {
"index.max_result_window": 100000
}
}
@Test
public void updateMaxResult() {
String indexConfig = readFile("indexConfig.json");
String indexName = "test-index-03";
String url = baseUrl + indexName + "_settings";
String result = RestClient.putWithJson(url, indexConfig, null);
System.out.println("result = " + result);
}
1.9 收缩索引
1.10 扩展索引
1.11 模糊搜索索引
GET /_resolve/index/test-index*?expand_wildcards=all
@Test
public void queryIndexTest() {
String indexName = "test-index*";
String url = baseUrl + "_resolve/index/" + indexName;
Map<String, String> paramMap = new HashMap<>();
// 查询索引匹配上的索引,不管索引目前处于何种状态
paramMap.put("expand_wildcards", "all");
String result = RestClient.get(url, paramMap, null);
System.out.println("result = " + result);
}
参数expand_wildcards
可选的值有:
all
:查询所有匹配上的索引open
:匹配状态为open
或non-hidden
的索引closed
:匹配状态为closed
或non-hidden
的索引hidden
:匹配状态为hidden
的索引none
:不匹配任何索引