<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Search Nuggets &#187; AngularJS</title>
	<atom:link href="http://blog.comperiosearch.com/blog/tag/angularjs/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.comperiosearch.com</link>
	<description>A blog about Search as THE solution</description>
	<lastBuildDate>Mon, 13 Jun 2016 08:59:45 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Search templates in elasticsearch</title>
		<link>http://blog.comperiosearch.com/blog/2014/04/11/revised-architecture-for-the-seasonal-recipe-app-elasticsearch-angularjs-search-templates/</link>
		<comments>http://blog.comperiosearch.com/blog/2014/04/11/revised-architecture-for-the-seasonal-recipe-app-elasticsearch-angularjs-search-templates/#comments</comments>
		<pubDate>Fri, 11 Apr 2014 13:56:57 +0000</pubDate>
		<dc:creator><![CDATA[Christoffer Vig]]></dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[qbox]]></category>
		<category><![CDATA[recipe app]]></category>
		<category><![CDATA[recipes]]></category>
		<category><![CDATA[relevancy]]></category>
		<category><![CDATA[search templates]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=2194</guid>
		<description><![CDATA[Using search templates simplifies the life of the client searcher. Now she can have an elasticsearch query DSL expert create search templates for him, hiding away all the ugly booleans, weightings, facets and what have you, while she relaxes in his armchair emitting simple searches for &#8216;beetroot&#8217; or any other favourite vegetable, fish, meats, dairy, or [...]]]></description>
				<content:encoded><![CDATA[<p>Using <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html#search-template">search templates</a> simplifies the life of the client searcher. Now she can have an elasticsearch query DSL expert create search templates for him, hiding away all the ugly booleans, weightings, facets and what have you, while she relaxes in his armchair emitting simple searches for &#8216;beetroot&#8217; or any other favourite vegetable, fish, meats, dairy, or whatever her search domain requires.</p>
<p>Search templates is a brand new feature  added to elasticsearch version 1.1. 0. It allows potentially complex search logic to be stored as templates. The user sends a query with the parameters that matters for him, along with the name of a search template, and the supplied parameters will be inserted at the proper place on execution.  The template format is <a href="http://mustache.github.io/">moustache</a>, a widely used javascript templating tool.</p>
<p>Cool stuff, you say. How can I use it? And does it really work?</p>
<p>In the current implentation of search templates, you create a JSON request specifying both the template and the parameters.</p>
<script src="https://gist.github.com/10469480.js?file=demo1"></script>
<p>You can save the templates in the config/scripts folder of your elasticsearch installation, and use the saved query.<br />
The template is saved as config/scripts/ingredientsquery.moustache. This is how my template looked like:</p>
<script src="https://gist.github.com/10469480.js?file=template"></script>
<p>And here is a sample query using this template</p>
<script src="https://gist.github.com/10469480.js?file=demo2"></script>
<p>To make changes to the template effective, you need to restart elasticsearch.</p>
<p>You would perhaps expect it to be possible to upload a search template using the REST API.  Unfortunately,  that hasn&#8217;t been implemented yet.  At the moment, you have to store the search templates in the  config/scripts folder in your elasticsearch node.  There is an <a href="https://github.com/elasticsearch/elasticsearch/issues/5637">issue</a> registered to add this feature, but in the mean time, our friends over at <a href="http://qbox.io">qbox</a> are helpful as always and offer to manually place the templates where they belong while we wait for elasticsearch version 1.1.1 (or something).</p>
<p>Using this awesome feature, the only reason we still need some middleware between our elasticsearch installation and our browser is to protect the search service from malicious requests. As it so happens, Qbox have recently added a feature that enables public read only access to the elasticsearch query _search endpoint.</p>
<p>This enables us to use the Qbox elasticsearch service directly from the <a href="http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/index.html">angularjs client for elasticsearch</a>.</p>
<p>Thus we eliminate the need for the searchclient in ruby, and one less server component to care about.</p>
<p>Sounds nice? This is work in progress, so <a href="http://blog.comperiosearch.com/blog/tag/recipe-app/">check back every now and then for new blog posts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2014/04/11/revised-architecture-for-the-seasonal-recipe-app-elasticsearch-angularjs-search-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Instant search with AngularJS and elasticsearch</title>
		<link>http://blog.comperiosearch.com/blog/2013/10/24/instant-search-with-angularjs-and-elasticsearch/</link>
		<comments>http://blog.comperiosearch.com/blog/2013/10/24/instant-search-with-angularjs-and-elasticsearch/#comments</comments>
		<pubDate>Thu, 24 Oct 2013 09:22:09 +0000</pubDate>
		<dc:creator><![CDATA[Murhaf Fares]]></dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[elastic.js]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=1826</guid>
		<description><![CDATA[Join our breakfast seminar in Oslo November 28th In this blog post we try to explain how to build a single-page application (SPA) to search in elasticsearch using AngularJS. For simplicity, we will use a twitter index which you can easily create yourself (see here and here). Angular is an open-source JavaScript framework maintained by [...]]]></description>
				<content:encoded><![CDATA[<address><a href="http://www.comperio.no/frokost131128" target="_blank">Join our breakfast seminar in Oslo November 28th</a></address>
<p><img class="alignright size-medium wp-image-1751" src="http://blog.comperiosearch.com/wp-content/uploads/2013/10/es-ang-300x191.png" alt="" width="300" height="191" />In this blog post we try to explain how to build a single-page application (SPA) to search in elasticsearch using <a title="AngularJS " href="http://angularjs.org/" target="_blank">AngularJS</a>. For simplicity, we will use a twitter index which you can easily create yourself (see <a href="https://github.com/s1monw/hammertime" target="_blank">here</a> and <a href="https://github.com/comperiosearch/elasticsearch-uio-demo/blob/master/examples/advanced/setup-twitter-river.md" target="_blank">here</a>).<br />
Angular is an open-source JavaScript framework maintained by Google. According to the official Angular website &#8220;Angular teaches the browser new syntax through a construct we call directives&#8221;&#8212;in other words, Angluar allows you to extend the HTML syntax to make your application &#8216;more dynamic&#8217;. With Angular, we can also build applications following the Model-View-Control (MVC) design pattern.</p>
<p>To communicate with elasticsearch, we rely on <a href="https://github.com/fullscale/elastic.js" target="_blank">elastic.js</a>, a JavaScript client for elasticsearch.</p>
<p>In this post, we assume some basic understanding of search concepts and Angular. If you are not familiar with Angular, see the further readings section.</p>
<h2>Sketching the structure out</h2>
<p>Angular applications, in general, consist of three types of components: models, views and controllers, and our application is no exception.<br />
The model in our application is, more or less, included within elastic.js, which sends requests to elasticsearch and returns the requested information as JSON objects. The views are <em>almost </em>plain HTML pages which allow the user to enter search queries and display the results of those queries. The controllers are mediators between elastic.js and the views. We have two views and two corresponding controllers for Twitter and Stackoverflow, but we will not be implementing the Stackoverflow part; we added it only for the sake of clarity.</p>
<p>The full code is on <a href="https://github.com/Murhaf/angular-elasticsearch-example">github</a>, you can start by looking at the general structure of the application and then come back here to understand how the different parts of this application work together.</p>
<h2>The single page in the single-page application</h2>
<p>Single-page applications contain, as the name suggests, one page whose content changes dynamically. This page is called <code>index.html</code> in our search application. The following code snippet shows the main parts of index.html.</p>
<script src="https://gist.github.com/7038944.js?file=index.html"></script>
<p>There are two important points to note about the code above. First, the Angluar directive <code>np-app</code> within the <code>&lt;html&gt;</code> tag which defines the boundary of the Angular application. Second, the <code>ng-view</code> directive which specifies where the different views will be rendered within the layout of index.html. Said differently, <code>ng-view</code> is &#8216;replaced&#8217; by the dynamic content in the main page.</p>
<h3>searchApp.js</h3>
<p>In each and every Angular application, there is at least one Angular module. &#8220;searchApp&#8221; is the sole and main module in our application, as indicated in <code>ng-app</code> above. The following piece of code defines this module with its dependencies.</p>
<script src="https://gist.github.com/7038944.js?file=searchApp.js"></script>
<p>The module searchApp depends on three other modules, namely: (1) elasticjs.service, the JavaScript client for elasticsearch, elastic.js, (2) <a href="http://docs.angularjs.org/api/ngSanitize" target="_blank">ngSanitize</a>, an Angular module to sanitize HTML, and (3) <a href="http://binarymuse.github.io/ngInfiniteScroll" target="_blank">infinite-scroll</a>, a directive to implement infinite scrolling in Angular.</p>
<p>We also need to configure the so-called routes, URLs linking to views and controllers. In concrete terms, we need to link the items in the navigation bar to HTML partials. To do so, we use the route service, <code>$routeProvider</code>. Observe that each route has a view, templateUrl, and a controller. Recall that we used the directive <code>ng-view</code> to define the dynamic part within the main page, <code>$routeProvider</code> and <code>ng-view</code> are used together to achieve this dynamic behavior.</p>
<h2>Twitter view</h2>
<p>The first thing we need in a search application is a search box, ha? The following code snippet defines an instant search box that calls a search function, <code>search()</code>, every time the input text is changed. This <code>search()</code> function will be defined later in the Twitter controller.</p>
<script src="https://gist.github.com/7040018.js?file=searchbox.twitter.html"></script>
<p>Observe that we bind the input text to a property named <code>queryTerm</code> using the Angular directive <code>ng-model</code>. This binding makes the input value available throughout the Angular application via an object called scope (<code>$scope</code>). It is noteworthy that binding in Angular is two-way, meaning that if the input text (queryTerm) is changed somewhere in the application this change will be directly reflected on the user interface and vice versa.<br />
Now let&#8217;s dive into how the search function works, and then we can come back to displaying the search results.</p>
<h2>Simple queries</h2>
<p>All the search functionalities are defined within the Twitter controller, TwitterController.js. We start by implementing a simple search function and then gradually enhance it. First, we define a controller named Twitter, which depends on <code>$scope</code> and <code>esjResource</code> (the elastic.js client).</p>
<script src="https://gist.github.com/7039581.js?file=TwitterController.js"></script>
<p>Within the twitter controller, we instantiate the elastic.js client, passing the URL of the local elasticsearch instance. All search requests to elasticsearch go through a <a href="http://docs.fullscale.co/elasticjs/ejs.Request.html" target="_blank">Request</a> object, <code>statusRequest</code>, which queries the specified index and type.<br />
Observe that we are using the binding <code>$scope.queryTerm</code> to access the search query. We use the <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-match-query.html" target="_blank">match query</a> family in elasticsearch to search in the <code>_all</code> field for <code>$scope.queryTerm</code>. In addition, we define the fields to be returned by elasticsearch instead of returning full documents. Finally, the search results are appended to <code>$scope.resultArr</code> (again defined within the <code>$scope</code> to make accessible by the views).</p>
<p>With the code above, however, we only get the top ten results matching the user&#8217;s query, usually we want to show more than that, this is where pagination comes into play.</p>
<h3>Pagination with infinite scroll</h3>
<p>Each search request by elastic.js returns ten results by default, but the user&#8217;s information need might not be satisfied by the top 10 results, hence we want to allow the user to load more results if needed. The process of dividing the search results into different pages is called pagination. However, in this example, we will not implement the classic pagination, but instead we will use scrolling; whenever a user scrolls down and reaches the bottom of the page, a new search will be performed with an offset of <code>pege_number*result_per_page</code> and, consequently, a new set of results will be rendered.</p>
<script src="https://gist.github.com/7039581.js?file=pagination.js"></script>
<p>The function <code>show_more</code> will be called whenever the user scrolls down and hits the end of the page. Every time the function <code>show_more</code> is called, it increases the page number, and then calls another search function, <code>searchMore()</code>. The only difference between <code>searchMore()</code> and <code>search()</code> is that the latter defines an offset, <code>.from(offset)</code>.</p>
<h3>Highlighting</h3>
<p>Elasticsearch implements search result highlighting on one or more fields. In the following we define highlighting on the text field, and specify that for each hit the highlighted fragments should be <strong>in bold</strong>.</p>
<script src="https://gist.github.com/7039581.js?file=highlight.js"></script>
<p>The code above defines a <a href="http://docs.fullscale.co/elasticjs/ejs.Highlight.html" target="_blank"><code>Highlight</code></a> object, then passes it to the search request. No changes are required in the search function.</p>
<h2>Viewing, again</h2>
<p>Now that we have defined all the functions needed to send queries to elasticsearch, we can extend our view to display search results.<br />
<script src="https://gist.github.com/7040018.js?file=twitter.html"></script></p>
<p>The directive <code>infinite-scroll</code> specifies that the function <code>show_more()</code> should be called on scroll-down. The directive <a href="http://docs.angularjs.org/api/ng.directive:ngRepeat" target="_blank"><code>ng-repeat</code></a> is like an &#8216;iterative loop&#8217; that repeats the HTML template for each element in the collection specified, e.g. the directive <code>ng-repeat="doc in results.hits.hits"</code> iterates over all the hits returned by elastic.js from elasticsearch. Note that the array <code>resultArr</code> is accessible from the Twitter view because it was defined on the $scope object in TwitterController.js. <code>renderResult</code> and <code>renderResultMetadata</code> are JavaScript functions defined in TwitterController.js, both function return HTML, hence we need to sanitize their returned values using the directive <code>ng-bind-html</code>. We didn&#8217;t explain the functions <code>renderResult</code> and <code>renderResultMetadata</code> as they are straightforward.</p>
<p>In future posts, we will go through faceting with elasticsearch and Angular. If you are curious, though, the code on github already contains faceting.</p>
<h2>Further readings</h2>
<p><a href="http://www.fullscale.co/elasticjs/" target="_blank">elastic.js user guide</a><br />
<a href="http://www.fullscale.co/blog/2013/02/28/getting_started_with_elasticsearch_and_AngularJS_searching.html" target="_blank">Getting Started with elasticsearch and AngularJS: Part1 &#8211; Searching</a><br />
<a href="http://docs.angularjs.org/tutorial/" target="_blank">Angular tutorial &#8212; official website</a><br />
<a href="https://github.com/IgorMinar/foodme" target="_blank">Foodme: Angular example application</a></p>
<p>&nbsp;</p>
<h2><a href="http://www.comperio.no/frokost131128" target="_blank">Join our breakfast seminar in Oslo November 28th</a></h2>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2013/10/24/instant-search-with-angularjs-and-elasticsearch/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
