55 Index Patterns in OpenSearch Dashboards

Index Patterns sind das Fundament Ihrer Arbeit mit OpenSearch Dashboards. Sie definieren, wie OpenSearch Dashboards Ihre Daten interpretiert und darstellt. Denken Sie an Index Patterns als eine Art “Datenschablone”, die festlegt, welche Indizes einbezogen werden und wie ihre Felder interpretiert werden sollen.

55.1 Grundlagen der Index Patterns

55.1.1 Anatomie eines Index Patterns

Ein Index Pattern besteht aus mehreren Komponenten:

  1. Pattern Definition: Definiert, welche Indizes eingeschlossen werden
  2. Field Mappings: Beschreibt die Struktur und Typen der Felder
  3. Time Field: Optional, für zeitbasierte Daten
  4. Custom Settings: Zusätzliche Einstellungen für spezielle Anforderungen

55.1.2 Erstellung eines Basic Index Patterns

PUT _component_template/logs_template
{
  "template": {
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "message": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "level": {
          "type": "keyword"
        },
        "source": {
          "type": "keyword"
        }
      }
    }
  }
}

55.2 Pattern-Syntax verstehen

55.2.1 Wildcards und Expressions

Index Patterns unterstützen verschiedene Matching-Strategien:

logs-*           // Alle Indizes, die mit "logs-" beginnen
logs-{app,web}-* // Logs von app und web Services
logs-2024-*      // Alle Logs aus 2024
*-production     // Alle Production-Indizes

55.2.2 Zeit-basierte Patterns

Für zeitbasierte Daten:

PUT _index_template/time_based_logs
{
  "index_patterns": ["logs-*-*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}

55.3 Field Management

55.3.1 Field Types definieren

PUT _component_template/field_types
{
  "template": {
    "mappings": {
      "properties": {
        "user_id": {
          "type": "keyword"
        },
        "email": {
          "type": "keyword",
          "fields": {
            "analyzed": {
              "type": "text",
              "analyzer": "standard"
            }
          }
        },
        "age": {
          "type": "integer"
        },
        "registration_date": {
          "type": "date"
        }
      }
    }
  }
}

55.3.2 Runtime Fields

Runtime Fields ermöglichen dynamische Feldberechnungen:

PUT my_index/_mapping
{
  "runtime": {
    "full_name": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)"
      }
    },
    "age_category": {
      "type": "keyword",
      "script": {
        "source": """
          int age = doc['age'].value;
          if (age < 18) emit('minor');
          else if (age < 65) emit('adult');
          else emit('senior');
        """
      }
    }
  }
}

55.4 Advanced Pattern Configuration

55.4.1 Alias und Multi-Pattern Setup

PUT _aliases
{
  "actions": [
    {
      "add": {
        "index": "logs-*",
        "alias": "all_logs",
        "filter": {
          "term": {
            "environment": "production"
          }
        }
      }
    }
  ]
}

55.4.2 Field Formatters

PUT _component_template/field_formatters
{
  "template": {
    "mappings": {
      "properties": {
        "price": {
          "type": "float",
          "meta": {
            "opensearch_dashboards": {
              "format": "currency",
              "currency": "EUR"
            }
          }
        },
        "percentage": {
          "type": "float",
          "meta": {
            "opensearch_dashboards": {
              "format": "percent"
            }
          }
        }
      }
    }
  }
}

55.5 Pattern Maintenance

55.5.1 Pattern Health Checking

// Pattern Health Check Script
const checkPatternHealth = async (pattern) => {
  try {
    // Prüfe Index-Existenz
    const indices = await client.cat.indices({
      index: pattern,
      format: 'json'
    });
    
    // Prüfe Mapping-Konsistenz
    const mappings = await client.indices.getMapping({
      index: pattern
    });
    
    // Prüfe Feld-Nutzung
    const fieldStats = await client.fieldCaps({
      index: pattern,
      fields: '*'
    });
    
    return {
      indicesCount: indices.length,
      mappingConsistency: checkMappingConsistency(mappings),
      unusedFields: findUnusedFields(fieldStats)
    };
  } catch (error) {
    console.error(`Pattern health check failed: ${error}`);
    throw error;
  }
};

55.5.2 Pattern Migration

// Pattern Migration Helper
const migratePattern = async (oldPattern, newPattern) => {
  try {
    // Backup alte Pattern-Konfiguration
    const oldConfig = await getPatternConfig(oldPattern);
    
    // Erstelle neues Pattern
    await createPattern(newPattern, {
      ...oldConfig,
      // Anpassungen hier
    });
    
    // Migriere Visualisierungen
    await migrateVisualizations(oldPattern, newPattern);
    
    // Migriere Dashboards
    await migrateDashboards(oldPattern, newPattern);
    
    return {
      status: 'success',
      migratedElements: {
        visualizations: visualizationCount,
        dashboards: dashboardCount
      }
    };
  } catch (error) {
    console.error(`Pattern migration failed: ${error}`);
    throw error;
  }
};

55.6 Best Practices

55.6.1 Pattern-Naming Conventions

[service]-[environment]-[datatype]-YYYY.MM.DD
logs-production-auth-2024.01.14
metrics-staging-cpu-2024.01.14

55.6.2 Field Optimization

55.6.3 Performance Considerations

55.7 Übungen

  1. Basic Pattern Creation

  2. Advanced Pattern Management# Index Patterns in OpenSearch Dashboards

Index Patterns sind das Fundament für die Arbeit mit OpenSearch Dashboards. Sie definieren, wie Dashboards Ihre Daten interpretiert und visualisiert. Denken Sie an Index Patterns als eine Art Datenschema, das OpenSearch Dashboards mitteilt, wie es Ihre Daten verstehen und darstellen soll.

