Mappings Flashcards
Create index with Mappings
PUT /my_index
{
“mappings”: {
“properties”: {
“title”: {
“type”: “text”,
“analyzer”: “standard”
},
“status”: {
“type”: “keyword”
},
“views”: {
“type”: “integer”
},
“published_date”: {
“type”: “date”,
“format”: “yyyy-MM-dd”
},
“is_active”: {
“type”: “boolean”
},
“author”: {
“properties”: {
“name”: {
“type”: “text”
},
“age”: {
“type”: “integer”
}
}
}
}
}
}
How the mappings is analyzed
title is analyzed for full-text search.
status is a keyword, which is used for exact matching.
views is an integer.
published_date is a date in yyyy-MM-dd format.
is_active is a boolean.
author is an object, allowing you to store name as text and age as an integer.
Preparing the data
Ensure your data format aligns with the mappings defined in the index.
{
“title”: “Introduction to Elasticsearch”,
“status”: “published”,
“views”: 150,
“published_date”: “2024-01-01”,
“is_active”: true,
“author”: {
“name”: “Jane Doe”,
“age”: 34
}
}
Ingest Data using Single Insert
POST /my_index/_doc/1
{
“title”: “Introduction to Elasticsearch”,
“status”: “published”,
“views”: 150,
“published_date”: “2024-01-01”,
“is_active”: true,
“author”: {
“name”: “Jane Doe”,
“age”: 34
}
}
Insert data using bulk insert
POST /my_index/_bulk
{ “index”: { “_id”: “1” } }
{ “title”: “Introduction to Elasticsearch”, “status”: “published”, “views”: 150, “published_date”: “2024-01-01”, “is_active”: true, “author”: { “name”: “Jane Doe”, “age”: 34 } }
{ “index”: { “_id”: “2” } }
{ “title”: “Advanced Elasticsearch Techniques”, “status”: “draft”, “views”: 75, “published_date”: “2024-01-02”, “is_active”: false, “author”: { “name”: “John Smith”, “age”: 40 } }
Update Mappings
PUT /my_index/_mapping
{
“properties”: {
“new_field”: {
“type”: “text”
}
}
}
Common Mappings
Field Types - string,byte,short integer,long,float,double,boolean,date
Field Index - Field to be analyzed/not-analyzed for full text search?
Field Analyzer - Define your tokenizer and token filter
standard/whitespace/simple/english
Analyzers
Character Filters
Remove HTML encoding, convert & to and
Tokenizer
Split strings on whitespace/punctuation/non-letters
Token Filter
Lowercasing, stemming, synonyms, stopwords