Elasticsearch(ES) 创建索引

欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习、面试资源哟!!

个人网站: https://www.exception.site/elasticsearch/elasticsearch-create-index

一、开始创建索引

您可以通过 Elasticsearch 的 RESTFul API 来创建索引:

PUT http://127.0.0.1:9200/commodity

注意:默认情况下,创建的索引分片数量是 5 个,副本数量是 1 个。

您可以通过如下参数来指定分片数、副本数量:

{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
    }
}

1.1 实战演示

通过 CURL 命令来上手操作一下,我们尝试创建一个商品索引, 看下效果:

curl -X PUT "localhost:9200/commodity?pretty"

索引创建成功会返回以下出参:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "commodity"
}

如下图所示:

二、创建带有类型、映射的索引(Index)

其实,我们可以在创建索引的时候,同时将索引的类型、以及映射一并创建好:

curl -X PUT "localhost:9200/commodity?pretty"

入参:

扫描二维码关注公众号,回复: 7264120 查看本文章
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
    },
    "mapping": {
        "_doc": {
            "properties": {
                "commodity_id": {
                    "type": "long"
                },
                "commodity_name": {
                    "type": "text"
                },
                "picture_url": {
                    "type": "keyword"
                },
                "price": {
                    "type": "double"
                }
            }
        }
    }
}

我们创建了一个分片数为 3,副本数为 2 的索引,同时,定义了一个 _doc 的类型,里面包含了 4 个字段,类型各不相同。

接下来,我们用 Postman 工具来一次性创建带有类型、映射的索引(Index):

这里应为笔者通过 CURL 创建索引,由于带入参,出现了格式错误的问题,改用了 Postman 工具,效果相同。

三、修改索引的副本数

我们可以通过如下 API 来修改索引的副本数:

PUT http://127.0.0.1:9200/commodity/_settings

入参:

{
    "number_of_replicas": 3
}

我们将 commodity 索引副本数更新为了 3:

猜你喜欢

转载自www.cnblogs.com/quanxiaoha2/p/11512623.html