Managing Indices
Learn how to create, update and delete indices with CBElasticsearch
Last updated
Was this helpful?
Learn how to create, update and delete indices with CBElasticsearch
Last updated
Was this helpful?
Elasticsearch documents are stored in an "index", with the document structure defined by a "mapping". An Elasticsearch index is a JSON document store, and the mapping is a JSON configuration which defines the data type Elasticsearch should use for each document field.
By default, Elasticsearch will dynamically generate these index mapping when a document is saved to the index. See for more details.
To retrieve a list of all indices on the connected cluster, use the client getIndices
method:
This will return a struct of all indices ( with the names as keys ), which will provide additional information on each index, such as:
Any assigned aliases
The number of documents in the index
The size of the storage space used for the index in bytes
The IndexBuilder
model assists with the creation and mapping of indices. Mappings define the allowable data types within your documents and allow for better and more accurate search aggregations. Let's say we have a book model that we intend to make searchable via a bookshop
index. Let's go ahead and create the index using the IndexBuilder:
This will create an empty index which we can begin populating with documents.
To avoid the inherent troubles with dynamic mappings, you can define an explicit mapping using the properties
argument:
In the previous examples, we've created the index and mapping from the IndexBuilder itself. If we wish, we could instead pass the IndexBuilder
object to the Client@cbelasticsearch
instance's applyIndex( required IndexBuilder indexBuilder )
method:
The IndexBuilder
model also provides a patch()
convenience method for updating the mapping or settings on an index:
Here's a quick example of using indexBuilder.patch()
to add two new fields to an existing reviews
index:
To retreive a list of all settings for an index you may use the getSettings
method on the client.
To retreive a list of the configured mappings for an index you may use the getMappings
method on the client.
On occasion, you may need to ensure the index is updated in real time (immediately and synchronously). This can be done via the refreshIndex()
client method:
You can refresh multiple indices at once:
To retrieve statistics on an index, use the getIndexStats()
method:
You can retrieve particular statistics metrics:
Or all metrics:
You can even retrieve all metrics on all indices by skipping the indexName
parameter entirely:
Finally, you can pass a struct of parameters to fine-tune the statistics result:
Certain index-level settings can not be applied while the index is open. To solve this, CBElasticsearch offers the .closeIndex()
and .openIndex()
methods:
Each of these methods accepts a struct of name/value (simple values only) arguments to pass in the query string:
All good things must come to an end, eh? You can use Client.deleteIndex()
to delete an existing index:
Or you can use IndexBuilder.delete()
:
So far we've passed a simple struct of field mappings in to the index properties. If we wanted to add additional settings or configure replicas and shards, we could pass a more comprehensive struct, including a to the new()
method to do so:
as well as pass to the refresh endpoint. This can be useful when using wildcards in the index/alias names:
Elasticsearch allows , which are fields calculated at search time and returned in the "fields"
array.
This summarized_emotions
field to display an array of emotions matching the review summary.
See for more information.
Deprecation notice: Custom index types since Elasticsearch v7.0, and should no longer be used. Only a single type will be accepted in future releases.