| endpoint | indices.create |
|---|---|
| lang | javascript |
| es_version | 9.3 |
| client | @elastic/elasticsearch@9.3.0 |
Use client.indices.create() to create a new index with explicit
field mappings:
await client.indices.create({
index: "products",
mappings: {
properties: {
name: { type: "text" },
price: { type: "float" },
category: { type: "keyword" },
in_stock: { type: "boolean" },
created_at: { type: "date" },
},
},
});Fields not listed in the mappings will still be indexed using
dynamic mapping. Define mappings explicitly for fields where you
need a specific type (e.g. keyword instead of text).
Pass settings to control shards, replicas, and analysis:
await client.indices.create({
index: "products",
settings: {
number_of_shards: 2,
number_of_replicas: 1,
analysis: {
analyzer: {
lowercase_keyword: {
type: "custom",
tokenizer: "keyword",
filter: ["lowercase"],
},
},
},
},
mappings: {
properties: {
name: { type: "text" },
sku: { type: "text", analyzer: "lowercase_keyword" },
},
},
});