Created
January 17, 2014 12:41
-
-
Save UmarIqbal/8472733 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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