In this guide, we will look at some advanced index actions that are not covered in the Index Lifecycle guide.
Let's create a client instance, and an index named movies
:
const { Client } = require('@opensearch-project/opensearch');
const client = new Client({
node: 'http://localhost:9200',
});
client.indices.create({index: 'movies'})
You can clear the cache of an index or indices by using the indices.clearCache
API action. The following example clears the cache of the movies
index:
await client.indices.clearCache({index: 'movies'})
By default, the indices.clearCache
API action clears all types of cache. To clear specific types of cache pass the the query
, fielddata
, or request
parameter to the API action:
await client.indices.clearCache({index: 'movies', query: true})
await client.indices.clearCache({index: 'movies', fielddata: true, request: true})
Sometimes you might want to flush an index or indices to make sure that all data in the transaction log is persisted to the index. To flush an index or indices use the indices.flush
API action. The following example flushes the movies
index:
await client.indices.flush({index: 'movies'})
You can refresh an index or indices to make sure that all changes are available for search. To refresh an index or indices use the indices.refresh
API action:
await client.indices.refresh({index: 'movies'})
You can close an index to prevent read and write operations on the index. A closed index does not have to maintain certain data structures that an opened index require, reducing the memory and disk space required by the index. The following example closes and reopens the movies
index:
await client.indices.close({index: 'movies'})
await client.indices.open({index: 'movies'})
You can force merge an index or indices to reduce the number of segments in the index. This can be useful if you have a large number of small segments in the index. Merging segments reduces the memory footprint of the index. Do note that this action is resource intensive and it is only recommended for read-only indices. The following example force merges the movies
index:
await client.indices.forcemerge({index: 'movies'})
You can clone an index to create a new index with the same mappings, data, and MOST of the settings. The source index must be in read-only state for cloning. The following example blocks write operations from movies
index, clones the said index to create a new index named movies_clone
, then re-enables write:
await client.indices.addBlock({index: 'movies', block: 'write'})
await client.indices.clone({index: 'movies', target: 'movies_clone'})
await client.indices.putSettings({index: 'movies', body: { index: { blocks: { write: false } } } })
You can split an index into another index with more primary shards. The source index must be in read-only state for splitting. The following example create the read-only books
index with 30 routing shards and 5 shards (which is divisible by 30), splits index into bigger_books
with 10 shards (which is also divisible by 30), then re-enables write:
await client.indices.create({
index: 'books',
body: { settings: {
index: { number_of_shards: 5,
number_of_routing_shards: 30,
blocks: { write: true } } } } })
await client.indices.split({
index: 'books',
target: 'bigger_books',
body: { settings: { index: { number_of_shards: 10 } } } })
await client.indices.putSettings({index: 'books', body: { index: { blocks: { write: false } } } })
Let's delete all the indices we created in this guide:
await client.indices.delete({index: ['movies', 'books', 'movies_clone', 'bigger_books']});