Know-How
Elasticsearch tutorial
Elasticsearch je distribuovaný search engine. Full-text search, log analytics, real-time aggregations.
Indexing
# Create index
PUT /products
{ "mappings": { "properties": {
"name": { "type": "text", "analyzer": "czech" },
"price": { "type": "float" },
"category": { "type": "keyword" }
}}}
# Index document
POST /products/_doc
{ "name": "Notebook Lenovo", "price": 25990, "category": "electronics" }
Search
# Full-text search
GET /products/_search
{ "query": {
"bool": {
"must": [{ "match": { "name": "notebook" } }],
"filter": [
{ "range": { "price": { "lte": 30000 } } },
{ "term": { "category": "electronics" } }
]
}
}}
Aggregations
GET /products/_search
{ "size": 0, "aggs": {
"by_category": {
"terms": { "field": "category" },
"aggs": { "avg_price": { "avg": { "field": "price" } } }
}
}}
Klíčový takeaway
Elasticsearch pro full-text search a analytics. Keyword pro exact match, text pro full-text. Aggregations pro BI.