Example usage for org.apache.lucene.store SimpleFSDirectory SimpleFSDirectory

List of usage examples for org.apache.lucene.store SimpleFSDirectory SimpleFSDirectory

Introduction

In this page you can find the example usage for org.apache.lucene.store SimpleFSDirectory SimpleFSDirectory.

Prototype

public SimpleFSDirectory(Path path) throws IOException 

Source Link

Document

Create a new SimpleFSDirectory for the named location and FSLockFactory#getDefault() .

Usage

From source file:application.ReviewDocumentIndexer.java

License:Open Source License

/**
 * @param args/*  w  w  w  .j a  va2s .com*/
 */
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    // Parse command line arguments. Exit program is provided arguments are insufficient
    ReviewDocumentIndexer indexer = new ReviewDocumentIndexer(args);
    if (indexer == null)
        return;

    // Open a new index
    IndexWriter index = null;
    try {
        index = new IndexWriter(new SimpleFSDirectory(new File(Paths.luceneIndex)),
                new ReviewTextAnalyzer(indexer), indexer.new_index ? true : false, MaxFieldLength.UNLIMITED);
        if (indexer.pause_every > 2) {
            index.setMaxBufferedDocs(indexer.pause_every);
        }
        index.setMaxMergeDocs(Config.maxMergeDocs);
        index.setMergeFactor(Config.mergeFactor);
    } catch (CorruptIndexException e) {
        AppLogger.error.log(Level.SEVERE,
                "Lucene detected an inconsistency upon opening the index located at " + Paths.luceneIndex);
        throw new RuntimeException("Exiting application", e);
    } catch (LockObtainFailedException e) {
        AppLogger.error.log(Level.SEVERE,
                "Index located at " + Paths.luceneIndex + " is already open by another Lucene process");
        throw new RuntimeException("Exiting application", e);
    } catch (IOException e) {
        AppLogger.error.log(Level.SEVERE, "Could not access location " + Paths.luceneIndex);
        throw new RuntimeException("Exiting application", e);
    }

    // Load a number of reviews from database
    NumberFormat docIdFormat = TokenListsCollector.defaultDocIdFormat();
    try {
        DatabaseReviewCollection reviews = new DatabaseReviewCollection(indexer.pause_every);
        reviews.setLimits(indexer.min_reviewid, indexer.stop_after);
        int indexed_counter = 0;

        while (reviews.hasNextSegment()) {

            System.out.print(Calendar.getInstance().getTime().toGMTString());

            System.out.print(" Loading from DB... ");
            reviews.loadNextSegment();
            Iterator<Review> reviewsIterator = reviews.getIterator();

            System.out.print(" Indexing... ");
            while (reviewsIterator.hasNext()) {
                DatabaseReview dbr = (DatabaseReview) reviewsIterator.next();
                int dbr_id = dbr.getReviewid();
                int dbr_rating = dbr.getRating();

                try {
                    indexer.theReviewId.set(dbr_id);
                    indexer.theStats.setCurrent(dbr_id, dbr_rating);

                    index.addDocument(dbr.getDocumentForIndexing());
                    indexed_counter++;

                    // Also, keep track of the rating and length of this review
                    indexer.theStats.storeCurrent();

                } catch (CorruptIndexException e) {
                    AppLogger.error.log(Level.SEVERE,
                            "Lucene detected an inconsistency upon saving review #"
                                    + Integer.toString(dbr.getReviewid()) + "to the index located at "
                                    + Paths.luceneIndex);
                    return;
                } catch (IOException e) {
                    AppLogger.error.log(Level.WARNING,
                            "Review #" + Integer.toString(dbr.getReviewid()) + " could not be indexed");
                }
            }

            // Backup everything
            System.out.print("Indexed " + indexed_counter + " reviews total. ");
            if (indexer.pause_every > 0) {
                System.out.print("Saving tokenlists... ");
                indexer.theTokenLists.writeNextFile(docIdFormat);

                System.out.print("Saving state... ");
                try {
                    index.commit();
                    indexer.saveState();
                } catch (CorruptIndexException e) {
                    AppLogger.error.log(Level.SEVERE, "Committing index changes failed on review #"
                            + indexer.theReviewId.get() + "due to CorruptIndexException");
                    return;
                } catch (IOException e) {
                    AppLogger.error.log(Level.WARNING, "Committing index changes failed on review #"
                            + indexer.theReviewId.get() + "due to IOException");
                }
            }
            System.out.print("DONE\n");

            reviews.reset();
        }
    } catch (SQLException e) {
        AppLogger.error.log(Level.SEVERE,
                "An exception occured while trying to access the database.\n" + e.getMessage());
        return;
    }

    try {
        index.close();
        indexer.backupIndex();
    } catch (CorruptIndexException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.err.println("Indexing successfully completed!");
    return;
}

