Smart research using Elasticsearch
Structure of the Index
Invoking mlt-index
(Listing 3) from the command line grabs the files one by one and feeds them via the index()
method to the Elasticsearch server installed on my desktop computer. (See "Installing the Elasticsearch Server" box.)
Installing the Elasticsearch Server
The Elasticsearch server can be easily installed on a desktop computer or a virtual machine using the following simple steps:
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-installerjava -version wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.1.debsudo dpkg -i *.deb sudo /etc/init.d/elasticsearch start
If you would rather not clutter your filesystem with yet more software, you can simply boot a VM using vagrant
up
with the Vagrant file shown in Listing 2, and install the server in the VM. The forwarded_port
mapping ensures that the Elasticsearch server in the VM listing on port 9200 is also responsive on the host system on port 9200.
Listing 2
Vagrant File
Looking at Listing 3, I simply called the newly created index in the server blog
. Line 15 deletes it first of all if it is already there – which is typically after previously calling mlt-index
.
Listing 3
mlt-index
The find()
function in line 17, originating from the File::Find module accompanying Perl, then recursively works through all the files under the specified directory and, behind the scenes, switches to the directory in which they're located during the user-defined callback using chdir
. It also sets the name of the file currently being edited in the variable $_
.
Feeding by Slurping
The index()
method in line 23 adds the file's text content slurped with slurp()
from the Sysadm::Install CPAN module's treasure trove, along with the file name to the search engine's index. Later, the search functions return the file names with matches, and they are easy to recognize because the names were chosen according to the content of each blog entry (e.g., 10-cents-for-a-grocery-bag.txt
).
The index()
feed function also creates a new index (if it doesn't already exist) and defines both the item's name (blog
) and a type
(set to text
) – which in Elasticsearch is nothing more than an arbitrarily named partition in the index.
More of That!
Feeding data to Elasticsearch took about a tenth of a second per 2KB text file on my Linux desktop; this therefore really tested my patience with the 877 blog entries. By contrast, feeding data on my laptop with a faster solid state disk and plenty of memory was much faster – it was all over within 10 seconds.
Queries can be submitted as soon as the index is ready. The output from the mlt-search
command in Figure 3 shows that Listing 4 unsurprisingly rediscovers the document I provided as a reference – content related to my family dealing earthquakes in the San Francisco Bay area. But, it also dug up some other earth-shaking results, such as a report about how to traverse the bureaucratic jungle to obtain a driver's license in California, about how to distinguish good and bad neighborhoods to live in, and the annoying habit of some motorcycle riders to let their engines roar at earth-shattering levels. The results appear in fractions of a second, meaning the function is certainly also useful on busy websites.
Listing 4
mlt-search

Listing 4 expects a text file's path at the command line and imports this file using slurp
, while telling Perl to keep the utf8
encoding intact. It sends the query to the Elasticsearch server in line 13 using the search()
method and then collects the names of the files from the matches with comparable content from the result returned as JSON.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News
-
The First Point Release For Ubuntu 22.04 is Now Available
Canonical has released the first point upgrade for Jammy Jellyfish which includes important new toolchains and fixes.
-
Kali Linux 2022.3 Released
From the creators of the most popular penetration testing distributions on the planet, comes a new release with some new tools and a community, real-time chat option.
-
The 14" Pinebook Pro Linux Laptop is Shipping
After a considerable delay, the 14" version of the Pinebook Pro laptop is, once again, available for purchase.
-
OpenMandriva Lx ROME Technical Preview Released
OpenMandriva’s rolling release distribution technical preview has been released for testing purposes and adds some of the latest/greatest software into the mix.
-
Linux Mint 21 is Now Available
The latest iteration of Linux Mint, codenamed Vanessa, has been released with a new upgrade tool and other fantastic features.
-
Firefox Adds Long-Anticipated Feature
Firefox 103 has arrived and it now includes a feature users have long awaited…sort of.
-
System76 Refreshes Their Popular Oryx Pro Laptop with a New CPU
The System76 Oryx Pro laptop has been relaunched with a 12th Gen CPU and more powerful graphics options.
-
Elive Has Released a New Beta
The Elive team is proud to announce the latest beta version (3.8.30) of its Enlightenment-centric Linux distribution.
-
Rocky Linux 9 Has Arrived
The latest iteration of Rocky Linux is now available and includes a host of new features and support for new architecture.
-
Slimbook Executive Linux Ultrabook Upgrading Their CPUs
The Spanish-based company, Slimbook, has made available their next generation Slimbook Executive Linux ultrabooks with a 12th Gen Intel Alder Lake CPU.