Elasticsearch ist eine verteilte Suchmaschine für Volltextsuche, Log-Analyse und Analytics. Es ist das Herz des ELK-Stacks (Elasticsearch, Logstash, Kibana).

Warum Elasticsearch?

Anwendungsfälle

- Volltextsuche für Websites/Apps
- Log-Analyse (mit ELK Stack)
- E-Commerce-Produktsuche
- Application Performance Monitoring
- Security Analytics (SIEM)
- Business Analytics

Eigenschaften

| Feature | Beschreibung | |---------|--------------| | Verteilt | Cluster aus mehreren Nodes | | Schema-frei | JSON-Dokumente | | RESTful | HTTP/JSON API | | Near Real-Time | ~1 Sekunde Indexierung | | Skalierbar | Horizontal skalierbar |

Installation

Systemanforderungen

- Java 11 oder höher (integriert ab ES 7.x)
- 2 GB RAM minimum (4+ GB empfohlen)
- 64-Bit System

Debian/Ubuntu

# Elasticsearch GPG Key
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

# Repository hinzufügen
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

# Installieren
apt update
apt install elasticsearch

Starten

systemctl enable --now elasticsearch

Installation prüfen

curl -X GET "localhost:9200"

Konfiguration

elasticsearch.yml

# /etc/elasticsearch/elasticsearch.yml

# Cluster-Name
cluster.name: my-cluster

# Node-Name
node.name: node-1

# Datenverzeichnis
path.data: /var/lib/elasticsearch

# Log-Verzeichnis
path.logs: /var/log/elasticsearch

# Netzwerk
network.host: 0.0.0.0
http.port: 9200

# Discovery (Single-Node)
discovery.type: single-node

# Sicherheit (Elasticsearch 8+)
xpack.security.enabled: false  # Für Tests

JVM-Einstellungen

# /etc/elasticsearch/jvm.options.d/heap.options

# Heap-Größe (50% des verfügbaren RAM, max 32 GB)
-Xms4g
-Xmx4g

Konfiguration anwenden

systemctl restart elasticsearch

Grundlegende Operationen

Index erstellen

curl -X PUT "localhost:9200/mein-index"

Index mit Mapping

curl -X PUT "localhost:9200/produkte" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "beschreibung": { "type": "text" },
      "preis": { "type": "float" },
      "kategorie": { "type": "keyword" },
      "erstellt_am": { "type": "date" },
      "verfuegbar": { "type": "boolean" }
    }
  }
}'

Dokument indexieren

# Mit automatischer ID
curl -X POST "localhost:9200/produkte/_doc" -H 'Content-Type: application/json' -d'
{
  "name": "Laptop XYZ",
  "beschreibung": "Leistungsstarker Laptop mit 16 GB RAM",
  "preis": 999.99,
  "kategorie": "elektronik",
  "erstellt_am": "2026-01-26",
  "verfuegbar": true
}'

# Mit eigener ID
curl -X PUT "localhost:9200/produkte/_doc/1" -H 'Content-Type: application/json' -d'
{
  "name": "Monitor ABC",
  "beschreibung": "27 Zoll 4K Monitor",
  "preis": 399.99,
  "kategorie": "elektronik",
  "erstellt_am": "2026-01-26",
  "verfuegbar": true
}'

Dokument abrufen

curl -X GET "localhost:9200/produkte/_doc/1"

Dokument aktualisieren

curl -X POST "localhost:9200/produkte/_update/1" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "preis": 349.99
  }
}'

Dokument löschen

curl -X DELETE "localhost:9200/produkte/_doc/1"

Index löschen

curl -X DELETE "localhost:9200/mein-index"

Suche

Einfache Suche

# Alle Dokumente
curl -X GET "localhost:9200/produkte/_search"

# Volltextsuche
curl -X GET "localhost:9200/produkte/_search?q=laptop"

Query DSL

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "beschreibung": "laptop"
    }
  }
}'

Bool-Query

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "kategorie": "elektronik" } }
      ],
      "filter": [
        { "range": { "preis": { "lte": 500 } } }
      ],
      "should": [
        { "match": { "beschreibung": "monitor" } }
      ]
    }
  }
}'

Filter (exact match)

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "kategorie": "elektronik"
    }
  }
}'

