{"id":1654,"date":"2018-05-03T12:45:12","date_gmt":"2018-05-03T12:45:12","guid":{"rendered":"https:\/\/gosqeng.test\/?p=1654"},"modified":"2019-11-28T11:03:39","modified_gmt":"2019-11-28T11:03:39","slug":"authoring-elasticsearch-queries-javascript","status":"publish","type":"post","link":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript","title":{"rendered":"Authoring Elasticsearch queries in Javascript"},"content":{"rendered":"<p>To search something in Elasticsearch, the request looks like this:<\/p>\n<p><code>POST https:\/\/es-cluster:9200\/index-name\/_search<\/code><\/p>\n<p>The query definition is sent as JSON in the body of the request. Here&#8217;s an example search body:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/TheDeveloper\/071a03d3ce0bccddbfa4fd9f270ab241.js\"><\/script><\/p>\n<p>We have found it&#8217;s quite useful to put queries together using the improved object literal syntax of more recent versions of JavaScript:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/TheDeveloper\/2c9f822ae279b378263e5c0df3543daf.js\"><\/script><\/p>\n<p>This script shows how we might build the search body for the previous request.<\/p>\n<p>In the script, we&#8217;re building the query line-by-line and making use of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Object_initializer\" rel=\"noopener noreferrer\" target=\"_blank\">ES2015&#8217;s shorthand property<\/a> names to link it all together.<\/p>\n<p>I usually find I work backwards from the es.search() statement. First are the params &#8211; defining the index and options for the request. Then onto the request body, which itself unravels into its constituent parts.<\/p>\n<p>This technique can be used in Node JS or modern browsers &#8211; <a href=\"https:\/\/kangax.github.io\/compat-table\/es6\/#test-object_literal_extensions_shorthand_properties\" rel=\"noopener noreferrer\" target=\"_blank\">wherever ES2015 is supported<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To search something in Elasticsearch, the request looks like this: POST https:\/\/es-cluster:9200\/index-name\/_search The query definition is sent as JSON in&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1452],"tags":[],"class_list":["post-1654","post","type-post","status-publish","format-standard","hentry","category-engineering"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v18.6 (Yoast SEO v19.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Authoring Elasticsearch queries in Javascript - GoSquared Blog<\/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.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Authoring Elasticsearch queries in Javascript\" \/>\n<meta property=\"og:description\" content=\"To search something in Elasticsearch, the request looks like this: POST https:\/\/es-cluster:9200\/index-name\/_search The query definition is sent as JSON in...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript\" \/>\n<meta property=\"og:site_name\" content=\"GoSquared Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GoSquared\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-03T12:45:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-28T11:03:39+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TheDeveloper\" \/>\n<meta name=\"twitter:site\" content=\"@GoSquared\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Geoff Wagstaff\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\",\"name\":\"GoSquared\",\"url\":\"https:\/\/www.gosquared.com\/blog\/\",\"sameAs\":[\"https:\/\/instagram.com\/gosquaredteam\",\"https:\/\/www.linkedin.com\/company\/go-squared-ltd.\",\"https:\/\/www.facebook.com\/GoSquared\",\"https:\/\/twitter.com\/GoSquared\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png\",\"contentUrl\":\"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png\",\"width\":1270,\"height\":250,\"caption\":\"GoSquared\"},\"image\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#website\",\"url\":\"https:\/\/www.gosquared.com\/blog\/\",\"name\":\"GoSquared Blog\",\"description\":\"Turn visitors into customers.\",\"publisher\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.gosquared.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#webpage\",\"url\":\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript\",\"name\":\"Authoring Elasticsearch queries in Javascript - GoSquared Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#website\"},\"datePublished\":\"2018-05-03T12:45:12+00:00\",\"dateModified\":\"2019-11-28T11:03:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.gosquared.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Authoring Elasticsearch queries in Javascript\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#webpage\"},\"author\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448\"},\"headline\":\"Authoring Elasticsearch queries in Javascript\",\"datePublished\":\"2018-05-03T12:45:12+00:00\",\"dateModified\":\"2019-11-28T11:03:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#webpage\"},\"wordCount\":143,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\"},\"articleSection\":[\"Engineering\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448\",\"name\":\"Geoff Wagstaff\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g\",\"caption\":\"Geoff Wagstaff\"},\"sameAs\":[\"https:\/\/twitter.com\/TheDeveloper\"],\"url\":\"https:\/\/www.gosquared.com\/blog\/author\/echo\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Authoring Elasticsearch queries in Javascript - GoSquared Blog","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.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript","og_locale":"en_US","og_type":"article","og_title":"Authoring Elasticsearch queries in Javascript","og_description":"To search something in Elasticsearch, the request looks like this: POST https:\/\/es-cluster:9200\/index-name\/_search The query definition is sent as JSON in...","og_url":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript","og_site_name":"GoSquared Blog","article_publisher":"https:\/\/www.facebook.com\/GoSquared","article_published_time":"2018-05-03T12:45:12+00:00","article_modified_time":"2019-11-28T11:03:39+00:00","twitter_card":"summary_large_image","twitter_creator":"@TheDeveloper","twitter_site":"@GoSquared","twitter_misc":{"Written by":"Geoff Wagstaff","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.gosquared.com\/blog\/#organization","name":"GoSquared","url":"https:\/\/www.gosquared.com\/blog\/","sameAs":["https:\/\/instagram.com\/gosquaredteam","https:\/\/www.linkedin.com\/company\/go-squared-ltd.","https:\/\/www.facebook.com\/GoSquared","https:\/\/twitter.com\/GoSquared"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png","contentUrl":"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png","width":1270,"height":250,"caption":"GoSquared"},"image":{"@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/www.gosquared.com\/blog\/#website","url":"https:\/\/www.gosquared.com\/blog\/","name":"GoSquared Blog","description":"Turn visitors into customers.","publisher":{"@id":"https:\/\/www.gosquared.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gosquared.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#webpage","url":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript","name":"Authoring Elasticsearch queries in Javascript - GoSquared Blog","isPartOf":{"@id":"https:\/\/www.gosquared.com\/blog\/#website"},"datePublished":"2018-05-03T12:45:12+00:00","dateModified":"2019-11-28T11:03:39+00:00","breadcrumb":{"@id":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.gosquared.com\/blog"},{"@type":"ListItem","position":2,"name":"Authoring Elasticsearch queries in Javascript"}]},{"@type":"Article","@id":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#article","isPartOf":{"@id":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#webpage"},"author":{"@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448"},"headline":"Authoring Elasticsearch queries in Javascript","datePublished":"2018-05-03T12:45:12+00:00","dateModified":"2019-11-28T11:03:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#webpage"},"wordCount":143,"commentCount":0,"publisher":{"@id":"https:\/\/www.gosquared.com\/blog\/#organization"},"articleSection":["Engineering"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.gosquared.com\/blog\/authoring-elasticsearch-queries-javascript#respond"]}]},{"@type":"Person","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448","name":"Geoff Wagstaff","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g","caption":"Geoff Wagstaff"},"sameAs":["https:\/\/twitter.com\/TheDeveloper"],"url":"https:\/\/www.gosquared.com\/blog\/author\/echo"}]}},"wps_subtitle":"Using JS\u2019 object literal syntax to build queries","_links":{"self":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts\/1654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/comments?post=1654"}],"version-history":[{"count":0,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts\/1654\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/media?parent=1654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/categories?post=1654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/tags?post=1654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}