Example usage for org.apache.lucene.store NIOFSDirectory close

List of usage examples for org.apache.lucene.store NIOFSDirectory close

Introduction

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

Prototype

@Override
    public synchronized void close() throws IOException 

Source Link

Usage

From source file:com.knowgate.lucene.Indexer.java

License:Open Source License

public static void add(String sTableName, String sDirectory, String sAnalyzer, Map oKeywords, Map oTexts,
        Map oUnStored) throws ClassNotFoundException, IOException, IllegalArgumentException,
        NoSuchFieldException, IllegalAccessException, InstantiationException, NullPointerException {

    if (!allowedTable(sTableName))
        throw new IllegalArgumentException("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");

    if (null == sDirectory)
        throw new NoSuchFieldException("Cannot find luceneindex property");

    File oDir = new File(sDirectory);
    if (!oDir.exists()) {
        FileSystem oFS = new FileSystem();
        try {/*from   w w w  .java 2 s  . c  o  m*/
            oFS.mkdirs(sDirectory);
        } catch (Exception e) {
            throw new IOException(e.getClass().getName() + " " + e.getMessage());
        }
    }

    Class oAnalyzer = Class.forName((sAnalyzer == null) ? DEFAULT_ANALYZER : sAnalyzer);

    NIOFSDirectory oFsDir = new NIOFSDirectory(new File(sDirectory));
    IndexWriter oIWrt = new IndexWriter(oFsDir, (Analyzer) oAnalyzer.newInstance(),
            IndexWriter.MaxFieldLength.LIMITED);

    add(oIWrt, oKeywords, oTexts, oUnStored);

    oIWrt.close();
    oFsDir.close();
}

From source file:com.knowgate.lucene.Indexer.java

License:Open Source License

/**
 * Delete a document with a given GUID//from w  ww. j av a 2 s.c om
 * @param sTableName k_bugs, k_newsmsgs or k_mime_msgs
 * @param oProps Properties Collection containing luceneindex directory
 * @param sGuid Document GUID
 * @return Number of documents deleted
 * @throws IllegalArgumentException If sTableName is not one of { k_bugs, k_newsmsgs, k_mime_msgs }
 * @throws NoSuchFieldException If luceneindex property is not found at oProps
 * @throws IllegalAccessException
 * @throws IOException
 * @throws NullPointerException If sGuid is <b>null</b>
 */
public static int delete(String sTableName, String sWorkArea, Properties oProps, String sGuid)
        throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException, IOException,
        NullPointerException {

    if (null == sGuid)
        throw new NullPointerException("Document GUID may not be null");

    if (!allowedTable(sTableName))
        throw new IllegalArgumentException("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");

    String sDirectory = oProps.getProperty("luceneindex");

    if (null == sDirectory)
        throw new NoSuchFieldException("Cannot find luceneindex property");

    sDirectory = Gadgets.chomp(sDirectory, File.separator) + sTableName.toLowerCase() + File.separator
            + sWorkArea;
    File oDir = new File(sDirectory);
    if (!oDir.exists()) {
        FileSystem oFS = new FileSystem();
        try {
            oFS.mkdirs(sDirectory);
        } catch (Exception e) {
            throw new IOException(e.getClass().getName() + " " + e.getMessage());
        }
    } // fi

    NIOFSDirectory oFsDir = new NIOFSDirectory(new File(sDirectory));
    IndexReader oReader = IndexReader.open(oFsDir);

    int iDeleted = oReader.deleteDocuments(new Term("guid", sGuid));

    oReader.close();
    oFsDir.close();

    return iDeleted;
}

From source file:de.uni_koeln.spinfo.maalr.lucene.core.DictionaryLoader.java

License:Apache License

private synchronized void loadIndex() throws NoIndexAvailableException {
    if (searcher == null) {
        try {/*from w  w  w .j  a v  a2s.  co  m*/
            logger.info("Loading index from directory " + environment.getLuceneIndexDir().getAbsolutePath());
            NIOFSDirectory directory = new NIOFSDirectory(environment.getLuceneIndexDir());
            ram = new RAMDirectory(directory, new IOContext());
            reader = DirectoryReader.open(ram);
            searcher = new IndexSearcher(reader);
            searcher.setSimilarity(new SimilarityBase() {

                @Override
                public String toString() {
                    return "Constant Similarity";
                }

                @Override
                protected float score(BasicStats stats, float freq, float docLen) {
                    return stats.getTotalBoost();
                }

            });
            directory.close();
            logger.info("Index loaded.");
        } catch (IOException e) {
            throw new NoIndexAvailableException("Failed to load index", e);
        }
    }
}