Range-Query

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "range": {
      "preis": {
        "gte": 100,
        "lte": 500
      }
    }
  }
}'

Sortierung

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": [
    { "preis": "asc" },
    { "name.keyword": "desc" }
  ]
}'

Pagination

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "from": 0,
  "size": 10,
  "query": { "match_all": {} }
}'

Aggregationen

Durchschnitt

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "durchschnittspreis": {
      "avg": { "field": "preis" }
    }
  }
}'

Gruppierung (Terms)

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "kategorien": {
      "terms": { "field": "kategorie" }
    }
  }
}'

Preis-Bereiche

curl -X GET "localhost:9200/produkte/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "preis_bereiche": {
      "range": {
        "field": "preis",
        "ranges": [
          { "to": 100 },
          { "from": 100, "to": 500 },
          { "from": 500 }
        ]
      }
    }
  }
}'

Bulk-Operationen

Bulk-Indexierung

curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
{ "index": { "_index": "produkte" } }
{ "name": "Produkt 1", "preis": 10.99 }
{ "index": { "_index": "produkte" } }
{ "name": "Produkt 2", "preis": 20.99 }
{ "index": { "_index": "produkte" } }
{ "name": "Produkt 3", "preis": 30.99 }
'

Aus Datei

curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @data.json

Index-Verwaltung

Alle Indizes anzeigen

curl -X GET "localhost:9200/_cat/indices?v"

Index-Einstellungen

curl -X GET "localhost:9200/produkte/_settings"

Index-Mapping

curl -X GET "localhost:9200/produkte/_mapping"

Index-Aliases

# Alias erstellen
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
  "actions": [
    { "add": { "index": "produkte", "alias": "shop" } }
  ]
}'

# Über Alias suchen
curl -X GET "localhost:9200/shop/_search"

Reindex

curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
  "source": { "index": "alter-index" },
  "dest": { "index": "neuer-index" }
}'

Cluster-Verwaltung

Cluster-Status

curl -X GET "localhost:9200/_cluster/health?pretty"

Node-Informationen

curl -X GET "localhost:9200/_nodes?pretty"

Cluster-Statistiken

curl -X GET "localhost:9200/_cluster/stats?pretty"

Sicherheit (X-Pack)

Sicherheit aktivieren

# elasticsearch.yml
xpack.security.enabled: true
xpack.security.enrollment.enabled: true

Passwörter setzen

/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive

API mit Authentifizierung

curl -u elastic:passwort -X GET "localhost:9200"

Backup (Snapshots)

Repository einrichten

# elasticsearch.yml
path.repo: ["/backup/elasticsearch"]
curl -X PUT "localhost:9200/_snapshot/backup_repo" -H 'Content-Type: application/json' -d'
{
  "type": "fs",
  "settings": {
    "location": "/backup/elasticsearch"
  }
}'

Snapshot erstellen

curl -X PUT "localhost:9200/_snapshot/backup_repo/snapshot_1?wait_for_completion=true"

Snapshot wiederherstellen

curl -X POST "localhost:9200/_snapshot/backup_repo/snapshot_1/_restore"

Python-Client

from elasticsearch import Elasticsearch

es = Elasticsearch(["localhost:9200"])

# Dokument indexieren
es.index(index="produkte", document={
    "name": "Test-Produkt",
    "preis": 49.99
})

# Suchen
result = es.search(index="produkte", query={
    "match": {"name": "test"}
})

for hit in result['hits']['hits']:
    print(hit['_source'])

Zusammenfassung

| Operation | Endpoint | |-----------|----------| | Index erstellen | PUT /index | | Dokument hinzufügen | POST /index/_doc | | Dokument abrufen | GET /index/_doc/id | | Suchen | GET /index/_search | | Aktualisieren | POST /index/_update/id | | Löschen | DELETE /index/_doc/id | | Index löschen | DELETE /index | | Cluster-Status | GET /_cluster/health |

Fazit

Elasticsearch ist mächtig für Volltextsuche und Datenanalyse. Die Lernkurve ist steil, aber die Möglichkeiten sind enorm. Beginnen Sie mit einem Single-Node-Setup, verstehen Sie die Query DSL und erweitern Sie dann zu Clustern. Für Log-Analyse kombinieren Sie Elasticsearch mit Kibana und Logstash zum vollständigen ELK-Stack.