Wednesday 18 September 2013

Tutorial - Working with lucene and eclipse



Aim: This tutorial enables a user to get started with implementing apache lucene with the help of eclipse IDE and Java.


Tools required:

Apache Lucene: Lucene is a full text search library written in java. Lucene allows users to embed search functionality into any application. Read more about lucene at their official website. You can get an idea of the basic concepts in lucene by visiting this website.

Procedure: So, what are we waiting for? Let’s start to use lucene with eclipse…


Step 1: Open eclipse IDE




Step 2: Create a java project (File -> New -> Java Project)




Step 3: Give a name for the project. I have given “LuceneDemo” as the project name. Then click on “Finish”.



Now your eclipse IDE would look something like shown below:





Step 4: Right click on the project (LuceneDemo) and select “Properties” as shown below:



 Step 5: Click on “Java Build Path” on the left side and select “Add External JARs…” button on the right as shown below:





Step 6: This step is very important. In this step, we will be adding 4 JAR files to our project (LuceneDemo). Previously you have extracted lucene on to your desktop. The folder name will be lucene-4.4.0. Below I will be listing the paths to 4 JAR files that we will be adding to our project:

C:\Users\User\Desktop\lucene-4.4.0\queryparser\lucene-queryparser-4.4.0.jar
C:\Users\User\Desktop\lucene-4.4.0\analysis\common\lucene-analyzers-common-4.4.0.jar
C:\Users\User\Desktop\lucene-4.4.0\core\lucene-core-4.4.0.jar
C:\Users\User\Desktop\lucene-4.4.0\demo\lucene-demo-4.4.0.jar

My lucene folder (lucene-4.4.0) is stored at location C:\Users\User\Desktop. This path might vary on your systems.

After selecting and adding the 4 JAR files, the eclipse “Java Build Path” dialog box will look as shown below:

 After adding the JAR files, click on the “OK” button as shown in the above figure. 



Step 7: Again right click on the project and select “New” and select “Class” as shown below:


 Step 8: Provide a name for your class. I provided the name as “LuceneTest”. We will write our java code in this file and run it. Refer the below picture to know where to write the class name. Click on “Finish” once you are done.





Step 9: Copy/Paste the code from this link. You should have prior knowledge of core java to understand the code. The code is commented at appropriate places so that you can understand what is going on in the code.



Step 10: Click on “Run” button as shown below:






Output: The keyword I was searching in the code is “teja”. You can see the output in the console window as shown below:



Final note: I hope this tutorial helped you to start up with apache lucene using eclipse. Things may not go perfect for you. You might face some errors while performing the steps in this tutorial. You can feel free to contact me via email if you have any errors or you can comment here in this article.