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.

9 comments:

  1. Hi
    I'm geting many errors in the class after the copy-paste of code. Why is that?
    If i want to index and search external files?
    Thank you.

    ReplyDelete
  2. The quotation marks shouldn't have the \ character preceding them. E.g., this is correct
    addDoc(w, "Lucene in Action", "193398817");

    ReplyDelete
  3. It was so very helpful. Thank you Sir.

    ReplyDelete
  4. I have an error on "LUCENE_44".. I have the latest version of Lucene 6.3.0... I need to change this?

    ReplyDelete
    Replies
    1. You just change LUCENE_44 to LUCENE_6_3_0 or follow the suggestion from your IDE to update the command

      Delete
    2. i have wriiten LUCENE_6_4_0 still i m getting error??

      Delete