apache lucy search examples

Investigating search engines and this time apache Lucy 0.4.2. I am showing a basic indexer and a small search application. See below code for indexer (This will take documents one by one and then index them). Search module will take arugument as STDIN and then will show the search result.

This is pure command line utility just to show how basic indexing and searching works using apache lucy.



use strict;
use warnings;
use Lucy::Simple;

# Ensure the index directory is both available and empty.
my $index = "/ppant/LucyTest/index";
system( "rm", "-rf", $index );
system( "mkdir", "-p", $index );
# Create the helper...a new Lucy::Simple object
my $lucy = Lucy::Simple new( path = $index, language = 'en', );

# Add the first "document". (We are mainly adding meta data of the document)
my %one = ( title ="This is a title of first article" , body ="some text inside the body we need to test the implementaion of lucy", id =1 );
$lucy-add_doc( \%one );

# Add the second "document".
my %two = ( title ="This is another article" , body ="I am putting some basic content, using some words which are also in first document like implementation", id =2 );
$lucy add_doc( \%two );

# Both the documents are now indexed in path

One indexing of the documents is done we’ll make a small search script.



use strict;
use warnings;

use Lucy::Search::IndexSearcher;

my $term = shift || die "Usage: $0 search-term";

my $searcher = Lucy::Search::IndexSearcher new( index ='/ppant/LucyTest/index');
# A basic search command line which will look for indexed items based on STDIN and will show that in which document query string is found and no of hits
my $hits = $searcher hits( query =$term );
while ( my $hit = $hits next ) {
print "Title: $hit {title} - ID: $hit {id}\n";
# End of search.cgi


If you want to explore more check Full Code on GitHub

Implementing mapper attachment in elasticsearch

Create a new index

curl -X PUT "" -d '{
"settings" : { "index" : { "number_of_shards" : 1, "number_of_replicas" : 0 }}

Mapping attachement type

curl -X PUT "" -d '{
"attachment" : {
"properties" : {
"file" : {
"type" : "attachment",
"fields" : {
"title" : { "store" : "yes" },
"file" : { "term_vector":"with_positions_offsets", "store":"yes" }
} } } } }'

Shell script to convert content to base64 encoding and index


coded=`cat TestPDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`
echo "$json" &gt; json.file
curl -X POST "" -d @json.file

Query  (Search esults will be highlighted)

curl "" -d '{
"fields" : ["title"],
"query" : {
"query_string" : {
"query" : "Cycling tips"
"highlight" : {
"fields" : {
"file" : {}
} } }'


If you want to explore more check Full Code on GitHub

RESTfull brief overview

Putting some thoughts mainly for newbies trying to understand REST. Sometimes I observed that the actual documentation on the web is too technical or abstract and scattered which a bit difficult for newbies..  I am writing down some broad points which in my view REST is and it’s equation with HTTP … these points based on my reading and experience working with REST. It doesn’t replace any of REST documentation. Advised to go through references given at the end for detaled study.
  • REST  REpresentational State Transfer is a design pattern/design concept (architecture) used in web applications for managing state information-> REST is not a tool/techology/specification/protocol. In other words, REST isn’t a tangible thing like a piece of software or even a specification, it’s a selection of ideals, of best practices distilled from the HTTP specs.
  •  If you are using HTTP you are being RESTful to some degree since HTTP is a REST protocol, but to take full advantage of the platform, APIs should use RESTful practices as much as possible.
  • We can make our application more RESTful by using the correct HTTP methods.
  • RESTful Web service is required to be stateless in it’s communication between server and client  so you should be able to request almost any format while non-REST mainly SOAP uses XML.
  • URL is the important part of REST. REST is more than GET/POST actually browsers pretty much just GET stuff. They don’t do a lot of other types of interaction with resources. This is a problem because it has led many people to assume that HTTP is just for GETing. But HTTP is actually ageneral purpose protocol for applying http verbs (HTTP verbs (GET, POST, PUT, DELETE, etc.) to nouns (object/web page which has a URL).

Recommended reading:

Paper by Roy Fielding http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

HTTP Specs http://www.w3.org/Protocols/Specs.html

How I Explained REST to My Wife:

Happy reading!

Web development: LAMP: which programming languages should be used: Some thoughts

Now a days people keep asking which technology stack to be used for web development (LAMP, Java, Microsoft) and finally which programming language mainly server-side. Most of the expert says that use whichever you like and comfortable and I totally agree. If you intend to use Java and Microsoft based env then you don’t have much choice but if you are using LAMP stack then you have a lot of options so question again arises which language should be used? Again, I personally think that decision should mainly on based on the requirement, experience, comfort, team etc. Still here is my take based on my little own experiences working with languages:

Pros: Old fellow still widely used, Very powerful, secure, well tested over the years in web dev, very good market repo among users, huge collection of open source libraries, new framework like Dancer, Mojolicious are positive sign.
Cons: Difficult to maintain (dirty syntax etc), Hard to get resources, industry is not very positive about its future versions.

Pros: Powerful, widely used in handling scientific data, academics, analytics, system administrators, Market sentiment is positive, Very good framework like Django.
Cons: Less flexible, performance issues mainly threading.

Pros: Most preferred language, widely used, fast development, big community, huge available resource pool.
Cons: Some reported security loopholes, Less trustworthy, Market image as cheap and dirty option for quick development, multi-threading issue, debugging issues.

Pros: Very flexible, good support, positive image in communities, Very popular framework for web development (ROR).
Cons: Some benchmarks shows that its request-response time is a bit slow than others in same category, Getting good resources can be difficult.

Again few things differ project to project so choose based on your own requirement.

I personally prefer Perl 5.

Import RDBMS table to HDFS with sqoop from postgreSQL


1. Download JDBC driver


$wget http://jdbc.postgresql.org/download/postgresql-9.3-1102.jdbc4.jar


 2. Copy: 

cp /home/cloudera/Desktop/postgresql-9.3-1102.jdbc4.jar /usr/lib/sqoop/lib/ 


3. Configure: 


file. You need to allow the IP/host of machine running Hadoop.

Restart postgreSQL using 

$pg_ctl restart


4. Run sqoop: Open the terminal on machine running hadoop and type the below command.


 cloudera@cloudera-vm:/usr/lib/sqoop bin/sqoop import --connect jdbc:postgresql:// employee --username postgres -P --target-dir /sqoopOut1 -m 1 


Enter password:



  • Cloudera hadoop VM distribution or any other machine running hadoop.
  • postgreSQL installation.
  • database Testdb and employee table on a running instance of postgreSQL (e.g.; in point 4).


All set! Your pgsql table data is now available on HDFS of  VM hadoop cluster.


Enjoy hadoop learning!

Citrus Perl Raspberry Pi dev

Anyone interested in GUI Perl dev in Pi? Please go through the link here and download the distribution from sourceforge project site.

I am using Citrus Perl on Pi (Raspbian Wheezy OS) for quite some time ..no major issue.


Enjoy GUI dev on Pi .