While working with my recent project I need to work with Lucene indexing. I tried Google, but I was not able to find any example with Lucene API latest version (i.e. 4.10.0). So here I am giving very simple example for indexing and then searching the particular data using Lucene API. |
➟ About the Example
I am giving brief explanation here about this example. This example will retrieve some information from database using simple JDBC connection. After that it will index that data and we can search indexed data very fast using Lucene API.
➟ Pre-requirements
To run this example successfully you need to manage these requirements.
➤ Any SQL Database
You can any driver name according to database. I have used MySQL for this example. Create one database named solr_test and one table named student_details with fields id INT, name varchar(10), address varchar(50) and details varchar(500).
➤ Maven
You can see about it This is maven based project. So to run it successfully you need to configure any Java IDE with maven plugin or maven must installed externally
➟ Download demo
➟ Code Snap LceneTest.java.js package com.lucene.test; import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.core.KeywordAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.document.IntField; import org.apache.lucene.document.StringField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; /** * This is test example file to explore Lucene API * * @author vishal.zanzrukia * @version 1.0 */ public class LuceneTest { /** * this is index directory path where all index file will be stored which lucene uses internally. */ public static final File INDEX_DIRECTORY = new File("IndexDirectory"); /** * to create index on simple database table */ public void createIndex() { System.out.println("-- Indexing --"); try { /** JDBC Section */ Class.forName("com.mysql.jdbc.Driver").newInstance(); /** Assuming database solr_test exists */ Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/solr_test", "root", "P@ssw0rd@123"); Statement stmt = conn.createStatement(); String sql = "select id,name,address,details from student_details"; ResultSet rs = stmt.executeQuery(sql); /** Lucene Section */ Directory directory = FSDirectory.open(INDEX_DIRECTORY); /** defining Analyzer */ Analyzer keywordAnalyzer = new KeywordAnalyzer(); /** preparing config for indexWriter */ IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LATEST, keywordAnalyzer); /** Create a new index in the directory, removing any previously indexed documents */ writerConfig.setOpenMode(OpenMode.CREATE); /** * Optional: for better indexing performance, if you are indexing many documents, |
|
10 comments :
Nice article. worth reading and also provided easy to understand example. Thanks Vishal.
Thanks Keyur for your nice words :)
really very useful article. must read.
Thanks Neha for your nice words :)
Very helpful tutorial. Helped me set up my first lucene based search.
Thanks Nishita for your nice words :)
thanx for article
Thanks for your nice words :)
Thanks Vishal, offer easy example for me to understand lucene:-)
Thanks for your nice words :)
Post a Comment