Skip to content

Creating an index on a single Elasticsearch node

When creating an index on a single Elasticsearch node, it is important that the number_of_replicas is set correctly. This is because this value conveys how many replication instances the data is to be copied. In many online examples or templates, the number_of_replicas is assigned the value 2 by default.

Thus, in the case of single Elasticsearch setup, it is not necessary to set the replica higher than 0 since the Primary Shard is not replicated.
Not setting this value correctly will result in a yellow/warning status in Elasticsearch.
So when you create an index, on a single Elasticsearch setup, the number_of_replicas is set to 0.

Example
$client = ClientBuilder::create()->build();
$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 3,
            'number_of_replicas' => 0
        ],
        'mappings' => [
            'my_type' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'first_name' => [
                        'type' => 'string',
                        'analyzer' => 'standard'
                    ],
                    'age' => [
                        'type' => 'integer'
                    ]
                ]
            ]
        ]
    ]
];

// Create the index with mappings and settings now
$response = $client->indices()->create($params);

Additional information can be found in the Elasticsearch documentation.