Індекси та mappings в Elasticsearch
Mapping — схема для індексу: визначає типи полів, аналізатори та як поля індексуються для пошуку.
Створення індексу з mapping
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2,
"index.analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "stop"]
}
}
}
},
"mappings": {
"properties": {
"id": { "type": "keyword" },
"name": {
"type": "text",
"analyzer": "custom_analyzer",
"fields": {
"raw": { "type": "keyword" }
}
},
"description": { "type": "text" },
"price": { "type": "scaled_float", "scaling_factor": 100 },
"stock": { "type": "integer" },
"category": { "type": "keyword" },
"created_at": { "type": "date" },
"tags": { "type": "keyword" },
"location": { "type": "geo_point" }
}
}
}
Поширені типи полів
- text — аналізований текст (підтримка пошуку)
- keyword — точний збіг (для фільтрування, агрегування)
- integer, long — числа
- double, scaled_float — десяткові
- date — дата/час
- boolean — true/false
- geo_point — координати
- nested — масив об'єктів
- object — вбудований об'єкт
Переіндексування зі змінами mapping
# Створити новий індекс з новим mapping
PUT /products_v2
{ ... new mapping ... }
# Переіндексувати дані зі старого на новий
POST /_reindex
{
"source": { "index": "products" },
"dest": { "index": "products_v2" },
"script": {
"source": "ctx._source.new_field = 'default'"
}
}
# Переключити alias
POST /_aliases
{
"actions": [
{ "remove": { "index": "products", "alias": "products_alias" } },
{ "add": { "index": "products_v2", "alias": "products_alias" } }
]
}







