Hi All,
Now days I was working on Lucene a Java API that offers you search capability for your application.
Lucene is a powerful search library that lets you easily add search to any application. One of the key
factors behind Lucene’s popularity is its simplicity, but don’t let that fool you under the hood there are
sophisticated, state of the art Information Retrieval techniques quietly at work.
Current version available is 2.3.2.
The book i am referring for Lucene is Manning series "Lucene in Action ", but problem with this book is , this is handling Lucene 1.4 version that is entirely different from the latest one. There are many new syntax changes because of that you will no be able to run this books example with 2.3 Version.
I have modified its basic Indexer and Searcher example to run with latest version and posting here for your reference.
Indexer.java
This will create Index of directory provided by the user
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Date; /** // open an index and start file directory traversal if (!dataDir.exists() || !dataDir.isDirectory()) IndexWriter writer = int numIndexed = writer.docCount(); return numIndexed; /** if (args.length != 2) File indexDir = new File(args[0]); // recursive method that calls itself when it finds a directory File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) File f = files[i]; if (f.isDirectory()) // method to actually index a file using Lucene if (f.isHidden() || !f.exists() || !f.canRead()) return; System.out.println("Indexing " + f.getCanonicalPath()); Document doc = new Document(); //doc.add(Field.Text("contents", new FileReader(f))); |
Searcher.java –
Using Searcher you can perform search on created Index by Indexer.
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import java.io.File; import java.util.Date; /** /** if (args.length != 2) File indexDir = new File(args[0]); if (!indexDir.exists() || !indexDir.isDirectory()) search(indexDir, q); /** Directory fsDir = FSDirectory.getDirectory(indexDir, false); QueryParser qp = new QueryParser("contents", new StandardAnalyzer()); long start = new Date().getTime(); for (int i = 0; i < hits.length(); i++) Document doc = hits.doc(i); |
I Hope this information and examples will help you.
Do post your comments ;).
Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.
hello,
how can i execute this program?
how can i input the directory of index and the directory of datei to indexed in programm?
thank you
See the main program I am using arguments to get the input parameters
File indexDir = new File(args[0]);
File dataDir = new File(args[1]);
Hi,
It’s posible to add only one document to an existing index ? incrementally ?
Regards,
Marcello