lucene

Create Lucene Index

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: November 11th, 2012
0 95 1 minute read
package com.javacodegeeks.lucene; import java.io.File;import java.io.FileReader;import java.io.IOException; import org.apache.lucene.analysis.SimpleAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.store.FSDirectory; public class SimpleFileIndexer {        public static void main(String[] args) throws Exception {    File indexDir = new File("C:/index/");  File dataDir = new File("C:/programs/eclipse/workspace/");  String suffix = "java";    SimpleFileIndexer indexer = new SimpleFileIndexer();    int numIndex = indexer.index(indexDir, dataDir, suffix);    System.out.println("Total files indexed " + numIndex);      }        private int index(File indexDir, File dataDir, String suffix) throws Exception {    IndexWriter indexWriter = new IndexWriter(    FSDirectory.open(indexDir),     new SimpleAnalyzer(),    true,    IndexWriter.MaxFieldLength.LIMITED);  indexWriter.setUseCompoundFile(false);    indexDirectory(indexWriter, dataDir, suffix);    int numIndexed = indexWriter.maxDoc();  indexWriter.optimize();  indexWriter.close();    return numIndexed;      }        private void indexDirectory(IndexWriter indexWriter, File dataDir,      String suffix) throws IOException {   File[] files = dataDir.listFiles();  for (int i = 0; i < files.length; i++) {File f = files[i];if (f.isDirectory()) {    indexDirectory(indexWriter, f, suffix);}else {    indexFileWithIndexWriter(indexWriter, f, suffix);}  }     }        private void indexFileWithIndexWriter(IndexWriter indexWriter, File f, String suffix) throws IOException {   if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {return;  }  if (suffix!=null && !f.getName().endsWith(suffix)) {return;  }  System.out.println("Indexing file " + f.getCanonicalPath());    Document doc = new Document();  doc.add(new Field("contents", new FileReader(f)));    doc.add(new Field("filename", f.getCanonicalPath(),      Field.Store.YES, Field.Index.ANALYZED));    indexWriter.addDocument(doc);     } }

Related Article:

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: November 11th, 2012
0 95 1 minute read
Photo of Byron Kiourtzoglou

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.