ElasticSearch7.3学习(十六)----RestHighLevelClient Java api实现索引的创建、删除、是否存在、关闭、开启

1、写在前面

注意:导入的包区别,不同的包创建索引的方式不同。博主亲身实践,具体体现在createIndexRequest.mapping()里面。读者可自行试验。

import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;

 由此可以猜想一下:

import org.elasticsearch.client.indices.*;
import org.elasticsearch.action.admin.indices.*;

可以看到上述两种方式导入的包的子类名是相同的,但是具体对索引的操作方式可能是不同的。具体的区别博主暂时还不清楚,后续若有进展再在此处更新。

2、同步创建索引

例如要实现如下索引的创建

PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "description": {
        "type": "text"
      },
      "price": {
        "type": "long"
      },
      "pic": {
        "type": "text",
        "index": false
      }
    }
  },
  "aliases": {
    "default_index": {}
  }
}

代码如下(3种方式):

    //创建索引
    @Test
    public void testCreateIndex() throws IOException {

        //创建索引对象
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book");
        //设置参数
        createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0"));
        //指定映射1
        createIndexRequest.mapping(" {\n" +
                " \t\"properties\": {\n" +
                "            \"name\":{\n" +
                "             \"type\":\"keyword\"\n" +
                "           },\n" +
                "           \"description\": {\n" +
                "              \"type\": \"text\"\n" +
                "           },\n" +
                "            \"price\":{\n" +
                "             \"type\":\"long\"\n" +
                "           },\n" +
                "           \"pic\":{\n" +
                "             \"type\":\"text\",\n" +
                "             \"index\":false\n" +
                "           }\n" +
                " \t}\n" +
                "}", XContentType
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

|旧市拾荒|

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值