Advertisement
Guest User

Indexer.java

a guest
Apr 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.File;
  4. import java.io.FileFilter;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  8. import org.apache.lucene.document.Document;
  9. import org.apache.lucene.document.Field;
  10. import org.apache.lucene.index.CorruptIndexException;
  11. import org.apache.lucene.index.IndexWriter;
  12. import org.apache.lucene.store.Directory;
  13. import org.apache.lucene.store.FSDirectory;
  14. import org.apache.lucene.util.Version;
  15.  
  16.  
  17. public class Indexer {
  18.     private IndexWriter writer;
  19.    
  20.     public Indexer(String indexDirectoryPath) throws IOException {
  21.         Directory indexDirectory = FSDirectory.open(new File(indexDirectoryPath));
  22.         writer = new IndexWriter(indexDirectory, new StandardAnalyzer(Version.LUCENE_36), true, IndexWriter.MaxFieldLength.UNLIMITED);
  23.     }
  24.    
  25.     public void close() throws CorruptIndexException, IOException{
  26.         writer.close();
  27.     }
  28.    
  29.     private Document getDocument(File file) throws IOException{
  30.         Document document = new Document();
  31.         Field contentField = new Field(LuceneConstants.CONTENTS, new FileReader(file));
  32.         Field fileNameField = new Field(LuceneConstants.FILE_NAME, file.getName(), Field.Store.YES,Field.Index.NOT_ANALYZED);
  33.         Field filePathField = new Field(LuceneConstants.FILE_PATH, file.getCanonicalPath(),Field.Store.YES, Field.Index.NOT_ANALYZED);
  34.        
  35.         document.add(contentField);
  36.         document.add(fileNameField);
  37.         document.add(filePathField);
  38.         return document;
  39.     }
  40.    
  41.     private void indexFile(File file) throws IOException{
  42.         System.out.println("Indeksuje " + file.getCanonicalPath());
  43.         Document document = getDocument(file);
  44.         writer.addDocument(document);
  45.     }
  46.  
  47.     public int createIndex(String dataDirPath, FileFilter filter) throws IOException {
  48.         File[] files = new File(dataDirPath).listFiles();
  49.         for(File file : files){
  50.             if(!file.isDirectory() && !file.isHidden() && file.exists() && file.canRead() && filter.accept(file)){
  51.                 indexFile(file);
  52.             }
  53.         }
  54.         return writer.numDocs();
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement