I like my ElasticSearch a la Node.js

1 2 Page 2
Page 2 of 2
Indexing a document
<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<code class='javascript'><span class='line'><span class="nx">beer</span> <span class="o">=</span> <span class="p">{</span> <span class="s2">"name"</span><span class="o">:</span> <span class="s2">"Todd Enders' Witbier"</span><span class="p">,</span> <span class="s2">"style"</span><span class="o">:</span> <span class="s2">"wit, Belgian ale, wheat beer"</span><span class="p">,</span> <span class="s2">"ingredients"</span><span class="o">:</span> <span class="s2">"4.0 ..."</span><span class="p">}</span>
</span><span class='line'><span class="nx">client</span><span class="p">.</span><span class="nx">index</span><span class="p">(</span><span class="nx">indexName</span><span class="p">,</span> <span class="nx">objName</span><span class="p">,</span> <span class="nx">beer</span><span class="p">).</span><span class="nx">on</span> <span class="s1">'data'</span><span class="p">,</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="o">-></span>
</span><span class='line'>  <span class="nx">data</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span> <span class="nx">data</span>
</span><span class='line'>  <span class="nx">data</span><span class="p">.</span><span class="nx">ok</span><span class="p">.</span><span class="nx">should</span><span class="p">.</span><span class="nx">equal</span> <span class="kc">true</span>
</span><span class='line'><span class="p">.</span><span class="nx">exec</span><span class="p">()</span>
</span></code>

Note in this case, I pass along an index name, and object name, and a JSON document to index. Because JSON marries so nicely with JavaScript, this library is quite nice, don’t you think?

Searching is just as easy (as I’m sure you already guessed). You create a query JSON document and pass it along to the search method like so:

Searching is just as easy!
<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<code class='javascript'><span class='line'><span class="nx">iquery</span> <span class="o">=</span> <span class="p">{</span> <span class="s2">"query"</span> <span class="o">:</span> <span class="p">{</span> <span class="s2">"term"</span> <span class="o">:</span> <span class="p">{</span> <span class="s2">"ingredients"</span> <span class="o">:</span> <span class="s2">"lemons"</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span>
</span><span class='line'><span class="nx">client</span><span class="p">.</span><span class="nx">search</span><span class="p">(</span><span class="nx">indexName</span><span class="p">,</span> <span class="nx">objName</span><span class="p">,</span> <span class="nx">iquery</span><span class="p">).</span><span class="nx">on</span> <span class="s1">'data'</span><span class="p">,</span> <span class="p">(</span><span class="nx">idata</span><span class="p">)</span> <span class="o">-></span>
</span><span class='line'>  <span class="nx">result</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span> <span class="nx">idata</span>
</span><span class='line'>  <span class="nx">result</span><span class="p">.</span><span class="nx">hits</span><span class="p">.</span><span class="nx">total</span><span class="p">.</span><span class="nx">should</span><span class="p">.</span><span class="nx">equal</span> <span class="mi">1</span>
</span><span class='line'>  <span class="nx">done</span><span class="p">()</span>
</span><span class='line'><span class="p">.</span><span class="nx">exec</span><span class="p">()</span>
</span></code>

The resultant response from the server is a JSON document that must be parsed and dealt with accordingly.

Of course, you can grab an individual document via its id just as easily:

Getting an individual document via its document id
<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<code class='javascript'><span class='line'><span class="nx">client</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">indexName</span><span class="p">,</span> <span class="nx">objName</span><span class="p">,</span> <span class="nx">doc_id</span><span class="p">).</span><span class="nx">on</span> <span class="s1">'data'</span><span class="p">,</span> <span class="p">(</span><span class="nx">getData</span><span class="p">)</span> <span class="o">-></span>
</span><span class='line'>  <span class="nx">doc</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span> <span class="nx">getData</span>
</span><span class='line'>  <span class="nx">doc</span><span class="p">.</span><span class="nx">_source</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">should</span><span class="p">.</span><span class="nx">equal</span> <span class="s2">"Todd Enders' Witbier"</span>
</span><span class='line'>  <span class="nx">done</span><span class="p">()</span>
</span><span class='line'><span class="p">.</span><span class="nx">exec</span><span class="p">()</span>
</span></code>

Node’s Elastic Search Client is a breeze to pick up; plus, the natural union of JSON and JavaScript make Node a perfect language for interfacing with ElasticSearch. Dig it?

This story, "I like my ElasticSearch a la Node.js" was originally published by JavaWorld.

Related:

Copyright © 2013 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2