ES索引API

本文详细对比了Elasticsearch(ES)和MySQL在数据库、索引和文档操作上的差异,包括创建、查询、删除索引的过程,并介绍了ES特有的设置如克隆、修改配置和滚动搜索等功能,提供了丰富的示例代码。
摘要由CSDN通过智能技术生成

esmysql对比

MySQLES
databaseindex
tabledoc
rowdocument

一、索引操作

前置信息:

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_replicasindex.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:匹配状态为opennon-hidden的索引
  • closed:匹配状态为closednon-hidden的索引
  • hidden:匹配状态为hidden的索引
  • none:不匹配任何索引

1.12 滚动索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值