<?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; André Lynum</title>
	<atom:link href="http://blog.comperiosearch.com/blog/author/alynum/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>Voting patterns at the Norwegian parliament</title>
		<link>http://blog.comperiosearch.com/blog/2015/07/30/voting-patterns-at-the-norwegian-parliament/</link>
		<comments>http://blog.comperiosearch.com/blog/2015/07/30/voting-patterns-at-the-norwegian-parliament/#comments</comments>
		<pubDate>Thu, 30 Jul 2015 11:42:37 +0000</pubDate>
		<dc:creator><![CDATA[André Lynum]]></dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[PCA]]></category>
		<category><![CDATA[visualization]]></category>
		<category><![CDATA[word analysis]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3858</guid>
		<description><![CDATA[A couple of weeks ago we saw the blog post visualizing the voting patterns in the Polish parliament. In anticipation of the upcoming election and in the interest of checking up on our elected representatives we thought we would do a similar analysis for the Norwegian parliament. First we will visualize a projection of the voting data [...]]]></description>
				<content:encoded><![CDATA[<p><img class="alignnone" src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Stortinget%2C_Oslo%2C_Norway_%28cropped%29.jpg/640px-Stortinget%2C_Oslo%2C_Norway_%28cropped%29.jpg" alt="By Stortinget,_Oslo,_Norway.jpg: gcardinal from Norway (Stortinget,_Oslo,_Norway.jpg) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons" width="640" height="290" /></p>
<p style="color: #000000">A couple of weeks ago we saw the blog <a style="color: #337ab7" href="https://marcinciura.wordpress.com/2015/07/01/the-vector-space-of-the-polish-parliament-in-pictures/" target="_blank">post</a> visualizing the voting patterns in the Polish parliament. In anticipation of the upcoming election and in the interest of checking up on our elected representatives we thought we would do a similar analysis for the Norwegian parliament. First we will visualize a projection of the voting data of the Norwegian parliament into 3D, and then we will try to interpret the axes of the projection in terms of the issues voted on.</p>
<h2 id="The-data" style="color: #000000">The data</h2>
<p style="color: #000000">Stortinget, the Norwegian parliament, has a nice <a style="color: #337ab7" href="http://data.stortinget.no/" target="_blank">web service</a> for retrieving details about the process an issue passes through in the parliament. Proposals, committees, referendums and so on; there is a surprising amount of detail in there. With a bit of work we pulled out the individual votes for the parliamentary referendums we were able to access. On the less bright side the data doesn&#8217;t appear to be complete as far as we can see, and there is also a range of documented caveats and restrictions regarding which votes are actually registered in the system. This reduces the usefulness of any detailed analysis based on this data, but we still think the aggregated picture one can create is both interesting and valid.</p>
<p style="color: #000000">The voting data comes in the form of for, against or abstained votes. Like in the referenced blog post we would like to visualize the similarity in voting patterns for the representatives. In order to do this we constructed a grid of referendums versus representatives for the current parliamentary period with a single cell representing a vote encoded as 1 for &#8220;for&#8221;, -1 for &#8220;against&#8221; and 0 if &#8220;abstained&#8221; which makes this a neutral midpoint in the analysis we are going to do. This gives a data point for each representative in the multidimensional &#8220;vote space&#8221; where we can do things like clustering or similarity measures to find patterns in the votes.</p>
<h2 id="Visualizing-the-vote-space" style="color: #000000">Visualizing the vote space</h2>
<p style="color: #000000">Since we have the records for 58 referendums so far in the 2013-2017 period and 612 records in the 2009-2013 period we cannot directly visualize our representatives in the vote space. There are several techniques for projecting data like this into lower dimensional sub-spaces while still retaining the essential characteristics of the data. In this blog post we will project the voting data into 3 dimensions using Principal Components Analysis. There are several alternatives here and we chose PCA for the following reasons.</p>
<ul style="color: #000000">
<li>Our data is categorical, but non-binary. It is also distributed symmetrically with 0 representing a neutral value. One would expect that categorical data would not necessarily be modeled well by PCA, but the symmetry and the consistent values involved combined with the density of the data suggests that PCA should behave well.</li>
<li>PCA is based on Singular Value Decomposition (SVD) of the co-variance matrix and consequently has properties that are straightforward to interpret compared to methods such as Multidimensional Scaling (MDS) which seeks to preserve the individual distances between data points.</li>
<li>PCA gives us a projection along axes which have the most variance which in our case should highlight the parts of the data where there is less agreement between parties or representatives. This disagreement is the sort of contrast that we seek to visualize.</li>
<li>PCA axes are linear combinations of the separate referendums which means we can create a comprehensible interpretation of the visualization in terms of political issues.</li>
</ul>
<p style="color: #000000">Other methods are generally harder to interpret with regard to their applicability and the resulting projections (like probabilistic methods such as ICA) or don&#8217;t highlight the contrast we want to visualize (like MDS which preserves the distance relationships between the representatives).</p>
<h2 id="The-plots">The plots</h2>
<p>We&#8217;re just going to present the plots without any political commentary. The first plot is for the current parliamentary period while the second is for the previous period. In the first we have highlighted Miljøpartiet De Grønne which only has a single representative in the current parliament. It is interesting to note the coherence of certain coalition partners in government and the location of the centrist parties. An important measure for the PCA projection is the explained variance ratio of the axes. This is the amount of variance that is preserved in the projected axes and is a measure of how much of the original information is retained in the projection. For our plots the total explained variance is ca. 62% and 57% which means there is quite a bit of diversity in the voting patterns that is not shown in the plot but still enough to consider them informative. The axes are ordered by explained variance so that the first axis explains over 40% of the variance while the remaining two around 10% each.</p>
<div id="attachment_3860" style="width: 610px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/no-parliament-votes-2013-2015.png"><img class="wp-image-3860" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/no-parliament-votes-2013-2015-1024x619.png" alt="no-parliament-votes-2013-2015" width="600" height="363" /></a><p class="wp-caption-text">Projection of voting patterns in the 2013-2015 parliamentary periods.</p></div>
<h2></h2>
<div id="attachment_3859" style="width: 610px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/no-parliament-votes-2009-2013.png"><img class="wp-image-3859" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/no-parliament-votes-2009-2013.png" alt="no-parliament-votes-2009-2013" width="600" height="363" /></a><p class="wp-caption-text">Projection of voting patterns in the 2009-2013 parliamentary periods.</p></div>
<h2 id="Interpreting-the-projection-axes">Interpreting the projection axes</h2>
<p>In and of itself the colorful dots have a limited story to tell. If we could characterize what the axes in the graphs represent we could draw more interesting and detailed inferences. What we will do here is mostly for illustrative purposes though, since it really requires someone knowledgeable in Norwegian parliamentary politics and procedure to build an analysis with proper grounding in actual political activity. Here we will just play around with the data. It is tempting to treat the positive and negative directions of the axes as affirmative/negative on an issue but we would have to look at the text for each referendum to see what &#8220;for&#8221; and &#8220;against&#8221; actually means with regards to the sentiment on each issue. Actually the same issue tend to have both strong positive and negative components in a projected axis which strongly suggests that there isn&#8217;t a single sentiment expressed by the axis direction. Can we create a credible summary of the axes without closely studying each referendum and each issue? Each referendum is part of an issue and it turns out each issue has a list of topics associated with it. So we can make a summary of how much each topic is represented in an axis by the weight for each vote on this issue in a given axis. Each axis is a linear component of the votes and we add up the absolute value of each vote that is part of an issue concerning the topic. To class up this blog a bit we decided to make a word cloud out of the results.</p>
<div id="attachment_3880" style="width: 310px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-3.png"><img class="wp-image-3880 size-medium" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-3-300x150.png" alt="parliament-wordcloud-3" width="300" height="150" /></a><p class="wp-caption-text">Word cloud for PCA component 1 for the 2013-2015 plot.</p></div>
<div id="attachment_3879" style="width: 310px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-2.png"><img class="size-medium wp-image-3879" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-2-300x150.png" alt="Word cloud for PCA component 2 for the 2013-2015 plot." width="300" height="150" /></a><p class="wp-caption-text">Word cloud for PCA component 2 for the 2013-2015 plot.</p></div>
<div id="attachment_3878" style="width: 310px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-1.png"><img class="size-medium wp-image-3878" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-1-300x150.png" alt="Word cloud for PCA component 3 for the 2013-2015 plot." width="300" height="150" /></a><p class="wp-caption-text">Word cloud for PCA component 3 for the 2013-2015 plot.</p></div>
<h3 id="The-most-characteristic-topics" style="color: #000000">The most characteristic topics</h3>
<p style="color: #000000">Some topics account for most of the activity in parliament and tend to dominate the data if we look only at the volume of votes. For the present parliamentary period the data is rather sparse so this isn&#8217;t as pronounced in the word clouds, but for the 2009-2013 period where there is a lot more data transportation/communication and health dominates in all the axes. Can we see what topics are characteristic for an axis instead? To highlight topics that have a high overall weight in our projection in comparison to their overall presence in the referendums we weigh each topic by its &#8220;Inverse Topic Frequency&#8221; &#8211; similar to the common Inverse Document Frequency (IDF) common in search relevance and information retrieval. This makes rarer but highly weighted topics stand out. This weighting gives us a clearer picture of how the axes differ from each other, even if they don&#8217;t necessarily show the ratio of influence of these topics on the projection itself.</p>
<div id="attachment_3881" style="width: 310px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-4.png"><img class="size-medium wp-image-3881" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-4-300x150.png" alt="Contrastive word cloud for PCA component 1 for the 2009-2013 plot." width="300" height="150" /></a><p class="wp-caption-text">Contrastive word cloud for PCA component 1 for the 2009-2013 plot.</p></div>
<div id="attachment_3882" style="width: 310px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-5.png"><img class="size-medium wp-image-3882" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-5-300x150.png" alt="Contrastive word cloud for PCA component 2 for the 2009-2013 plot." width="300" height="150" /></a><p class="wp-caption-text">Contrastive word cloud for PCA component 2 for the 2009-2013 plot.</p></div>
<div id="attachment_3883" style="width: 310px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-6.png"><img class="size-medium wp-image-3883" src="http://blog.comperiosearch.com/wp-content/uploads/2015/07/parliament-wordcloud-6-300x150.png" alt="Contrastive word cloud for PCA component 3 for the 2009-2013 plot." width="300" height="150" /></a><p class="wp-caption-text">Contrastive word cloud for PCA component 3 for the 2009-2013 plot.</p></div>
<h2 id="Wrapping-up">Wrapping up</h2>
<p>While the plots shown here are both interesting and entertaining there are many possibilities available when creating these visualizations and interpreting them. Consequently while they can be very helpful during analysis and point out interesting directions one might not have noticed otherwise, a purely quantitative analysis can fall prey to  &#8220;researchers degrees of freedom&#8221; and steer conclusions towards predetermined biases and worse. External validation and domain expertise can help ground models and inferences in reality. With the help of a domain expert we could shape the data into a format more amenable for analysis, for example by weighting issues by importance and normalizing the vote sentiment across referendums. This would provide us with a much firmer foundation for making inferences than the raw data by itself as we have done here.</p>
<h2 id="The-plots"></h2>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2015/07/30/voting-patterns-at-the-norwegian-parliament/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Elasticsearch calculates significant terms</title>
		<link>http://blog.comperiosearch.com/blog/2015/06/10/how-elasticsearch-calculates-significant-terms/</link>
		<comments>http://blog.comperiosearch.com/blog/2015/06/10/how-elasticsearch-calculates-significant-terms/#comments</comments>
		<pubDate>Wed, 10 Jun 2015 11:02:28 +0000</pubDate>
		<dc:creator><![CDATA[André Lynum]]></dc:creator>
				<category><![CDATA[Elasticsearch]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[aggregations]]></category>
		<category><![CDATA[lexical analysis]]></category>
		<category><![CDATA[relevance]]></category>
		<category><![CDATA[significant terms]]></category>
		<category><![CDATA[word analysis]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3785</guid>
		<description><![CDATA[Many of you who use Elasticsearch may have used the significant terms aggregation and been intrigued by this example of fast and simple word analysis. The details and mechanism behind this aggregation tends to be kept rather vague however and couched in terms like &#8220;magic&#8221; and the commonly uncommon. This is unfortunate since developing informative [...]]]></description>
				<content:encoded><![CDATA[<div id="attachment_3823" style="width: 310px" class="wp-caption aligncenter"><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/06/uncommonlycommon.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/06/uncommonlycommon-300x187.png" alt="The &quot;unvommonly common&quot;" width="300" height="187" class="size-medium wp-image-3823" /></a><p class="wp-caption-text">The magic of the &#8220;uncommonly common&#8221;.</p></div>
<p>Many of you who use Elasticsearch may have used the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html" title="significant terms">significant terms aggregation</a> and been intrigued by this example of fast and simple word analysis. The details and mechanism behind this aggregation tends to be kept rather vague however and couched in terms like &#8220;magic&#8221; and the commonly uncommon. This is unfortunate since developing informative analyses based on this aggregation requires some adaptation to the underlying documents especially in the face of less structured text. Significant terms seems especially susceptible to garbage in &#8211; garbage out effects and developing a robust analysis requires some understanding of the underlying data. In this blog post we will take a look at the default relevance score used by the significance terms aggregation, the mysteriously named JLH score, as it is implemented in Elasticsearch 1.5. This score is especially developed for this aggregation and experience shows that it tends to be the most effective one available in Elasticsearch at this point.</p>
<p>The JLH relevance scoring function is not given in the documentation. A quick dive into the code however and we find the following scoring function.</p>
<img src='http://s0.wp.com/latex.php?latex=++JLH+%3D+%5Cleft%5C%7B%5Cbegin%7Bmatrix%7D++%28p_%7Bfore%7D+-+p_%7Bback%7D%29%5Cfrac%7Bp_%7Bfore%7D%7D%7Bp_%7Bback%7D%7D+%26+p_%7Bfore%7D+-+p_%7Bback%7D+%3E+0+%5C%5C++0++%26+elsewhere++%5Cend%7Bmatrix%7D%5Cright.++&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='  JLH = \left\{\begin{matrix}  (p_{fore} - p_{back})\frac{p_{fore}}{p_{back}} &amp; p_{fore} - p_{back} &gt; 0 \\  0  &amp; elsewhere  \end{matrix}\right.  ' title='  JLH = \left\{\begin{matrix}  (p_{fore} - p_{back})\frac{p_{fore}}{p_{back}} &amp; p_{fore} - p_{back} &gt; 0 \\  0  &amp; elsewhere  \end{matrix}\right.  ' class='latex' />
<p>Here the <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> is the frequency of the term in the foreground (or query) document set, while <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> is the term frequency in the background document set which by default is the whole index.</p>
<p>Expanding the formula gives us the following which is quadratic in <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' />.</p>
<img src='http://s0.wp.com/latex.php?latex=++%28p_%7Bfore%7D+-+p_%7Bback%7D%29%5Cfrac%7Bp_%7Bfore%7D%7D%7Bp_%7Bback%7D%7D+%3D+%5Cfrac%7Bp_%7Bfore%7D%5E2%7D%7Bp_%7Bback%7D%7D+-+p_%7Bfore%7D++&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='  (p_{fore} - p_{back})\frac{p_{fore}}{p_{back}} = \frac{p_{fore}^2}{p_{back}} - p_{fore}  ' title='  (p_{fore} - p_{back})\frac{p_{fore}}{p_{back}} = \frac{p_{fore}^2}{p_{back}} - p_{fore}  ' class='latex' />
<p>By keeping <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> fixed and keeping in mind that both it and <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> is positive we get the following function plot. Note that <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> is unnaturally large for illustration purposes.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/06/JLH-pb-fixed.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/06/JLH-pb-fixed-300x206.png" alt="JLH-pb-fixed" width="300" height="206" class="alignnone size-medium wp-image-3792"></a></p>
<p>On the face of it this looks bad for a scoring function. It can be undesirable that it changes sign, but more troublesome is the fact that this function is not monotonically increasing.</p>
<p>The gradient of the function:</p>
<img src='http://s0.wp.com/latex.php?latex=++%5Cnabla+JLH%28p_%7Bfore%7D%2C+p_%7Bback%7D%29+%3D+%5Cleft%28%5Cfrac%7B2+p_%7Bfore%7D%7D%7Bp_%7Bback%7D+-+1%7D+%2C+-%5Cfrac%7Bp_%7Bfore%7D%5E2%7D%7Bp_%7Bback%7D%5E2%7D%5Cright%29++&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='  \nabla JLH(p_{fore}, p_{back}) = \left(\frac{2 p_{fore}}{p_{back} - 1} , -\frac{p_{fore}^2}{p_{back}^2}\right)  ' title='  \nabla JLH(p_{fore}, p_{back}) = \left(\frac{2 p_{fore}}{p_{back} - 1} , -\frac{p_{fore}^2}{p_{back}^2}\right)  ' class='latex' />
<p>Setting the gradient to zero we see by looking at the second coordinate that the JLH does not have a minimum, but approaches it when <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> approaches zero where the function is undefined. While the second coordinate is always positive, the first coordinate shows us where the function is not increasing.</p>
<img src='http://s0.wp.com/latex.php?latex=++%5Cbegin%7Baligned%7D++%5Cfrac%7B2+p_%7Bfore%7D%7D%7Bp_%7Bback%7D%7D++-+1+%26+%3C+0+%5C%5C++p_%7Bfore%7D+%26+%3C+%5Cfrac%7B1%7D%7B2%7Dp_%7Bback%7D++%5Cend%7Baligned%7D++&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='  \begin{aligned}  \frac{2 p_{fore}}{p_{back}}  - 1 &amp; &lt; 0 \\  p_{fore} &amp; &lt; \frac{1}{2}p_{back}  \end{aligned}  ' title='  \begin{aligned}  \frac{2 p_{fore}}{p_{back}}  - 1 &amp; &lt; 0 \\  p_{fore} &amp; &lt; \frac{1}{2}p_{back}  \end{aligned}  ' class='latex' />
<p>Furtunately the decreasing part of the function is in an area where <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D+-+p_%7Bback%7D+%3C+0&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore} - p_{back} &lt; 0' title='p_{fore} - p_{back} &lt; 0' class='latex' /> and the JLH score explicitly defined as zero. By symmetry of the square around the minimum of the first coordinate of the gradient around <img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7Dp_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{1}{2}p_{back}' title='\frac{1}{2}p_{back}' class='latex' /> we also see that the entire area where the score is below zero is in this region.</p>
<p>With this it seems sensible to just drop the linear term of the JLH score and just use the quadratic part. This will result in the same ranking with a slightly less steep increase in score as <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> increases.</p>
<img src='http://s0.wp.com/latex.php?latex=++JLH_%7Bmod%7D+%3D+%5Cfrac%7Bp_%7Bfore%7D%5E2%7D%7Bp_%7Bback%7D%7D++&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='  JLH_{mod} = \frac{p_{fore}^2}{p_{back}}  ' title='  JLH_{mod} = \frac{p_{fore}^2}{p_{back}}  ' class='latex' />
<p>Looking at the level sets for the JLH score there is a quadratic relationship between the <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' />. Solving for a fixed level <img src='http://s0.wp.com/latex.php?latex=k&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='k' title='k' class='latex' /> we get:</p>
<img src='http://s0.wp.com/latex.php?latex=++%5Cbegin%7Baligned%7D++JLH+%3D+%26+%5Cfrac%7Bp_%7Bfore%7D%5E2%7D%7Bp_%7Bback%7D%7D+-+p_%7Bfore%7D+%3D+k+%5C%5C+++%26+p_%7Bfore%7D%5E2+-+p_%7Bfore%7D+-+k%5Ccdot+p_%7Bback%7D++%3D+0+%5C%5C+++%26+p_%7Bfore%7D+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cpm+%5Cfrac%7B%5Csqrt%7B1+%2B+4+%5Ccdot+k+%5Ccdot+p_%7Bback%7D%7D%7D%7B2%7D++%5Cend%7Baligned%7D++&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='  \begin{aligned}  JLH = &amp; \frac{p_{fore}^2}{p_{back}} - p_{fore} = k \\   &amp; p_{fore}^2 - p_{fore} - k\cdot p_{back}  = 0 \\   &amp; p_{fore} = \frac{1}{2} \pm \frac{\sqrt{1 + 4 \cdot k \cdot p_{back}}}{2}  \end{aligned}  ' title='  \begin{aligned}  JLH = &amp; \frac{p_{fore}^2}{p_{back}} - p_{fore} = k \\   &amp; p_{fore}^2 - p_{fore} - k\cdot p_{back}  = 0 \\   &amp; p_{fore} = \frac{1}{2} \pm \frac{\sqrt{1 + 4 \cdot k \cdot p_{back}}}{2}  \end{aligned}  ' class='latex' />
<p>Where the negative part is outside of function definition area.<br />
This is far easier to see in the simplified formula.</p>
<img src='http://s0.wp.com/latex.php?latex=++%5Cbegin%7Baligned%7D++JLH+%3D+%26+%5Cfrac%7Bp_%7Bfore%7D%5E2%7D%7Bp_%7Bback%7D%7D+%3D+k+%5C%5C+++%26+p_%7Bfore%7D+%3D+%5Csqrt%7Bk+%5Ccdot+p_%7Bback%7D%7D++%5Cend%7Baligned%7D++&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='  \begin{aligned}  JLH = &amp; \frac{p_{fore}^2}{p_{back}} = k \\   &amp; p_{fore} = \sqrt{k \cdot p_{back}}  \end{aligned}  ' title='  \begin{aligned}  JLH = &amp; \frac{p_{fore}^2}{p_{back}} = k \\   &amp; p_{fore} = \sqrt{k \cdot p_{back}}  \end{aligned}  ' class='latex' />
<p>An increase in <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> must be offset by approximately a square root increase in <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> to  retain the same score.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/06/JLH-contour.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/06/JLH-contour-300x209.png" alt="JLH-contour" width="300" height="209" class="alignnone size-medium wp-image-3791"></a></p>
<p>As we see the score increases sharply as <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> increases in a quadratic manner against <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' />. As <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> becomes small compared to <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> the growth goes from linear in <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> to squared.</p>
<p>Finally a 3D plot of the score function.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/06/JLH-3d.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/06/JLH-3d-300x203.png" alt="JLH-3d" width="300" height="203" class="alignnone size-medium wp-image-3790"></a></p>
<p>So what can we take away from all this? I think the main practical consideration is the squared relationship between <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> which means once there is significant difference between the two the <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> will dominate the score ranking. The <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> factor primarily makes the score sensitive when this factor is small and for reasonable similar <img src='http://s0.wp.com/latex.php?latex=p_%7Bback%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{back}' title='p_{back}' class='latex' /> the <img src='http://s0.wp.com/latex.php?latex=p_%7Bfore%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p_{fore}' title='p_{fore}' class='latex' /> decides the ranking. There are some obvious consequences from this which would be interesting to explore in real data. First that you would like to have a large background document set if you want more fine grained sensitivity to background frequency. Second, foreground frequencies can dominate the score to such an extent that peculiarities of the implementation may show up in the significant terms ranking, which we will look at in more detail as we try to apply the significant terms aggregation to single documents.</p>
<p>The results and visualizations in this blog post is also available as an <a href="https://github.com/andrely/ipython-notebooks/blob/master/JLH%20score%20characteristics.ipynb" title="JLH score characteristics">iPython notebook</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2015/06/10/how-elasticsearch-calculates-significant-terms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using iPython notebooks and Pycharm together</title>
		<link>http://blog.comperiosearch.com/blog/2015/05/11/using-ipython-notebooks-and-pycharm-together/</link>
		<comments>http://blog.comperiosearch.com/blog/2015/05/11/using-ipython-notebooks-and-pycharm-together/#comments</comments>
		<pubDate>Mon, 11 May 2015 11:12:24 +0000</pubDate>
		<dc:creator><![CDATA[André Lynum]]></dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[autoreload]]></category>
		<category><![CDATA[ipython notebook]]></category>
		<category><![CDATA[pycharm]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3626</guid>
		<description><![CDATA[IPython notebooks have become an indispensable tool for many Python developers. They are a reasonably good environment for interactive computing, can contain inline data visualisations and can be hosted remotely for sharing results or working together with other developers. In many academic environments and increasingly in industry IPython notebooks are used for data visualisation work [...]]]></description>
				<content:encoded><![CDATA[<p><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-7.png" alt="ipython-blog-7" width="282" height="70" class="aligncenter size-large wp-image-3628" /><br />
<a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-6.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-6.png" alt="ipython-blog-6" width="600" class="aligncenter size-full wp-image-3627" /></a></p>
<p />
IPython notebooks have become an indispensable tool for many Python developers. They are a reasonably good environment for interactive computing, can contain inline data visualisations and can be hosted remotely for sharing results or working together with other developers. In many academic environments and increasingly in industry IPython notebooks are used for data visualisation work and exploratory programming, depending on the IPython interactive environment for fast prototyping of ideas.</p>
<p />
As nice an environment we have in IPython, I often wish for the features of a full-fledged IDE. Here at Comperio we use PyCharm a lot which has excellent code editing, semantic completion, a graphical debugger and efficient code navigaton capabilities. In this blog post I’m going to show how you can simultaneously work on code in both the IDE and IPython notebook or interactive shell while keeping the running notebook and IDE project in sync.</p>
<p />
Hey, PyCharm already have IPython notebook integration. What about that? Personally I find that the IPython notebook integration in the latest PyCharm (version 4.0.6)  still isn’t adequate for serious work. You get the the completion and code navigation from PyCharm, but editing and navigation is reduced to half a dozen buttons. Further some functionality such as debugging appears to be plainly non-functional. Regardless there are other very nice IDEs for Python such as Wing or Eclipse, and the approach here will work equally well with them.</p>
<p />
This cunning recipe consists of two spicy ingredients, Both are neat tricks on their own, but together they form a smooth workflow bridging exploratory programming and more structured software engineering. We are going to:</p>
<p />
<ul>
<li>Install our code as an editable Pip package.</li>
<li>Use the IPython autoreload extension to dynamically reload code.</li>
</ul>
<p />
So let’s get cooking!</p>
<p />
<h2>Editable Pip packages</h2>
<p />
We are going to organise our code in a Python package and install it with Pip using the <code>-e</code> or <code>—editable</code> option. This installs the package as it is pointing to our project directory and that we are always importing the code that we are editing. We could also accomplish this with some hacking on <code>sys.path</code> or <code>PYTHONPATH</code>, but having our code available as a package is a lot more seamless. It makes sense to use <code>virtualenv</code> (or EPD/Anaconda environments) to isolate your system Python from your development packages.</p>
<p />
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-1.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-1-300x190.png" alt="ipython-blog-1" width="300" height="190" class="aligncenter size-medium wp-image-3620" /></a></p>
<p />
First we create Python project in PyCharm, add source folder with <code>setup.py</code> defining a basic python package.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-2.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-2-300x194.png" alt="ipython-blog-2" width="300" height="194" class="aligncenter size-medium wp-image-3621" /></a></p>
<p />
Then we create stub file with the following code in our python module and a folder for our notebooks.</p>
<p />
<p></p><pre class="crayon-plain-tag">def get_page():
    print "Don't know how to do this yet."</pre><p> </p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-3.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-3-300x158.png" alt="ipython-blog-3" width="300" height="158" class="aligncenter size-medium wp-image-3622" /></a></p>
<p />
And we activate our <code>virtualenv</code>/Conda environment and run <code>pip install -e </code>.</p>
<p />
<strong>Pip and Git: </strong>If you install your package with <code>-e</code> from a Git repository it may think that you want to install from Git even if you&#8217;re giving it a file path. This is usually not what you want when you&#8217;re developing since you would have to commit your code for the package to update itself. An ugly but practical way to avoid this behavior is to move the .git folder out of the way when installing the package.</p>
<p />
<h2>%autoreloading code</h2>
<p />
Now to the important part which is dynamically loading the IDE project into our IPython notebook. Let’s first fire up the notebook.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-4.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-4-300x71.png" alt="ipython-blog-4" width="300" height="71" class="aligncenter size-medium wp-image-3623" /></a></p>
<p />
Start iPython and create a notebook.</p>
<p />
You have probably used reload(module) to update the Python environment at runtime. This hardly ever works for more than five minutes and results in an inscrutable mess of old new stuff in your modules and classes.There are however a bag of neat tricks taking care of at least the majority of the problems around reloading Python code or modules (see http://pyunit.sourceforge.net/notes/reloading.html),  and the IPython developers has collected these into their autoreload extension. Let’s look at it in action.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-5.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-5-300x137.png" alt="ipython-blog-5" width="300" height="137" class="aligncenter size-medium wp-image-3624" /></a></p>
<p />
Here we set up the autoreload module and import our stub function in the first two cells. In the third we run our function. We then change the function definition in the IDE and save the file.</p>
<p></p><pre class="crayon-plain-tag">def get_page():
    print "Hey I'm updated."</pre><p> </p>
<p />
<p>And when we run the function again in cell four the updated code is run.</p>
<p />
<h2>From notebook to project and back</h2>
<p />
Combining editable Pip packages and the autoreload module we have a way to seamlessly load our project code in the notebook. When we are ready to move our exploratory programming back to project we can move our code over, import any new definitions and refine our implementation while using it in our notebook. In this way we can quickly move from noodling around in the notebook to developing and testing in the IDE and move back to the notebook to use our project code in further unstructured meanderings.</p>
<p />
In the next post we will demonstrate this in more detail.</p>
<p />
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2015/05/11/using-ipython-notebooks-and-pycharm-together/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beer and searching at Elasticon</title>
		<link>http://blog.comperiosearch.com/blog/2015/03/09/beer-searching-elasticon/</link>
		<comments>http://blog.comperiosearch.com/blog/2015/03/09/beer-searching-elasticon/#comments</comments>
		<pubDate>Sun, 08 Mar 2015 22:58:16 +0000</pubDate>
		<dc:creator><![CDATA[André Lynum]]></dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Chinatown]]></category>
		<category><![CDATA[Elasticon]]></category>
		<category><![CDATA[home grown hops and rare yeasts]]></category>
		<category><![CDATA[Liars Dice]]></category>
		<category><![CDATA[norwegian]]></category>
		<category><![CDATA[Nøgne Ø IPA]]></category>
		<category><![CDATA[Sierra Nevada pale ale]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3379</guid>
		<description><![CDATA[Christoffer was pacing angrily back and forth, Nøgne Ø IPA in his left hand, phone in the other. I was looking at the the long list of cancellations, including our connecting flight to Arlanda on the way to SF. The Norwegian strike was hitting hard with nearly no planes flying in Europe. Arlanda airport is [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/03/IMG_20150307_005509-3.jpg"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/03/IMG_20150307_005509-3-300x219.jpg" alt="IMG_20150307_005509 (3)" width="300" height="219" class="alignnone size-medium wp-image-3380" /></a></p>
<p />
Christoffer was pacing angrily back and forth, Nøgne Ø IPA in his left hand, phone in the other. I was looking at the the long list of cancellations, including our connecting flight to Arlanda on the way to SF. The Norwegian strike was hitting hard with nearly no planes flying in Europe.</p>
<p />
Arlanda airport is fairly described as the butthole of the world. Filled up with angry swedes and featuring the worlds slowest transfer security gate. We had booked the flight being fully aware of the pain and the risks and now the plane wasn’t even going to land there.</p>
<p />
&#8220;Listen up&#8221; Christoffer was growling between gulps of strong IPA, &#8220;There’s no way we’ll be planted in your crap airport for over 12 hours bullshit strike or not.&#8221;</p>
<p />
&#8220;Calm down&#8221; I looked up at the furious bearded giant. &#8220;You’re rattling my nerves.&#8221;</p>
<p />
&#8220;Besides we still have CEOs credit card. Get some other plane and upgrade our tickets to business class while you’re at it. I’ll need some rest and proper legspace after all this crap.&#8221;</p>
<p />
&#8230;</p>
<p />
Out of breath after a mad dash to the airport service desk with Christoffers 60 pound america suitcase in tow, we were swaying dangerously with a Nøgne Ø beer heavy on our breath.</p>
<p />
&#8220;We need tickets for the next plane to Arlanda. It’s of the utmost importance that we reach Arlanda as quickly as possible!&#8221;</p>
<p />
The man at the counter tried his best to ignore us. Maybe we skipped line, I don’t know.</p>
<p />
&#8220;Listen up, we are going to Elasticon in SF. The most degenerate collection of search professionals in the world. It&#8217;s a nasty assignment but somebody has to do it. We need to catch our flight from Arlanda and all this strike bullshit has left me with no patience!&#8221;</p>
<p />
The man at the desk was sensíng an ugly scene. Two mean IT bums with their CEOs card and shot nerves. He ignored the shouting and commotion behind us and got to work.</p>
<p />
&#8230;</p>
<p />
Feet up in bclass seats on our way to SF we finally got some rest. Christoffer scanning the bclass microbrewery selection with a critical eye. &#8220;I need an imperial stout! In case of turbulence you know, need the extra weight. Maybe two or three even.&#8221; &#8220;Suit yourself&#8221; I said nipping a light lager while Christoffer was waving and hollering at the flight attendants. &#8220;Turbulence can be heavy shit for sure.&#8221;</p>
<p />
&#8230;</p>
<p />
Coming down in SF we knew we had to tighten up in front of the nasty customs procedure awaiting us. Christoffers america suitcase was another concern, but we were banking his large collection of home grown hops and rare yeasts not triggering any bomb sensors or something. The trick is to stomp your toe into something right before approaching the customs official. This will give you the steely stare and tight grimace needed to pass muster at the in front of the customs official asking why you’ld ever come here and if you have the means to butt yourself out before you become too much of a nuisance. &#8220;It’s a conference&#8221; I’ve wheezed through my gritting teeth. &#8220;But it might as well be a madhouse. These people are serious business&#8221;. The man behind the counter wasn’t really satisfied but didn’t press any further, and we hobbled on to pick up Christoffers gigantic trunk at arrivals.</p>
<p />
&#8230;</p>
<p />
Eleven hours of dry recycled air and flat beers can break any man, and stumbling out of Oakland airport we knew we were in a particularly bad shape. After his half a dozen heavy stouts Christoffer had entered a nasty beer induced coma and snored like an annoyed elephant for nine consecutive hours. Now he was wide awake of course ready for action while the rest of us hadn’t slept for 28 hours or so. Jetlag was about to do a double trick on us and I knew we had to take action.</p>
<p />
There is only two remedies available to a man facing the disorientation and confusion of a serious jetlag and that is either to desperately stay awake long enough or induce a serious comatose sleep by an means necessary.<br />
&#8220;If we hurry we can get into some dive before it’s too late. Load you up and then get you back to the hotel so I can get some proper rest.&#8221; I said.  A plan I deeply regretted trying to keep myself together in a dingy Liars Dice den in Chinatown watching Christoffer enjoying a Sierra Nevada pale ale. The sharp rattling of dice cups among huge piles of dollar bills on the bar counter was jangling my nerves and leaving me with a sense that we had embarked something quite different than what we had signed up for &#8230;​</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2015/03/09/beer-searching-elasticon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comperio goes to Elasticon</title>
		<link>http://blog.comperiosearch.com/blog/2015/02/27/comperio-goes-elasticon/</link>
		<comments>http://blog.comperiosearch.com/blog/2015/02/27/comperio-goes-elasticon/#comments</comments>
		<pubDate>Fri, 27 Feb 2015 14:24:38 +0000</pubDate>
		<dc:creator><![CDATA[André Lynum]]></dc:creator>
				<category><![CDATA[Elasticsearch]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[elasticon elk elasticsearch kibana analytics]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3358</guid>
		<description><![CDATA[Elasticon, the first Elasticsearch user conference, is coming in a couple of weeks. Hosted in San-Francisco, the agenda promises a lot of interesting use cases and in-depth information about Elasticsearch and the ELK (Elasticsearch, Logstash, Kibana) analytics stack. It is an 11 hour plane trip away, but here at Comperio we consider Elasticsearch one of [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/02/elasticon.png"><img class="alignnone size-medium wp-image-3359" src="http://blog.comperiosearch.com/wp-content/uploads/2015/02/elasticon-300x120.png" alt="elasticon" width="300" height="120" /></a></p>
<p />
<a href="http://elasticon.com">Elasticon</a>, the first Elasticsearch user conference, is coming in a couple of weeks. Hosted in San-Francisco, the agenda promises a lot of interesting use cases and in-depth information about Elasticsearch and the ELK (Elasticsearch, Logstash, Kibana) analytics stack. It is an 11 hour plane trip away, but here at Comperio we consider Elasticsearch one of the most exciting developments in search today. Not only is it a scalable and flexible search platform but it is also at forefront of combining data analytics, text mining and information retrieval in a single scalable and cohesive platform. So it doesn&#8217;t matter if Elasticon is nearly on the other side of the planet, we can&#8217;t really miss this opportunity to get on top of the latest developments surrounding Elasticsearch.</p>
<p />
<p>At Comperio we&#8217;re happy to see that search is becoming so much more than it used to be. Elasticsearch has proven to be a platform that not only does search well, but also integrates documents with data in a way that enables information oriented applications. At the front of this wave of new applications is the ELK stack which makes it easy to build a complete pipeline for analytics. Search analytics, system monitoring or web analytics are all areas where a realtime reporting platform can be built on top of ELK. Combining the data analytics capabilities of Elasticsearch also allows us to build information insight driven applications for our customers, combining offline and realtime text analysis and data aggregation with web based visualization based on D3.js. Especially the real time queries has made it possible for users to drill-down into and compare specific pieces of data in an exploratory manner. These new applications does not only require a fast and scalable technology core, but also solid insight into search technology and an ability to modify it for specific requirements.</p>
<p />
On to Elasticon it is no surprise that there is a huge focus on all aspects of ELK, We&#8217;re looking forward to see how other companies are adopting the ELK stack to their projects and get new ideas of how we can help our customers bring out the value in their data with the ELK software. There are also some sessions on Shield, the new access security subsystem for Elasticsearch. This is a long asked for component which will surely make integration easier in many projects. We&#8217;re also looking forward to the sessions on Elasticsearch and ELK internals as we are now looking into several extensions that we&#8217;d like to implement in Elasticsearch.</p>
<p />
On to the sessions we&#8217;re looking forward to the ELK use cases, especially the <em>&#8220;Tackling Security Logs with the ELK&#8221;</em> and <em>&#8220;The ELK &amp; The Eagle: Search &amp; Analytics for the US Government&#8221;</em>. Also ELK internals sessions such as <em>&#8220;The Contributor&#8217;s Guide to the Kibana&#8221;</em> and <em>&#8220;Life of an Event in Logstash&#8221;</em>, in addition to <em>&#8220;Elasticsearch Architecture: Amusing Algorithms and Details on Data Structures&#8221;</em>. Building data centric applications is also covered in <em>&#8220;Using Elasticsearch to Unlock an Analytical Goldmine&#8221;</em>, <em>&#8220;Navigating Through the World&#8217;s Encyclopedia&#8221;</em> and <em>&#8220;The ELK Stack for Time Series Data&#8221;</em> which we hope can give us some fresh viewpoints on using Elasticsearch in our projects related to intelligence and data mining.</p>
<p />
Hope to see some of you in San Francisco :)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2015/02/27/comperio-goes-elasticon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some CSV import tricks in Neo4j</title>
		<link>http://blog.comperiosearch.com/blog/2015/02/04/csv-import-tricks-neo4j/</link>
		<comments>http://blog.comperiosearch.com/blog/2015/02/04/csv-import-tricks-neo4j/#comments</comments>
		<pubDate>Wed, 04 Feb 2015 12:46:57 +0000</pubDate>
		<dc:creator><![CDATA[André Lynum]]></dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3242</guid>
		<description><![CDATA[The CSV file import facility in Neo4J is interesting in that it allows you to run Cypher queries iteratively over your dataset. This gives us a lot of flexibility and relieves us of the need for transforming our data to a Neo4J specific format. We can export tables with for example foreign keys to other [...]]]></description>
				<content:encoded><![CDATA[<p>The CSV file import facility in Neo4J is interesting in that it allows you to run Cypher queries iteratively over your dataset. This gives us a lot of flexibility and relieves us of the need for transforming our data to a Neo4J specific format. We can export tables with for example foreign keys to other tables and reconstruct our relationships during import. The key to doing this successfully is adding nodes and relationships on demand with the MERGE clause.</p>
<p />
There are some quite good resources on CSV import in Neo4J already. Instead of repeating it we&#8217;ll just drop the linkshere:</p>
<p />
<a href="http://neo4j.com/docs/stable/queryn-load-csv.html">http://neo4j.com/docs/stable/queryn-load-csv.html</a><br />
<a href="http://neo4j.com/docs/stable/query-load-csv.html">http://neo4j.com/docs/stable/query-load-csv.html</a><br />
<a href="http://jexp.de/blog/2014/06/load-csv-into-neo4j-quickly-and-successfully/">http://jexp.de/blog/2014/06/load-csv-into-neo4j-quickly-and-successfully/</a><br />
<a href="http://blog.graphenedb.com/blog/2015/01/13/importing-data-into-neo4j-via-csv/">http://blog.graphenedb.com/blog/2015/01/13/importing-data-into-neo4j-via-csv/</a><br />
<a href="http://jexp.de/blog/2014/06/using-load-csv-to-import-git-history-into-neo4j/">http://jexp.de/blog/2014/06/using-load-csv-to-import-git-history-into-neo4j/</a></p>
<p />
<p>To recap the main points:</p>
<p />
<ul>
<li>Set up constraints and indexes before importing. Without them the import queries will be very slow</li>
<li>Use USING PERIODIC COMMIT to speed up the importing process.</li>
<li>Make sure your heap is big enough, especially if committing multiple queries. It is recommended that the entire dataset fits in the cache (see <a href="http://neo4j.com/docs/stable/configuration-caches.html#_file_buffer_cache">http://neo4j.com/docs/stable/configuration-caches.html#_file_buffer_cache</a>).</li>
<li>Values are read as strings and must be converted before insertion.</li>
<li>You can provide defaults with the COALESCE clause.</li>
<li>Use MERGE and MATCH to add data duplicated across tables or in denormalized tables.</li>
</ul>
<p />
<p>With these main points in mind we&#8217;ll look at some examples and tricks.</p>
<p />
<h3>MERGE instead of CREATE UNIQUE</h3>
<p>In general it seems better to use MERGE rather than CREATE UNIQUE. This last clause tend to have some surprising corner cases, while MERGE on the whole is behaves for more predictably. It is important to remember though that MERGE will create any node that in a pattern if the whole pattern doesn&#8217;t match if constraints are not violated. Often that is not what you want, and instead you need to use MATCH to find existing part of a pattern and call MERGE matching just the part of the pattern you intend to create.<br />
In this example we don&#8217;t have uniqueness constraints on the user property and matching the in the MERGE would create duplicate :USER nodes.</p>
<blockquote><p>USING PERIODIC COMMIT 1000<br />
LOAD CSV WITH HEADERS FROM &#8220;file://blabla.csv&#8221; AS csvLine<br />
MATCH (uf :USER { twitter_name: toInt(csvLine.follower_name) })<br />
MATCH (u :USER { twitter_name: toInt(csvLine.twitter_name) })<br />
MERGE uf -[:FOLLOWS]-&gt; u</p></blockquote>
<h3>Skipping incomplete rows</h3>
<p>This is rather straightforward with a WHERE keyword, but is made more complicated than it needs to because it has to be attached to clause. Most straightforward way to introduce a clause is with the WITH statement, but as WITH separates parts of the query we need to parse the whole CSV line here and bind it in the clause. Accessing the CSV line object won&#8217;t work within the clause.</p>
<blockquote><p>USING PERIODIC COMMIT 1000<br />
LOAD CSV WITH HEADERS FROM &#8220;file://blabla.csv&#8221; AS csvLine<br />
WITH toInt(csvLine.twitterid) as twitterid, toInt(csvLine.seat) as seat, csvLine.firstname as firstname,<br />
csvLine.lastname as lastname, csvLine.party as party, csvLine.region as region, csvLine.type as type<br />
WHERE twitterid IS NOT NULL<br />
MERGE (u :USER { seat: seat, firstname: firstname, lastname: lastname, party: party, region: region,<br />
type: type, twitterid: twitterid })</p></blockquote>
<h3>Accumulating relationship strength</h3>
<p>In many cases the rows in a CSV file describes a list of relationships where duplicate lines describes a recurring and stronger relationship. Here we&#8217;d rather not create multiple links and have to count the link cardinality to get the strength between two nodes, but rather accumulate the repeated relationships in a property on the relationship. This is easy using the ON MATCH and ON CREATE part of a MERGE clause.</p>
<blockquote><p>USING PERIODIC COMMIT 1000<br />
LOAD CSV WITH HEADERS FROM &#8220;file://blabla.csv&#8221; AS csvLine<br />
MATCH (u :USER { twitterid: toInt(csvLine.twitterid) })<br />
MERGE (h :HASHTAG { text: csvLine.text})<br />
MERGE u -[t :TWEETED]-&gt; h<br />
ON MATCH SET t.count = t.count + 1 ON CREATE SET t.count = 1</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2015/02/04/csv-import-tricks-neo4j/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
