Skip to content

Latest commit

 

History

History
59 lines (52 loc) · 1.37 KB

File metadata and controls

59 lines (52 loc) · 1.37 KB
endpoint indices.create
lang python
es_version 9.3
client elasticsearch==9.3.0

Elasticsearch 9.3 indices.create endpoint (Python example)

Use client.indices.create() to create a new index with explicit field mappings:

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).

Custom settings

Pass settings to control shards, replicas, and analysis:

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"},
        }
    },
)