{"id":3494,"date":"2015-01-15T16:32:40","date_gmt":"2015-01-15T16:32:40","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2015\/01\/15\/creating-documents-in-elasticsearch\/"},"modified":"2021-08-16T22:55:55","modified_gmt":"2021-08-17T02:55:55","slug":"creating-documents-in-elasticsearch","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/","title":{"rendered":"Creating documents in Elasticsearch"},"content":{"rendered":"<div class=\"in_this_tutorial\">\n<h2>In this tutorial:<\/h2>\n<p><a class=\"btn btn-primary\" href=\"#with\" type=\"button\">Creating a document in Elasticsearch with an existing ID<\/a> <a class=\"btn btn-primary\" href=\"#without\" type=\"button\">Creating a document in Elasticsearch without an existing ID <\/a><\/p>\n<\/div>\n<p>Individual entries within Elasticsearch are referred to as documents.  These documents contain various entries that relate to a single record and are stored in the appropriate index.  For example, an index named <em>customers<\/em> may contain a document with records such as <em>first_name<\/em>, <em>last_name<\/em>, and <em>email_address<\/em>.  Think of a document as an individual object and each entry describes a single item within that object.<\/p>\n<div class=\"alert alert-warning\">\n<p><strong>Note:<\/strong> This article assumes that you have already <a href=\"\/support\/edu\/software\/how-to-install-elasticsearch\/\">installed Elasticsearch<\/a> as well as have <a href=\"\/support\/edu\/software\/working-with-indexes-in-elasticsearch\/\">created an index within Elasticsearch<\/a>.<\/p>\n<\/div>\n<p>As you may already know from our article on <a href=\"\/support\/edu\/software\/working-with-indexes-in-elasticsearch\/\">creating an index within Elasticsearch<\/a>, data is both inserted as well as retrieved using simple HTTP requests.  The same will be done in this article.<\/p>\n<p><a name=\"with\"><\/a><\/p>\n<h2>Creating a document in Elasticsearch with a pre-defined ID<\/h2>\n<p>If you already know the ID that you want associated with your new document, you can insert a new document with that ID.  The following command line example will insert a new document into Elasticsearch with our predefined ID:<\/p>\n<p class=\"code_block\">user@server [~]# curl -XPUT &#8216;localhost:9200\/mynewindex\/external\/mynewid?pretty&#8217; -d &#8216;<br \/>\n{<br \/>\n&#8220;first_name&#8221;: &#8220;John&#8221;,<br \/>\n&#8220;last_name&#8221;: &#8220;Doe&#8221;,<br \/>\n&#8220;email_address&#8221;: &#8220;user@example.com&#8221;<br \/>\n}&#8217;<\/p>\n<p>As you can see in the above, we are making a simple PUT request using the <em>curl<\/em> command to our Elasticsearch server that is located at <em>localhost<\/em> on port 9200.<\/p>\n<p>Then, we are defining an index that we want this document to be located in which is <em>mynewindex<\/em>.  After this, we are stating the data type that we want to place the data within.  In this case, our type is <em>external<\/em>.<\/p>\n<p>We are then defining the ID within the last part of the URL which in this case, is <em>mynewid<\/em>.  The last little bit (<strong>?pretty<\/strong>) is simply stating that we want a well-formatted response.<\/p>\n<p>Lastly, is simply our data that we are placing into the document, formatted in JSON.  As you can see, we are inserting an individual&#8217;s first name, last name, and email address.<\/p>\n<p>If all goes well, you will get a response back that looks like this:<\/p>\n<p class=\"code_block\">user@server [~]# curl -XPUT &#8216;localhost:9200\/mynewindex\/external\/mynewid?pretty&#8217; -d &#8216;<br \/>\n{<br \/>\n&#8220;first_name&#8221;: &#8220;John&#8221;,<br \/>\n&#8220;last_name&#8221;: &#8220;Doe&#8221;,<br \/>\n&#8220;email_address&#8221;: &#8220;user@example.com&#8221;<br \/>\n}&#8217;<br \/>\n{<br \/>\n&#8220;_index&#8221; : &#8220;mynewindex&#8221;,<br \/>\n&#8220;_type&#8221; : &#8220;external&#8221;,<br \/>\n&#8220;_id&#8221; : &#8220;mynewid&#8221;,<br \/>\n&#8220;_version&#8221; : 1,<br \/>\n&#8220;created&#8221; : true<br \/>\n}<\/p>\n<p>As you can see in the above, our new document was successfully created in the index <em>mynewindex<\/em> with the type <em>external<\/em> and the ID <em>mynewid<\/em>.<\/p>\n<p>What happens if you don&#8217;t have an ID that you specifically want to define?  Follow our next section to learn how to insert a new document without defining an ID.<\/p>\n<p><a name=\"without\"><\/a><\/p>\n<h2>Inserting a document in Elasticsearch without defining an ID<\/h2>\n<p>Inserting a document without specifically defining an ID for it is quite easy.  If an ID is not passed, it will simply create one for you.  We can do so by executing the following command:<\/p>\n<p class=\"code_block\">user@server [~]# curl -XPOST &#8216;localhost:9200\/mynewindex\/external?pretty&#8217; -d &#8216;<br \/>\n{<br \/>\n&#8220;first_name&#8221;: &#8220;John&#8221;,<br \/>\n&#8220;last_name&#8221;: &#8220;Doe&#8221;,<br \/>\n&#8220;email_address&#8221;: &#8220;user@example.com&#8221;<br \/>\n}&#8217;<\/p>\n<p>The above example is very similar to creating a document with a pre-defined ID but with a few minor changes.<\/p>\n<p>First, we are sending a POST request instead of a PUT request.  Next, we are simply removing the ID that we previously had defined.<\/p>\n<p>When executing this, you should have the following response:<\/p>\n<p class=\"code_block\">user@server [~]# curl -XPOST &#8216;localhost:9200\/mynewindex\/external?pretty&#8217; -d &#8216;<br \/>\n{<br \/>\n&#8220;first_name&#8221;: &#8220;John&#8221;,<br \/>\n&#8220;last_name&#8221;: &#8220;Doe&#8221;,<br \/>\n&#8220;email_address&#8221;: &#8220;user@example.com&#8221;<br \/>\n}&#8217;<br \/>\n{<br \/>\n&#8220;_index&#8221; : &#8220;mynewindex&#8221;,<br \/>\n&#8220;_type&#8221; : &#8220;external&#8221;,<br \/>\n&#8220;_id&#8221; : &#8220;AUruRKQIWyBxgi2aaH34&#8221;,<br \/>\n&#8220;_version&#8221; : 1,<br \/>\n&#8220;created&#8221; : true<br \/>\n}<\/p>\n<p>As you can see in the above example, the ID has been automatically created with a random string.  This is especially useful if the data you are inserting does not have a way to uniquely identify it such as a list of customers.<\/p>\n<p>You should now be able to insert new documents within Elasticsearch.  If you have any questions or anything to add, leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial: Creating a document in Elasticsearch with an existing ID Creating a document in Elasticsearch without an existing ID Individual entries within Elasticsearch are referred to as documents. These documents contain various entries that relate to a single record and are stored in the appropriate index. For example, an index named customers may<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\"> Read More ><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4360],"tags":[],"class_list":["post-3494","post","type-post","status-publish","format-standard","hentry","category-software"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating documents in Elasticsearch | InMotion Hosting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating documents in Elasticsearch | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"In this tutorial: Creating a document in Elasticsearch with an existing ID Creating a document in Elasticsearch without an existing ID Individual entries within Elasticsearch are referred to as documents. These documents contain various entries that relate to a single record and are stored in the appropriate index. For example, an index named customers may Read More &gt;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-15T16:32:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-17T02:55:55+00:00\" \/>\n<meta name=\"author\" content=\"Jeff Matson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeff Matson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\"},\"author\":{\"name\":\"Jeff Matson\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b\"},\"headline\":\"Creating documents in Elasticsearch\",\"datePublished\":\"2015-01-15T16:32:40+00:00\",\"dateModified\":\"2021-08-17T02:55:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\"},\"wordCount\":662,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"Software\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\",\"name\":\"Creating documents in Elasticsearch | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2015-01-15T16:32:40+00:00\",\"dateModified\":\"2021-08-17T02:55:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating documents in Elasticsearch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b\",\"name\":\"Jeff Matson\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/jeffma\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating documents in Elasticsearch | InMotion Hosting","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:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/","og_locale":"en_US","og_type":"article","og_title":"Creating documents in Elasticsearch | InMotion Hosting","og_description":"In this tutorial: Creating a document in Elasticsearch with an existing ID Creating a document in Elasticsearch without an existing ID Individual entries within Elasticsearch are referred to as documents. These documents contain various entries that relate to a single record and are stored in the appropriate index. For example, an index named customers may Read More >","og_url":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2015-01-15T16:32:40+00:00","article_modified_time":"2021-08-17T02:55:55+00:00","author":"Jeff Matson","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Jeff Matson","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/"},"author":{"name":"Jeff Matson","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b"},"headline":"Creating documents in Elasticsearch","datePublished":"2015-01-15T16:32:40+00:00","dateModified":"2021-08-17T02:55:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/"},"wordCount":662,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["Software"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/","url":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/","name":"Creating documents in Elasticsearch | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2015-01-15T16:32:40+00:00","dateModified":"2021-08-17T02:55:55+00:00","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/software\/creating-documents-in-elasticsearch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"Creating documents in Elasticsearch"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/83776252b196c020e4352a3796e5642b","name":"Jeff Matson","url":"https:\/\/www.inmotionhosting.com\/support\/author\/jeffma\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=3494"}],"version-history":[{"count":2,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3494\/revisions"}],"predecessor-version":[{"id":84139,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/3494\/revisions\/84139"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=3494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=3494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=3494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}