Complete index lifecycle management including creation, deletion, mappings, settings, aliases, templates, and maintenance operations for organizing and optimizing data storage in Elasticsearch.
Create new indices with mappings, settings, and aliases.
/**
* Create a new index
* @param params - Index creation parameters
* @returns Promise with creation result
*/
function create(params: {
/** Index name */
index: IndexName;
/** Index aliases */
aliases?: Record<IndexName, IndicesAlias>;
/** Field mappings */
mappings?: MappingTypeMapping;
/** Index settings */
settings?: IndicesIndexSettings;
/** Wait for active shards */
wait_for_active_shards?: WaitForActiveShards;
/** Request timeout */
timeout?: Duration;
/** Master node timeout */
master_timeout?: Duration;
}): Promise<TransportResult<IndicesCreateResponse, unknown>>;
interface IndicesCreateResponse {
acknowledged: boolean;
shards_acknowledged: boolean;
index: IndexName;
}
interface IndicesAlias {
filter?: QueryDslQueryContainer;
index_routing?: Routing;
search_routing?: Routing;
is_write_index?: boolean;
is_hidden?: boolean;
}
interface MappingTypeMapping {
properties?: Record<PropertyName, MappingProperty>;
dynamic?: MappingDynamicMapping;
dynamic_templates?: MappingDynamicTemplate[];
date_detection?: boolean;
numeric_detection?: boolean;
_source?: MappingSourceField;
_routing?: MappingRoutingField;
meta?: Record<string, any>;
}Usage Examples:
// Create basic index
await client.indices.create({
index: "my-index"
});
// Create index with mappings and settings
await client.indices.create({
index: "products",
mappings: {
properties: {
name: { type: "text", analyzer: "standard" },
price: { type: "double" },
category: { type: "keyword" },
description: { type: "text" },
created_at: { type: "date" },
tags: { type: "keyword" },
location: { type: "geo_point" }
}
},
settings: {
number_of_shards: 2,
number_of_replicas: 1,
analysis: {
analyzer: {
custom_analyzer: {
type: "standard",
stopwords: "_english_"
}
}
}
},
aliases: {
"products-alias": {},
"current-products": {
filter: { term: { status: "active" } }
}
}
});Delete indices and handle index patterns.
/**
* Delete one or more indices
* @param params - Index deletion parameters
* @returns Promise with deletion result
*/
function delete(params: {
/** Index names or patterns */
index: Indices;
/** Allow no indices to match */
allow_no_indices?: boolean;
/** Expand wildcard expressions */
expand_wildcards?: ExpandWildcards;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Request timeout */
timeout?: Duration;
/** Master node timeout */
master_timeout?: Duration;
}): Promise<TransportResult<IndicesDeleteResponse, unknown>>;
interface IndicesDeleteResponse {
acknowledged: boolean;
}Usage Examples:
// Delete single index
await client.indices.delete({
index: "old-index"
});
// Delete multiple indices
await client.indices.delete({
index: ["index-1", "index-2", "logs-*"]
});
// Safe deletion with error handling
await client.indices.delete({
index: "maybe-missing-index",
ignore_unavailable: true
});Manage field mappings for data structure definition.
/**
* Update index mappings
* @param params - Mapping update parameters
* @returns Promise with mapping update result
*/
function putMapping(params: {
/** Target indices */
index: Indices;
/** Field properties */
properties?: Record<PropertyName, MappingProperty>;
/** Dynamic mapping behavior */
dynamic?: MappingDynamicMapping;
/** Dynamic templates */
dynamic_templates?: MappingDynamicTemplate[];
/** Source field configuration */
_source?: MappingSourceField;
/** Routing field configuration */
_routing?: MappingRoutingField;
/** Metadata */
meta?: Record<string, any>;
/** Request timeout */
timeout?: Duration;
/** Master node timeout */
master_timeout?: Duration;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Write index only */
write_index_only?: boolean;
}): Promise<TransportResult<IndicesPutMappingResponse, unknown>>;
/**
* Get index mappings
* @param params - Mapping retrieval parameters
* @returns Promise with mapping information
*/
function getMapping(params?: {
/** Target indices */
index?: Indices;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Include type name */
include_type_name?: boolean;
/** Master node timeout */
master_timeout?: Duration;
/** Local operation */
local?: boolean;
}): Promise<TransportResult<IndicesGetMappingResponse, unknown>>;Usage Examples:
// Add new fields to existing mapping
await client.indices.putMapping({
index: "products",
properties: {
brand: { type: "keyword" },
specifications: {
type: "object",
properties: {
weight: { type: "double" },
dimensions: { type: "text" }
}
},
reviews: {
type: "nested",
properties: {
rating: { type: "integer" },
comment: { type: "text" }
}
}
}
});
// Get current mappings
const mappings = await client.indices.getMapping({
index: "products"
});Configure index-level settings for performance and behavior.
/**
* Update index settings
* @param params - Settings update parameters
* @returns Promise with settings update result
*/
function putSettings(params: {
/** Target indices */
index?: Indices;
/** Index settings */
settings: IndicesIndexSettings;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Flat settings format */
flat_settings?: boolean;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Master node timeout */
master_timeout?: Duration;
/** Preserve existing settings */
preserve_existing?: boolean;
/** Request timeout */
timeout?: Duration;
}): Promise<TransportResult<IndicesPutSettingsResponse, unknown>>;
/**
* Get index settings
* @param params - Settings retrieval parameters
* @returns Promise with settings information
*/
function getSettings(params?: {
/** Target indices */
index?: Indices;
/** Setting names to retrieve */
name?: Names;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Flat settings format */
flat_settings?: boolean;
/** Include defaults */
include_defaults?: boolean;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Local operation */
local?: boolean;
/** Master node timeout */
master_timeout?: Duration;
}): Promise<TransportResult<IndicesGetSettingsResponse, unknown>>;
interface IndicesIndexSettings {
number_of_shards?: number;
number_of_replicas?: number;
refresh_interval?: Duration;
max_result_window?: number;
max_rescore_window?: number;
blocks?: IndicesIndexSettingsBlocks;
analysis?: IndicesIndexSettingsAnalysis;
mapping?: IndicesIndexSettingsMapping;
// ... many more settings
}Usage Examples:
// Update replica count
await client.indices.putSettings({
index: "my-index",
settings: {
number_of_replicas: 2
}
});
// Update refresh interval for better indexing performance
await client.indices.putSettings({
index: "bulk-loading-index",
settings: {
refresh_interval: "30s",
number_of_replicas: 0 // Disable replicas during bulk loading
}
});
// Get current settings
const settings = await client.indices.getSettings({
index: "my-index",
name: ["number_of_replicas", "refresh_interval"]
});Manage index aliases for flexible index references.
/**
* Create or update index aliases
* @param params - Alias management parameters
* @returns Promise with alias operation result
*/
function updateAliases(params: {
/** Array of alias actions */
actions: IndicesAliasAction[];
/** Master node timeout */
master_timeout?: Duration;
/** Request timeout */
timeout?: Duration;
}): Promise<TransportResult<IndicesUpdateAliasesResponse, unknown>>;
/**
* Create alias
* @param params - Alias creation parameters
* @returns Promise with alias creation result
*/
function putAlias(params: {
/** Target indices */
index: Indices;
/** Alias name */
name: Name;
/** Alias filter */
filter?: QueryDslQueryContainer;
/** Index routing */
index_routing?: Routing;
/** Search routing */
search_routing?: Routing;
/** Is write index */
is_write_index?: boolean;
/** Master node timeout */
master_timeout?: Duration;
/** Request timeout */
timeout?: Duration;
}): Promise<TransportResult<IndicesPutAliasResponse, unknown>>;
/**
* Get aliases
* @param params - Alias retrieval parameters
* @returns Promise with alias information
*/
function getAlias(params?: {
/** Target indices */
index?: Indices;
/** Alias names */
name?: Names;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Local operation */
local?: boolean;
}): Promise<TransportResult<IndicesGetAliasResponse, unknown>>;
interface IndicesAliasAction {
add?: IndicesAliasActionAdd;
remove?: IndicesAliasActionRemove;
remove_index?: IndicesAliasActionRemoveIndex;
}Usage Examples:
// Atomic alias operations
await client.indices.updateAliases({
actions: [
{ remove: { index: "old-index", alias: "current-data" } },
{ add: { index: "new-index", alias: "current-data" } }
]
});
// Create filtered alias
await client.indices.putAlias({
index: "logs-*",
name: "error-logs",
filter: {
term: { level: "ERROR" }
}
});
// List all aliases
const aliases = await client.indices.getAlias({
name: "*"
});Manage index templates for automatic index configuration.
/**
* Create or update index template
* @param params - Template creation parameters
* @returns Promise with template creation result
*/
function putIndexTemplate(params: {
/** Template name */
name: Name;
/** Index patterns */
index_patterns: string[];
/** Template content */
template?: IndicesIndexTemplate;
/** Composed component templates */
composed_of?: Name[];
/** Template priority */
priority?: number;
/** Template version */
version?: VersionNumber;
/** Template metadata */
_meta?: Metadata;
/** Data stream configuration */
data_stream?: IndicesDataStreamVisibility;
/** Allow auto create */
allow_auto_create?: boolean;
/** Master node timeout */
master_timeout?: Duration;
/** Create only if not exists */
create?: boolean;
}): Promise<TransportResult<IndicesPutIndexTemplateResponse, unknown>>;
/**
* Get index templates
* @param params - Template retrieval parameters
* @returns Promise with template information
*/
function getIndexTemplate(params?: {
/** Template name */
name?: Name;
/** Flat settings */
flat_settings?: boolean;
/** Local operation */
local?: boolean;
/** Master node timeout */
master_timeout?: Duration;
}): Promise<TransportResult<IndicesGetIndexTemplateResponse, unknown>>;
interface IndicesIndexTemplate {
aliases?: Record<IndexName, IndicesAlias>;
mappings?: MappingTypeMapping;
settings?: IndicesIndexSettings;
lifecycle?: IndicesIndexTemplateSummaryLifecycle;
}Usage Examples:
// Create index template
await client.indices.putIndexTemplate({
name: "logs-template",
index_patterns: ["logs-*"],
template: {
settings: {
number_of_shards: 1,
number_of_replicas: 1,
refresh_interval: "10s"
},
mappings: {
properties: {
timestamp: { type: "date" },
level: { type: "keyword" },
message: { type: "text" },
host: { type: "keyword" },
service: { type: "keyword" }
}
},
aliases: {
"all-logs": {}
}
},
priority: 100
});
// Get templates
const templates = await client.indices.getIndexTemplate({
name: "logs-*"
});Perform maintenance operations on indices.
/**
* Open closed indices
* @param params - Open parameters
* @returns Promise with open result
*/
function open(params: {
/** Target indices */
index: Indices;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Master node timeout */
master_timeout?: Duration;
/** Request timeout */
timeout?: Duration;
/** Wait for active shards */
wait_for_active_shards?: WaitForActiveShards;
}): Promise<TransportResult<IndicesOpenResponse, unknown>>;
/**
* Close indices
* @param params - Close parameters
* @returns Promise with close result
*/
function close(params: {
/** Target indices */
index: Indices;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Master node timeout */
master_timeout?: Duration;
/** Request timeout */
timeout?: Duration;
/** Wait for active shards */
wait_for_active_shards?: WaitForActiveShards;
}): Promise<TransportResult<IndicesCloseResponse, unknown>>;
/**
* Refresh indices
* @param params - Refresh parameters
* @returns Promise with refresh result
*/
function refresh(params?: {
/** Target indices */
index?: Indices;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
}): Promise<TransportResult<IndicesRefreshResponse, unknown>>;
/**
* Flush indices
* @param params - Flush parameters
* @returns Promise with flush result
*/
function flush(params?: {
/** Target indices */
index?: Indices;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Force flush */
force?: boolean;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Wait if ongoing */
wait_if_ongoing?: boolean;
}): Promise<TransportResult<IndicesFlushResponse, unknown>>;
/**
* Force merge segments
* @param params - Force merge parameters
* @returns Promise with force merge result
*/
function forcemerge(params?: {
/** Target indices */
index?: Indices;
/** Allow no indices */
allow_no_indices?: boolean;
/** Expand wildcards */
expand_wildcards?: ExpandWildcards;
/** Flush after merge */
flush?: boolean;
/** Ignore unavailable indices */
ignore_unavailable?: boolean;
/** Maximum segments */
max_num_segments?: number;
/** Only expunge deletes */
only_expunge_deletes?: boolean;
/** Wait for completion */
wait_for_completion?: boolean;
}): Promise<TransportResult<IndicesForcemergeResponse, unknown>>;Usage Examples:
// Refresh indices to make recent changes searchable
await client.indices.refresh({
index: "my-index"
});
// Close index to save resources
await client.indices.close({
index: "old-logs-*"
});
// Reopen closed index
await client.indices.open({
index: "old-logs-2023-01"
});
// Force merge to optimize segments
await client.indices.forcemerge({
index: "completed-index",
max_num_segments: 1,
wait_for_completion: true
});
// Flush to ensure data is written to disk
await client.indices.flush({
index: "critical-data"
});type IndexName = string;
type Indices = string | string[];
type Names = string | string[];
type Name = string;
type PropertyName = string;
type Duration = string;
type Routing = string;
type VersionNumber = number;
type WaitForActiveShards = number | 'all';
type ExpandWildcards = 'all' | 'open' | 'closed' | 'hidden' | 'none';
type MappingDynamicMapping = boolean | 'strict' | 'runtime';
type Metadata = Record<string, any>;
interface MappingProperty {
type?: string;
analyzer?: string;
search_analyzer?: string;
properties?: Record<PropertyName, MappingProperty>;
fields?: Record<string, MappingProperty>;
format?: string;
// ... many more mapping options
}
interface QueryDslQueryContainer {
term?: Record<string, any>;
match?: Record<string, any>;
bool?: Record<string, any>;
// ... other query types
}