55.8 Grundlagen der Index Patterns

55.8.1 Was sind Index Patterns?

Ein Index Pattern ist eine Vorlage, die einen oder mehrere OpenSearch-Indizes abbildet. Es definiert:

55.8.2 Anatomie eines Index Patterns

Ein typisches Index Pattern besteht aus:

{
  "title": "logs-*",
  "timeFieldName": "@timestamp",
  "fields": [
    {
      "name": "@timestamp",
      "type": "date",
      "esTypes": ["date"],
      "searchable": true,
      "aggregatable": true,
      "readFromDocValues": true
    },
    {
      "name": "message",
      "type": "text",
      "esTypes": ["text"],
      "searchable": true,
      "aggregatable": false
    },
    {
      "name": "level",
      "type": "keyword",
      "esTypes": ["keyword"],
      "searchable": true,
      "aggregatable": true,
      "readFromDocValues": true
    }
  ]
}

55.9 Index Pattern erstellen

55.9.1 Grundlegende Pattern-Definition

  1. Einfaches Pattern für Logs
PUT _index_template/logs_template
{
  "index_patterns": ["logs-*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "message": {
          "type": "text"
        },
        "level": {
          "type": "keyword"
        }
      }
    }
  }
}
  1. Pattern mit mehreren Indizes
{
  "title": "{logs,metrics}-*",
  "timeFieldName": "@timestamp",
  "allowNoIndex": false
}

55.9.2 Erweiterte Pattern-Konfiguration

PUT _index_template/advanced_logs
{
  "index_patterns": ["logs-*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    },
    "mappings": {
      "dynamic_templates": [
        {
          "strings_as_keywords": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword",
              "ignore_above": 256,
              "fields": {
                "text": {
                  "type": "text"
                }
              }
            }
          }
        }
      ],
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "message": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "level": {
          "type": "keyword"
        },
        "tags": {
          "type": "keyword"
        },
        "geo_location": {
          "type": "geo_point"
        }
      }
    }
  }
}

55.10 Feldtypen und Mapping

55.10.1 Grundlegende Feldtypen

  1. Text und Keyword Fields
{
  "properties": {
    "full_text": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}
  1. Numerische Felder
{
  "properties": {
    "integer_value": {
      "type": "integer"
    },
    "float_value": {
      "type": "float"
    },
    "scaled_float": {
      "type": "scaled_float",
      "scaling_factor": 100
    }
  }
}

55.10.2 Runtime Fields

Runtime Fields erlauben dynamische Feldberechnungen:

{
  "runtime_mappings": {
    "response_time_seconds": {
      "type": "double",
      "script": {
        "source": "emit(doc['response_time_ms'].value / 1000.0)"
      }
    },
    "day_of_week": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"
      }
    }
  }
}

55.11 Pattern Management

55.11.1 Pattern Wartung

  1. Pattern überprüfen
GET _index_template/logs_template
  1. Pattern aktualisieren
PUT _index_template/logs_template/_settings
{
  "index": {
    "refresh_interval": "30s",
    "number_of_replicas": 2
  }
}

55.11.2 Pattern Migration

Beim Migrieren von Patterns beachten Sie:

POST _reindex
{
  "source": {
    "index": "old-logs-*"
  },
  "dest": {
    "index": "new-logs"
  },
  "script": {
    "source": """
      ctx._source.new_field = ctx._source.remove("old_field");
      ctx._source.timestamp = ctx._source.remove("@timestamp");
    """
  }
}

55.12 Best Practices

55.12.1 Naming Conventions

{
  "index_patterns": [
    "logs-{service}-{environment}-*",
    "metrics-{service}-{environment}-*"
  ]
}

55.12.2 Feldoptimierung

{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keywords": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    ]
  }
}

55.12.3 Performance-Optimierung

{
  "settings": {
    "index.mapping.total_fields.limit": 1000,
    "index.mapping.depth.limit": 20,
    "index.mapping.nested_fields.limit": 50
  }
}

55.13 Praktische Übungen

55.13.1 Log Analysis Pattern

Erstellen Sie ein Pattern für Logs mit:

55.13.2 Metriken Pattern

Implementieren Sie ein Pattern für Metriken mit:

55.13.3 Business Data Pattern

Entwickeln Sie ein Pattern für Geschäftsdaten mit:

55.14 Troubleshooting

55.14.1 Häufige Probleme und Lösungen

  1. Felder nicht suchbar
PUT _index_template/searchable_template
{
  "index_patterns": ["*"],
  "template": {
    "mappings": {
      "properties": {
        "field_name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}
  1. Felder nicht aggregierbar
{
  "properties": {
    "field_name": {
      "type": "keyword",
      "doc_values": true
    }
  }
}
  1. Mapping-Konflikte
POST _reindex
{
  "source": {
    "index": "conflicting-index"
  },
  "dest": {
    "index": "new-index"
  },
  "script": {
    "source": "ctx._source.conflicting_field = ctx._source.conflicting_field.toString()"
  }
}

Diese Grundlagen helfen Ihnen, Index Patterns effektiv zu erstellen und zu verwalten. Die nächsten Kapitel werden spezifische Aspekte vertiefen und fortgeschrittene Techniken vorstellen.

- Implementieren Sie Runtime Fields
- Erstellen Sie Field Formatters
- Konfigurieren Sie Pattern Aliases
  1. Pattern Migration

Diese Übungen helfen Ihnen, Index Patterns in der Praxis zu verstehen und effektiv einzusetzen.