From source file:application.SentimentLexiconGenerator.java

License:Open Source License

/**
 * Constructor for class SentimentLexiconGenerator
 *///from  w  w w  .j  av  a  2s  .co m
public SentimentLexiconGenerator() {
    // Load term index for reading
    try {
        reader = IndexReader.open(new SimpleFSDirectory(new File(Paths.luceneIndex)), true);
        searcher = new IndexSearcher(reader);
    } catch (IOException e) {
        AppLogger.error.log(Level.SEVERE, "Error reading index");
    }

    // Load index metadata
    try {
        reviewStats = new State<ReviewStats>("stats", null).restoreState();
        indexedSynsets = new State<SynsetTermsAggregator>("synsets", null).restoreState();
    } catch (IOException e1) {
        AppLogger.error.log(Level.SEVERE, "Error reading index metadata\n" + e1.getMessage());
    }

    // Prepare files that collected discarded terms from the index
    try {
        discardedSynsetsFile = new FileWriter(Paths.lexiconDiscardedPath + "synsets.txt", false);
        discardedTermsFile = new FileWriter(Paths.lexiconDiscardedPath + "terms.txt", false);
        discardedOtherFile = new FileWriter(Paths.lexiconDiscardedPath + "other.txt", false);
    } catch (IOException e) {
        AppLogger.error.log(Level.SEVERE, "Error initializing discarded terms files");
    }

    // Initialize filter to default state;
    filter = PayloadFilters.FILTER_NONE;

}

From source file:br.bireme.mlts.MoreLikeThat.java

License:Open Source License

/**
 * Constructor of the class//from   w w w. j a v  a 2 s .c  om
 * @param dir directory of the Lucene index
 * @param analyz  analyzer used during search and similar
 * @throws IOException
 */
public MoreLikeThat(final File dir, final Analyzer analyz) throws IOException {
    if (dir == null) {
        throw new NullPointerException("dir");
    }
    if (analyz == null) {
        throw new NullPointerException("analyz");
    }

    if (!dir.isDirectory()) {
        throw new IllegalArgumentException(dir.getCanonicalPath() + " is not a directory");
    }
    directory = new SimpleFSDirectory(dir); /* RAMDirectory(new SimpleFSDirectory(dir)); //MMapDirectory(dir);*/
    ir = IndexReader.open(directory);
    if (ir.numDocs() == 0) {
        throw new IllegalArgumentException("zero document index");
    }
    is = new IndexSearcher(ir);
    analyzer = analyz;
    stopwords = (analyzer instanceof StopwordAnalyzerBase) ? ((StopwordAnalyzerBase) analyzer).getStopwordSet()
            : null;
    defaultValues();
}

From source file:br.bireme.prvtrm.PreviousTerm.java

License:Open Source License

/**
 * Construtor da classe//from  w  w  w. j  a v a  2  s  .  co m
 * @param dir caminho para o diretorio onde esta o indice do Lucene
 * @param fields indica a qual campos os termos previos pretencem
 * @param maxSize numero de termos previos a serem retornados
 * @throws IOException
 */
public PreviousTerm(final File dir, final List<String> fields, final int maxSize) throws IOException {
    if (dir == null) {
        throw new NullPointerException("dir");
    }
    if (fields == null) {
        throw new NullPointerException("fields");
    }
    if (maxSize <= 0) {
        throw new IllegalArgumentException("maxSize <= 0");
    }

    final Directory sdir = new SimpleFSDirectory(dir);
    reader = IndexReader.open(sdir);
    this.fields = fields;
    this.maxSize = maxSize;
}

From source file:br.ufmt.harmonizacao.implementer.PatenteeSearcher.java

