遇见安多芬 2019-06-23 10:34 采纳率: 0%
浏览 288

elasticsearch 单机模式下 分片shard 是5个?我只创建了【product和】不应该是1个shard吗?

{
"cluster_name": "elastic",
"status": "yellow",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
** "active_primary_shards": 6,
"active_shards": 6,**

"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 6,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 50
}


  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-10 12:58
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在 Elasticsearch 的单机模式下,一个 shard 可以有多个分片。这取决于你如何配置你的索引(例如,是否使用 sharding 或分片)。如果你的集群只有一个节点,那么每个索引都将在单个节点上运行,这意味着每个索引都将有一个唯一的 Shard。

    然而,在实际生产环境中,通常会为每个索引分配多个 Shard。这样做的好处是,当某个 Shard 失效时,其他 Shard 可以被用来替代它,并保持服务可用性。此外,将索引分割成多个 Shard 还可以提高查询性能,因为 Elasticsearch 可以并行处理请求。

    如果你只是创建了一个名为 product 的分片,而没有创建其他分片,那么实际上 Elasticsearch 将不会为你创建任何额外的分片来支持 product 索引。你需要手动为 product 索引添加分片,或者在集群中创建新的分片以满足需求。以下是一个简单的示例:

    public class CreateShardCommand extends BulkCreateIndexCommand {
        private final String index;
        private final int numberOfShards;
        private final int numberOfReplicas;
    
        public CreateShardCommand(String index, int numberOfShards, int numberOfReplicas) {
            this.index = index;
            this.numberOfShards = numberOfShards;
            this.numberOfReplicas = numberOfReplicas;
        }
    
        @Override
        protected void doExecute() throws IOException {
            // 在这里执行你的操作,比如添加分片到现有索引
            IndexRequest request = new IndexRequest(index);
            request.id("1234");
            request.source(new Document().field("name", "John").field("age", 28));
            client.prepareIndex(index, "type")
                    .setSource(request)
                    .execute()
                    .actionGet();
        }
    }
    

    这个示例展示了如何为 product 索引创建一个新的分片。在这个例子中,我们正在创建一个分片,其数量与我们的索引数量相同。然后,我们使用 client.prepareIndex() 来创建一个新的索引,并设置 source 属性为包含文档的 Document 对象。最后,我们使用 execute() 方法执行请求。

    请注意,这只是一个基本示例,实际操作可能需要根据你的具体需求进行调整。

    评论

报告相同问题?