{"id":7262,"date":"2013-03-07T11:34:54","date_gmt":"2013-03-07T10:34:54","guid":{"rendered":"https:\/\/blog.trifork.com\/?p=7262"},"modified":"2013-03-07T11:34:54","modified_gmt":"2013-03-07T10:34:54","slug":"searching-with-the-elasticshell","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/","title":{"rendered":"Searching with the elasticshell"},"content":{"rendered":"<p><a style=\"float: right\" href=\"http:\/\/github.com\/javanna\/elasticshell\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png\" alt=\"elasticshell\" width=\"150\" height=\"150\" class=\"alignright size-full wp-image-7922\" \/><\/a><br \/>\nSo as promised here is a sequel to my previous post <a href=\"https:\/\/blog.trifork.com\/2013\/03\/06\/introducing-the-elasticshell\/\">Introducing the elasticshell<\/a>. Let&#8217;s start exactly where we left off&#8230;<\/p>\n<p><strong>What about search?<\/strong><br \/>\nWe of course need to search against the created index. We can provide queries as either json documents or Java <a href=\"http:\/\/www.elasticsearch.org\/guide\/reference\/java-api\/query-dsl-queries.html\"><code>QueryBuilders<\/code><\/a> provided with the elasticsearch Java API, which are exposed to the shell as they are.<br \/>\n<!--more--><br \/>\nHere is an example using the basic search method; we can either search on all indexes or on a specific index (and type too). When no query is specified, the <a href=\"http:\/\/www.elasticsearch.org\/guide\/reference\/query-dsl\/match-all-query.html\">match_all<\/a> query is used. Since we are lazy by nature, and we don&#8217;t want to write a whole query by ourselves, let&#8217;s say we want to execute a <a href=\"http:\/\/www.elasticsearch.org\/guide\/reference\/query-dsl\/multi-match-query.html\">multi match query<\/a>, thus we start just copy pasting the example taken from the query DSL documentation.<\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\n&gt; var query = {\n...   &quot;multi_match&quot; : {\n...     &quot;query&quot; : &quot;this is a test&quot;,\n...     &quot;fields&quot; : &#x5B; &quot;subject&quot;, &quot;message&quot; ]\n...   }\n... }\n<\/pre>\n<p>Now we can customize our example as we prefer.<\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\n&gt; query.multi_match.query=&quot;elasticshell&quot;\nelasticshell\n&gt; query.multi_match.fields=&#x5B;&quot;title^2&quot;,&quot;content&quot;]\n&#x5B;title^2, content]\n&gt; query\n{\n  &quot;multi_match&quot;: {\n    &quot;query&quot;: &quot;elasticshell&quot;,\n    &quot;fields&quot;: &#x5B;\n      &quot;title^2&quot;,\n      &quot;content&quot;\n    ]\n  }\n}\n<\/pre>\n<p>The basic search method accepts the whole search source as parameter, which might contain facets, sort and many more fragments. Thus we need to wrap our query in a new json object, let&#8217;s call it <code>searchSource<\/code>.<\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\n&gt; var searchSource={}\n&gt; searchSource.query=query\n{\n  &quot;multi_match&quot;: {\n    &quot;query&quot;: &quot;elasticshell&quot;,\n    &quot;fields&quot;: &#x5B;\n      &quot;title^2&quot;,\n      &quot;content&quot;\n    ]\n  }\n}\n&gt; searchSource\n{\n  &quot;query&quot;: {\n    &quot;multi_match&quot;: {\n      &quot;query&quot;: &quot;elasticshell&quot;,\n      &quot;fields&quot;: &#x5B;\n        &quot;title^2&quot;,\n        &quot;content&quot;\n      ]\n    }\n  }\n}\n<\/pre>\n<p>We are now ready to execute our search:<\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\n&gt; es.search(searchSource);\n{\n  &quot;took&quot;: 6,\n  &quot;timed_out&quot;: false,\n  &quot;_shards&quot;: {\n    &quot;total&quot;: 5,\n    &quot;successful&quot;: 5,\n    &quot;failed&quot;: 0\n  },\n  &quot;hits&quot;: {\n    &quot;total&quot;: 1,\n    &quot;max_score&quot;: 0.19178301,\n    &quot;hits&quot;: &#x5B;\n      {\n        &quot;_index&quot;: &quot;blog&quot;,\n        &quot;_type&quot;: &quot;trifork&quot;,\n        &quot;_id&quot;: &quot;1&quot;,\n        &quot;_score&quot;: 0.19178301,\n        &quot;_source&quot;: {\n          &quot;title&quot;: &quot;Introducing the elasticshell&quot;,\n          &quot;author&quot;: &quot;Luca Cavanna&quot;,\n          &quot;content&quot;: &quot;A few days ago I released the first beta version of the elasticshell, a shell for elasticsearch.&quot;\n        }\n      }\n    ]\n  }\n}\n<\/pre>\n<p>Here is how we can achieve the same result using the more flexible <code>searchBuilder<\/code> method, which allows to specify all the options that elasticsearch supports. The difference is that we don&#8217;t need to specify the whole search source but only the needed parts, like only the query in the example below. We can just use a fragment of the searchSource json object, since it&#8217;s native in the shell.<\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\n&gt; es.searchBuilder().query(searchSource.query).execute();\n{\n  &quot;took&quot;: 5,\n  &quot;timed_out&quot;: false,\n  &quot;_shards&quot;: {\n    &quot;total&quot;: 5,\n    &quot;successful&quot;: 5,\n    &quot;failed&quot;: 0\n  },\n  &quot;hits&quot;: {\n    &quot;total&quot;: 1,\n    &quot;max_score&quot;: 0.19178301,\n    &quot;hits&quot;: &#x5B;\n      {\n        &quot;_index&quot;: &quot;blog&quot;,\n        &quot;_type&quot;: &quot;trifork&quot;,\n        &quot;_id&quot;: &quot;1&quot;,\n        &quot;_score&quot;: 0.19178301,\n        &quot;_source&quot;: {\n          &quot;title&quot;: &quot;Introducing the elasticshell&quot;,\n          &quot;author&quot;: &quot;Luca Cavanna&quot;,\n          &quot;content&quot;: &quot;A few days ago I released the first beta version of the elasticshell, a shell for elasticsearch.&quot;\n        }\n      }\n    ]\n  }\n}\n<\/pre>\n<p>Let&#8217;s say we now want to search for something else, it&#8217;s really easy to modify again our existing query object and execute a new search.<\/p>\n<p>The following is another example, using this time the <a href=\"http:\/\/www.elasticsearch.org\/guide\/reference\/java-api\/query-dsl-queries.html\"><code>QueryBuilders<\/code><\/a> provided with the Java API. The <a href=\"https:\/\/github.com\/elasticsearch\/elasticsearch\/blob\/master\/src\/main\/java\/org\/elasticsearch\/index\/query\/QueryBuilders.java\"><code>QueryBuilders<\/code><\/a> class exposes some static methods that allow to create the different queries supported by the query DSL. All the queries are created through builder objects, which all implement the <a href=\"https:\/\/github.com\/elasticsearch\/elasticsearch\/blob\/master\/src\/main\/java\/org\/elasticsearch\/index\/query\/QueryBuilder.java\"><code>QueryBuilder<\/code><\/a> interface. You can use auto-suggestions here too to know what methods you can use. When you&#8217;re done you just have to provide your own <code>QueryBuilder<\/code> as argument to the <code>queryBuilder<\/code> method and execute your search like this:<\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\n&gt; var queryBuilder = QueryBuilders.multiMatchQuery(&quot;elasticshell&quot;, &quot;title^2&quot;, &quot;content&quot;);\n&gt; es.searchBuilder().queryBuilder(queryBuilder).execute();\n{\n  &quot;took&quot;: 8,\n  &quot;timed_out&quot;: false,\n  &quot;_shards&quot;: {\n    &quot;total&quot;: 5,\n    &quot;successful&quot;: 5,\n    &quot;failed&quot;: 0\n  },\n  &quot;hits&quot;: {\n    &quot;total&quot;: 1,\n    &quot;max_score&quot;: 0.19178301,\n    &quot;hits&quot;: &#x5B;\n      {\n        &quot;_index&quot;: &quot;blog&quot;,\n        &quot;_type&quot;: &quot;trifork&quot;,\n        &quot;_id&quot;: &quot;1&quot;,\n        &quot;_score&quot;: 0.19178301,\n        &quot;_source&quot;: {\n          &quot;title&quot;: &quot;Introducing the elasticshell&quot;,\n          &quot;author&quot;: &quot;Luca Cavanna&quot;,\n          &quot;content&quot;: &quot;A few days ago I released the first beta version of the elasticshell, a shell for elasticsearch.&quot;\n        }\n      }\n    ]\n  }\n}\n<\/pre>\n<p>The limitation of this last approach is that it&#8217;s not trivial to combine it with native json within the shell. The QueryBuilder is the elasticsearch (Java) internal representation of the queries, thus we would need to convert it back to the string format and parse it as json in order to play around with the related json objects. I&#8217;ll explain this better in the next post, together with another solution I&#8217;ve been working on, which would allow you to use the nice <a href=\"http:\/\/www.fullscale.co\/elasticjs\/\">elastic.js<\/a> api within the shell.<\/p>\n<p><strong>Stay tuned!<\/strong><br \/>\nNow that you know how you can execute a search using the elasticshell you have one more good reason to download it and give it a try! More articles and examples will follow too.<br \/>\nFeedback is more than welcome, and again, the released version is still a BETA; should you find any problem, I&#8217;d kindly ask you to open an issue <a href=\"https:\/\/github.com\/javanna\/elasticshell\/issues\">here on github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So as promised here is a sequel to my previous post Introducing the elasticshell. Let&#8217;s start exactly where we left off&#8230; What about search? We of course need to search against the created index. We can provide queries as either json documents or Java QueryBuilders provided with the elasticsearch Java API, which are exposed to [&hellip;]<\/p>\n","protected":false},"author":71,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[15,31,65],"tags":[61,101,50],"class_list":["post-7262","post","type-post","status-publish","format-standard","hentry","category-enterprise-search","category-java","category-big_data_search","tag-elasticsearch","tag-javascript","tag-shell"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Searching with the elasticshell - Trifork 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:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Searching with the elasticshell - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"So as promised here is a sequel to my previous post Introducing the elasticshell. Let&#8217;s start exactly where we left off&#8230; What about search? We of course need to search against the created index. We can provide queries as either json documents or Java QueryBuilders provided with the elasticsearch Java API, which are exposed to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2013-03-07T10:34:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png\" \/>\n<meta name=\"author\" content=\"Luca Cavanna\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Luca Cavanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/\",\"url\":\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/\",\"name\":\"Searching with the elasticshell - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png\",\"datePublished\":\"2013-03-07T10:34:54+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/d9aa0a29580038af46b7a223de3e4fd8\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#primaryimage\",\"url\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png\",\"contentUrl\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Searching with the elasticshell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/d9aa0a29580038af46b7a223de3e4fd8\",\"name\":\"Luca Cavanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56beb100625f636499231471d8c27425?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56beb100625f636499231471d8c27425?s=96&d=mm&r=g\",\"caption\":\"Luca Cavanna\"},\"url\":\"https:\/\/trifork.nl\/blog\/author\/luca\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Searching with the elasticshell - Trifork 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:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/","og_locale":"en_US","og_type":"article","og_title":"Searching with the elasticshell - Trifork Blog","og_description":"So as promised here is a sequel to my previous post Introducing the elasticshell. Let&#8217;s start exactly where we left off&#8230; What about search? We of course need to search against the created index. We can provide queries as either json documents or Java QueryBuilders provided with the elasticsearch Java API, which are exposed to [&hellip;]","og_url":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/","og_site_name":"Trifork Blog","article_published_time":"2013-03-07T10:34:54+00:00","og_image":[{"url":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png","type":"","width":"","height":""}],"author":"Luca Cavanna","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Luca Cavanna","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/","url":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/","name":"Searching with the elasticshell - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#primaryimage"},"image":{"@id":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#primaryimage"},"thumbnailUrl":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png","datePublished":"2013-03-07T10:34:54+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/d9aa0a29580038af46b7a223de3e4fd8"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#primaryimage","url":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png","contentUrl":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2013\/03\/logo-elasticshell-150x150.png"},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/searching-with-the-elasticshell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"Searching with the elasticshell"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/d9aa0a29580038af46b7a223de3e4fd8","name":"Luca Cavanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/56beb100625f636499231471d8c27425?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56beb100625f636499231471d8c27425?s=96&d=mm&r=g","caption":"Luca Cavanna"},"url":"https:\/\/trifork.nl\/blog\/author\/luca\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/7262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/71"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=7262"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/7262\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=7262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=7262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=7262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}