{"id":2251,"date":"2016-01-16T18:59:01","date_gmt":"2016-01-16T17:59:01","guid":{"rendered":"http:\/\/codingexplained.com\/?p=2251"},"modified":"2016-01-16T18:59:01","modified_gmt":"2016-01-16T17:59:01","slug":"introduction-to-mapping","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping","title":{"rendered":"Introduction to Mapping"},"content":{"rendered":"<p>In this part of the series, we will be talking about mapping in Elasticsearch. I will introduce mapping as well as cover dynamic and explicit mapping.<\/p>\n<h2>What is Mapping in Elasticsearch?<\/h2>\n<p>Like I briefly mentioned in the <a href=\"\/coding\/elasticsearch\/terminology\" target=\"_blank\">article\u00a0about Elasticsearch terminology<\/a>, mapping describes how documents and their fields are indexed and stored. This\u00a0typically involves defining the data types of the fields that make up the documents, which is quite similar to what one does in relational databases. Mapping can also be used to define the format for date fields, for instance, and to define whether or not field values should be indexed into the catch-all field named <span class=\"code\">_all<\/span>. There is more to mapping than this, but the most important part is defining the data types and formats of fields.<\/p>\n<h2>Field Data Types<\/h2>\n<p>A mapping type contains fields such as the title or content for an <span class=\"code\">article<\/span> type. Each field has a data type, which can be strings, numbers, booleans, etc. These data types can be defined in the mapping, similar to how data types are defined for columns in relational databases. We will take a more detailed look at this in the next article in this series, where I will be walking through the most important data types. For now, you should just know that this information is associated with mapping types.<\/p>\n<h2>Meta Fields<\/h2>\n<p>Each mapping type contains a number of meta fields for various purposes, some of which can be customized. Each document has these meta fields associated with them apart from the JSON object that was added to the index. A few examples of such meta fields are <span class=\"code\">_id<\/span>, <span class=\"code\">_type<\/span>, <span class=\"code\">_uid<\/span> and <span class=\"code\">_index<\/span>.\u00a0We will get back to meta fields in one of the next articles, so I am not going to go into much detail now.<\/p>\n<h2>Dynamic Mapping<\/h2>\n<p>Mapping does not necessarily have to be defined explicitly before adding documents of a given type to an index. In fact, <em>dynamic mapping<\/em> refers to the\u00a0automatic detection and addition of new types and fields. This means that you can add a document without first having to define the mapping type and\u00a0fields. It is even possible to add a document without creating an index first! In this case, Elasticsearch will take care of creating the index, mapping\u00a0type and fields. This is done automatically, and Elasticsearch will infer the data types based on the document&#8217;s data.<\/p>\n<h2>Explicit Mapping<\/h2>\n<p>Explicit mapping refers to adding mapping information explicitly instead of letting Elasticsearch infer this based on added documents. This can be done\u00a0when creating an index, but can also be done by issuing a <span class=\"code\">PUT<\/span> request to an existing index. Explicit mapping is useful if you have some requirements\u00a0for a mapping type&#8217;s data, such as date formats. It can also be useful if Elasticsearch cannot infer the correct mapping information based on the added\u00a0documents. While dynamic mapping is very convenient, especially when getting started with Elasticsearch, it can be a good idea to define explicit\u00a0mappings, as this describes the data and the requirements for this data.<\/p>\n<h2>Mapping Gotchas<\/h2>\n<p>Now there are a few gotchas in regards to mapping that you should be aware of. First of all, existing type and field mappings cannot be updated. This\u00a0means that if you have data in an index, you have to create a new index with the new mappings and add the data into the new index. This is because any\u00a0existing data would effectively be invalidated if existing mappings were updated.<\/p>\n<p>Secondly, it is important to know that fields are shared across mapping\u00a0types. What this means, is that if a <span class=\"code\">title<\/span> field exists in both an <span class=\"code\">employee<\/span> and <span class=\"code\">article<\/span> mapping type, then the fields must have exactly the same\u00a0mapping in each type. This is quite inconvenient if you need different mappings, but the easy solution is to prefix field names with the name of the\u00a0mapping type. In this example, the <span class=\"code\">title<\/span> fields would then be named <span class=\"code\">employee_title<\/span> and <span class=\"code\">article_title<\/span>. In my humble opinion, this is really a shame,\u00a0because it does not look pretty and is not convenient for developers, but it is nevertheless how Elasticsearch works for the time being.<\/p>\n<p>In the next two articles, we will go through\u00a0field data types and meta fields in more details.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"series\">Part 8 of 35 in the <a href=\"https:\/\/codingexplained.com\/tutorial\/complete-guide-to-elasticsearch\" class=\"series-163\" title=\"Complete Guide to Elasticsearch\">Complete Guide to Elasticsearch<\/a> series<\/div><p>In this part of the series, we will be talking about mapping in Elasticsearch. I will introduce mapping as well as cover dynamic and explicit mapping. What is Mapping in Elasticsearch? Like I briefly mentioned in the article\u00a0about Elasticsearch terminology, mapping describes how documents and their fields are indexed and stored. This\u00a0typically involves defining the&hellip; <a href=\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping\" class=\"more-link\">read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"Introduction to Mapping","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[154],"tags":[155],"series":[163],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Introduction to Mapping<\/title>\n<meta name=\"description\" content=\"Get introduced to mapping! This article is an introduction to mapping in Elasticsearch, including explicit and dynamic mapping.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to Mapping\" \/>\n<meta property=\"og:description\" content=\"Get introduced to mapping! This article is an introduction to mapping in Elasticsearch, including explicit and dynamic mapping.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2016-01-16T17:59:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping\",\"url\":\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping\",\"name\":\"Introduction to Mapping\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2016-01-16T17:59:01+00:00\",\"dateModified\":\"2016-01-16T17:59:01+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Get introduced to mapping! This article is an introduction to mapping in Elasticsearch, including explicit and dynamic mapping.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction to Mapping\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to Mapping","description":"Get introduced to mapping! This article is an introduction to mapping in Elasticsearch, including explicit and dynamic mapping.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping","og_locale":"en_US","og_type":"article","og_title":"Introduction to Mapping","og_description":"Get introduced to mapping! This article is an introduction to mapping in Elasticsearch, including explicit and dynamic mapping.","og_url":"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2016-01-16T17:59:01+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping","url":"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping","name":"Introduction to Mapping","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2016-01-16T17:59:01+00:00","dateModified":"2016-01-16T17:59:01+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Get introduced to mapping! This article is an introduction to mapping in Elasticsearch, including explicit and dynamic mapping.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/elasticsearch\/introduction-to-mapping#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Introduction to Mapping"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-Aj","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2251"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=2251"}],"version-history":[{"count":7,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2251\/revisions"}],"predecessor-version":[{"id":2258,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2251\/revisions\/2258"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=2251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=2251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=2251"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=2251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}