56 Dev Tools in OpenSearch Dashboards

Die Dev Tools Console in OpenSearch Dashboards ist ein leistungsfähiges Werkzeug für Entwickler, mit dem Sie direkt mit OpenSearch interagieren, Abfragen testen und die Antworten analysieren können.

56.1 Grundlegendes zur Dev Tools Console

56.1.1 Die Console verstehen

Die Dev Tools Console besteht aus zwei Hauptbereichen:

  1. Editor-Bereich (links) - Hier geben Sie Ihre Befehle ein
  2. Antwort-Bereich (rechts) - Hier sehen Sie die Antworten von OpenSearch

56.1.2 Grundlegende Syntax

# Index erstellen
PUT /mein-index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

# Dokument indexieren
POST /mein-index/_doc
{
  "field1": "value1",
  "field2": "value2"
}

# Suche ausführen
GET /mein-index/_search
{
  "query": {
    "match": {
      "field1": "value1"
    }
  }
}

56.2 Fortgeschrittene Console-Funktionen

56.2.1 Multi-Request Ausführung

# Mehrere Anfragen in einem Block
POST _bulk
{"index": {"_index": "test", "_id": "1"}}
{"field": "value1"}
{"index": {"_index": "test", "_id": "2"}}
{"field": "value2"}

# Nächste Anfrage
GET test/_search
{
  "query": {
    "match_all": {}
  }
}

56.2.2 Template-Variablen verwenden

# Variable definieren
POST _scripts/my-template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "{{field}}": "{{value}}"
        }
      }
    }
  }
}

# Template verwenden
GET test/_search/template
{
  "id": "my-template",
  "params": {
    "field": "name",
    "value": "john"
  }
}

56.3 Debugging und Testing

56.3.1 Query Profiling

GET /mein-index/_search
{
  "profile": true,
  "query": {
    "match": {
      "field1": "test"
    }
  }
}

56.3.2 Explain API verwenden

GET /mein-index/_explain/1
{
  "query": {
    "match": {
      "field1": "test"
    }
  }
}

56.4 Performance-Analyse

56.4.1 Analyze API

# Text-Analyse durchführen
GET _analyze
{
  "analyzer": "standard",
  "text": "This is a test"
}

# Index-spezifische Analyse
GET mein-index/_analyze
{
  "field": "field1",
  "text": "This is a test"
}

56.4.2 Hot Threads Analyse

GET /_nodes/hot_threads

56.5 Praktische Beispiele

56.5.1 Index Management

# Index mit Mapping erstellen
PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "price": {
        "type": "float"
      },
      "category": {
        "type": "keyword"
      }
    }
  }
}

# Alias hinzufügen
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "products",
        "alias": "current_products"
      }
    }
  ]
}

56.5.2 Komplexe Suchen

# Aggregierte Suche
GET /products/_search
{
  "size": 0,
  "query": {
    "range": {
      "price": {
        "gte": 100
      }
    }
  },
  "aggs": {
    "categories": {
      "terms": {
        "field": "category",
        "size": 10
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

56.5.3 Data Reindexing

# Reindex mit Transformation
POST _reindex
{
  "source": {
    "index": "old_products"
  },
  "dest": {
    "index": "new_products"
  },
  "script": {
    "source": """
      ctx._source.price *= 1.1;
      ctx._source.last_updated = System.currentTimeMillis();
    """
  }
}

56.6 Keyboard Shortcuts

Die wichtigsten Tastenkombinationen:

56.7 Best Practices

56.7.1 Query-Optimierung

# Effiziente Query-Struktur
GET /products/_search
{
  "_source": ["name", "price"],
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "category": "electronics"
          }
        },
        {
          "range": {
            "price": {
              "gte": 100,
              "lte": 500
            }
          }
        }
      ]
    }
  }
}

56.7.2 Performance-Monitoring

# Cluster-Status überprüfen
GET /_cluster/health

# Index-Statistiken
GET /products/_stats

# Shard-Allokation
GET /_cat/shards?v

56.7.3 Fehlerbehandlung

# Fehlersuche bei fehlgeschlagenen Anfragen
GET /_cat/indices?v&health=red

# Recovery-Status
GET /_recovery?active_only=true

# Pending Tasks
GET /_cluster/pending_tasks