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
1 VAGRANTFILE_API_VERSION = "2" 2 3 Vagrant::configure(VAGRANTFILE_API_VERSION) do |config| 4 5 # 32-bit Ubuntu Box 6 config.vm.box = "ubuntu/trusty64" 7 config.vm.network "forwarded_port", guest: 9200, host: 9200 8 end
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
01 #!/usr/local/bin/perl -w 02 use strict; 03 use Search::Elasticsearch; 04 use File::Find; 05 use Sysadm::Install qw( slurp ); 06 use Cwd; 07 use File::Basename; 08 09 my $idx = "blog"; 10 my $base_dir = getcwd; 11 my $base = $base_dir . "/idx"; 12 13 my $es = Search::Elasticsearch->new( ); 14 eval { 15 $es->indices->delete( index => $idx ) }; 16 17 find sub { 18 my $file = $_; 19 20 return if ! -f $file; 21 my $content = slurp $file, { utf8 => 1 }; 22 23 $es->index( 24 index => $idx, 25 type => 'text', 26 body => { 27 content => $content, 28 file => $file, 29 } 30 ); 31 print "Added $file\n"; 32 }, $base;
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
01 #!/usr/local/bin/perl -w 02 use strict; 03 use Search::Elasticsearch; 04 use Sysadm::Install qw( slurp ) ; 05 06 my $idx = "blog"; 07 08 my( $doc ) = @ARGV; 09 die "usage: $0 doc" if !defined $doc; 10 11 my $es = Search::Elasticsearch->new( ); 12 13 my $results = $es->search( 14 index => $idx, 15 body => { 16 query => { 17 more_like_this => { 18 like_text => 19 slurp( $doc, { utf8 => 1 } ), 20 min_term_freq => 5, 21 max_query_terms => 20, 22 } 23 } 24 } 25 ); 26 27 for my $result ( 28 @{ $results->{ hits }->{ hits } } ) { 29 30 print $result->{ _source }->{ file }, 31 "\n"; 32 }
![](/var/linux_magazin/storage/images/issues/2016/182/perl-elasticsearch/figure-3/662757-1-eng-US/Figure-3_large.png)
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
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.