Skip to content

Instantly share code, notes, and snippets.

@UmarIqbal
Created January 17, 2014 12:41
Show Gist options
  • Save UmarIqbal/8472733 to your computer and use it in GitHub Desktop.
Save UmarIqbal/8472733 to your computer and use it in GitHub Desktop.
package searchIndexer;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class DocIndexer {
private IndexWriter writer;
public DocIndexer(String indexDir) {
// create the index
if(writer == null) {
try {
writer = new IndexWriter(FSDirectory.open(new File(indexDir)),
new IndexWriterConfig(Version.LUCENE_45 ,new StandardAnalyzer(Version.LUCENE_45)));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
/**
* This method will add the items into index
*/
public void index(DocStructure indexItem) {
try {
Document doc = new Document();
doc.add(new TextField(DocStructure.ID, indexItem.getId().toString(), Field.Store.YES));
doc.add(new TextField(DocStructure.TITLE, indexItem.getTitle(), Field.Store.YES));
doc.add(new TextField(DocStructure.CONTENT, indexItem.getContent(), Field.Store.YES));
// add the document to the index
writer.addDocument(doc);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* Closing the index
*/
public void close() {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment