Stellen Sie sich Mapping als den Bauplan eines Hauses vor. Wie ein Architekt genau festlegt, wo Räume, Türen und Fenster sein sollen, definiert das Mapping die Struktur Ihrer Daten in OpenSearch. Es legt fest, wie Informationen gespeichert, indiziert und durchsucht werden können.
Ein durchdachtes Mapping ist entscheidend für:
OpenSearch bietet zwei Wege, ein Mapping zu erstellen:
// Explizites Mapping
PUT /customer-index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"age": {
"type": "integer"
}
}
}
}
// Dynamisches Mapping lässt OpenSearch die Typen erkennen
POST /dynamic-index/_doc
{
"name": "John Doe", // Wird als "text" erkannt
"age": 30, // Wird als "integer" erkannt
"joined": "2025-01-14" // Wird als "date" erkannt
}OpenSearch bietet eine Vielzahl von Datentypen für unterschiedliche Anwendungsfälle:
{
"properties": {
"title": {
"type": "text", // Volltext-Suche
"fields": {
"raw": {
"type": "keyword" // Exakte Suche & Aggregationen
}
}
}
}
}{
"properties": {
"price": {
"type": "float",
"coerce": true // Automatische Typkonvertierung
},
"quantity": {
"type": "integer"
}
}
}{
"properties": {
"created_at": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}{
"properties": {
"email": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
},
"analyzed": {
"type": "text",
"analyzer": "email_analyzer"
}
}
}
}
}{
"properties": {
"description": {
"type": "text",
"analyzer": "german",
"search_analyzer": "german_search",
"index_options": "positions",
"store": true,
"copy_to": "all_fields"
}
}
}PUT /products
{
"mappings": {
"properties": {
"sku": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"raw": { "type": "keyword" }
}
},
"description": {
"type": "text",
"analyzer": "custom_product_analyzer"
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
},
"inventory": {
"type": "nested",
"properties": {
"warehouse": { "type": "keyword" },
"quantity": { "type": "integer" },
"location": { "type": "geo_point" }
}
}
}
}
}PUT /logs
{
"mappings": {
"properties": {
"timestamp": {
"type": "date"
},
"level": {
"type": "keyword"
},
"message": {
"type": "text",
"index_options": "positions"
},
"stack_trace": {
"type": "text",
"index": false
},
"metrics": {
"type": "object",
"enabled": false
}
}
}
}{
"properties": {
"user_id": {
"type": "long",
"doc_values": false // Deaktivieren für nicht-aggregierte Felder
},
"email": {
"type": "keyword",
"ignore_above": 256 // Limitiert Feldlänge
}
}
}{
"properties": {
"title": {
"type": "text",
"index_prefixes": { // Optimiert Prefix-Suchen
"min_chars": 1,
"max_chars": 10
}
}
}
}PUT /my-index/_mapping
{
"properties": {
"new_field": {
"type": "keyword"
}
}
}POST /_reindex
{
"source": {
"index": "old-index"
},
"dest": {
"index": "new-index"
},
"script": {
"source": "ctx._source.new_field = ctx._source.remove('old_field')"
}
}GET /my-index/_mapping
GET /my-index/_mapping/field/specific.fieldPOST /my-index/_analyze
{
"field": "my_field",
"text": "Sample text"
}
Text-Felder sind ideal für Volltextsuche, verbrauchen aber mehr Speicher:
{
"properties": {
"email": {
"type": "text", // Ermöglicht Volltextsuche
"fields": {
"raw": {
"type": "keyword" // Ermöglicht effiziente Aggregationen
}
}
}
}
}
Nested-Typen bieten mehr Genauigkeit, sind aber ressourcenintensiver:
{
"properties": {
"comments": {
"type": "nested", // Präzise Suche in Arrays von Objekten
"properties": {
"author": { "type": "keyword" },
"text": { "type": "text" }
}
}
}
}// Neuen Index mit aktualisiertem Mapping erstellen
PUT /users-v2
{
"mappings": {
"properties": {
"name": { "type": "text" },
"email": { "type": "keyword" }
}
}
}
// Daten reindexieren
POST /_reindex
{
"source": { "index": "users-v1" },
"dest": { "index": "users-v2" }
}
// Alias aktualisieren
POST /_aliases
{
"actions": [
{ "remove": { "index": "users-v1", "alias": "users" }},
{ "add": { "index": "users-v2", "alias": "users" }}
]
}
Ein erfolgreiches Mapping sollte:
Die sorgfältige Planung des Mappings zu Beginn eines Projekts zahlt sich durch bessere Performance, geringere Wartungskosten und höhere Flexibilität aus