public void setPath(String path) {
    try {/* w  w  w .j  a  v  a  2  s. c o m*/
        this.path = path + dirName;
        System.out.println(this.path);
        dir = new SimpleFSDirectory(new File(this.path));
        reader = DirectoryReader.open(dir);
        searcher = new IndexSearcher(reader);
    } catch (IOException ex) {
        Logger.getLogger(PatenteeSearcher.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:br.ufmt.harmonizacao.implementer.StandardSearcher.java

public void setPath(String path) {
    try {/*w w w  .j  a  va 2  s.  c  o  m*/
        this.path = path + dirName;
        System.out.println(this.path);
        dir = new SimpleFSDirectory(new File(this.path));
        reader = DirectoryReader.open(dir);
        searcher = new IndexSearcher(reader);
    } catch (IOException ex) {
        Logger.getLogger(PatenteeSearcher.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:ca.pgon.freenetknowledge.search.impl.LuceneSearchEngine.java

License:Apache License

@PostConstruct
public void init() throws IOException {
    directory = new SimpleFSDirectory(new File(indexDirectory));
    analyzer = new StandardAnalyzer(LuceneSearchEngine.LUCENE_VERSION);
    luceneIndexerThread = new LuceneIndexerThread(maxAddInIndexBeforeComputing, directory, analyzer);
    luceneIndexerThread.start();//from   ww w  .  j  av a  2  s.co m
}

From source file:cn.hbu.cs.esearch.index.DefaultDirectoryManager.java

License:Apache License

@Override
public Directory getDirectory(boolean create) throws IOException {
    if (!directory.exists() && create) {
        // create the parent directory
        directory.mkdirs();/*from  w  w  w  .  j  a v a2 s .  co m*/
    }

    if (create) {
        IndexSignature sig = null;
        if (directory.exists()) {
            sig = getCurrentIndexSignature();
        }

        if (sig == null) {
            File directoryFile = new File(directory, INDEX_DIRECTORY);
            sig = new IndexSignature(null);
            try {
                saveSignature(sig, directoryFile);
            } catch (IOException e) {
                throw e;
            }
        }
    }

    FSDirectory dir = null;
    switch (mode) {
    case SIMPLE:
        dir = new SimpleFSDirectory(directory);
        break;
    case NIO:
        dir = new NIOFSDirectory(directory);
        break;
    case MMAP:
        dir = new MMapDirectory(directory);
        break;
    }
    LOGGER.info("created Directory: " + dir);
    return dir;
}

From source file:cn.hbu.cs.esearch.index.DefaultRAMDiskIndexFactory.java

License:Apache License

@Override
public synchronized RAMSearchIndex<R> newInstance(String version, IndexReaderDecorator<R> decorator,
        SearchIndexManager<R> idxMgr) {
    Directory ramIdxDir;//w w w  .  j a  v  a2  s .  co  m
    try {
        File backingdir = new File("/tmp/ram" + fold);// /Volumes/ramdisk/
        ramIdxDir = new SimpleFSDirectory(backingdir);
        fold++;
        return new RAMSearchIndex<R>(version, decorator, idxMgr, ramIdxDir, backingdir);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        LOGGER.error("{}", e);
        e.printStackTrace();
    } // new RAMDirectory();
    return null;
}

From source file:com.aurel.track.lucene.LuceneUtil.java

License:Open Source License

/**
 * Gets the directory for an index based on the index number
 * @param index//ww w .ja v  a 2s .c o m
 * @return
 */
public static Directory getIndexDirectory(int index) {
    String indexDir = getDirectoryString(index);
    String filePath = stripTrailingPathSeparator(LuceneUtil.getPath());
    if (filePath == null) {
        filePath = "";
    }
    //filePath = filePath.replace('\\', '/');
    String indexPath;
    if ("".equals(filePath.trim())) {
        //relative path
        indexPath = getContexPath() + "/" + LuceneUtil.LUCENEMAINDIR + "/" + indexDir;
    } else {
        //absolute path
        indexPath = filePath + "/" + LuceneUtil.LUCENEMAINDIR + "/" + indexDir;
    }
    File directoryFile = new File(indexPath);
    boolean created;
    //if not exists create a file
    if (!directoryFile.exists()) {
        created = directoryFile.mkdirs();
        if (!created) {
            LOGGER.error("Can't create the directory " + filePath);
        }
    } else {
        //if exists with this name but is not a directory
        //the file should be deleted manually
        if (!directoryFile.isDirectory()) {
            LOGGER.error("Can't create the directory because a file with this name exists " + filePath);
        }
    }
    Directory directory = null;
    try {
        directory = new SimpleFSDirectory(directoryFile.toPath());
    } catch (IOException e) {
        LOGGER.error("Can't create a SimpleFSDirectory because a file with this name exists " + filePath);
    }
    return directory;
}