Example usage for org.apache.lucene.search.spell SpellChecker close

List of usage examples for org.apache.lucene.search.spell SpellChecker close

Introduction

In this page you can find the example usage for org.apache.lucene.search.spell SpellChecker close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Close the IndexSearcher used by this SpellChecker

Usage

From source file:com.ikon.module.db.stuff.IndexHelper.java

License:Open Source License

public void updateSpellCheckerIndex(NodeDocumentVersion nDocVer) {
    log.info("Observed Wine added/updated event for {1} from Thread {0}", Thread.currentThread().getName(),
            String.valueOf(nDocVer));
    String text = (nDocVer != null) ? nDocVer.getText() : null;

    if (text != null) {
        Dictionary dictionary = null;

        try {//from   w w w  .j ava  2s.  c o m
            FullTextEntityManager ftEm = (FullTextEntityManager) entityManager;
            SearchFactory searchFactory = ftEm.getSearchFactory();
            dictionary = new SetDictionary(text, searchFactory.getAnalyzer("wine_en"));
        } catch (IOException ioExc) {
            log.error("Failed to analyze dictionary text {0} from Wine {1} to update spell checker due to: {2}"
                    + text + nDocVer.getUuid() + ioExc.toString());
        }

        if (dictionary != null) {
            Directory dir = null;
            // only allow one thread to update the index at a time ...
            // the Dictionary is pre-computed, so it should happen quickly
            // ...
            // this synchronized approach only works because this component
            // is application-scoped
            synchronized (this) {
                try {
                    dir = FSDirectory.open(new File("lucene_index/spellcheck"));
                    SpellChecker spell = new SpellChecker(dir);
                    spell.indexDictionary(dictionary);
                    spell.close();
                    log.info("Successfully updated the spell checker index after Document added/updated.");
                } catch (Exception exc) {
                    log.error("Failed to update the spell checker index!", exc);
                } finally {
                    if (dir != null) {
                        try {
                            dir.close();
                        } catch (Exception zzz) {
                        }
                    }
                }
            }
        }
    }
}

From source file:com.ikon.module.db.stuff.IndexHelper.java

License:Open Source License

protected void buildSpellCheckerIndex(SearchFactory searchFactory) {
    IndexReader reader = null;/*from w  w w. j a v a 2 s.  co  m*/
    Directory dir = null;
    long _entr = System.currentTimeMillis();
    File spellCheckIndexDir = new File("lucene_index/spellcheck");
    log.info("Building SpellChecker index in {0}", spellCheckIndexDir.getAbsolutePath());
    ReaderProvider readerProvider = searchFactory.getReaderProvider();

    try {
        reader = readerProvider.openReader(searchFactory.getDirectoryProviders(NodeDocumentVersion.class)[0]);
        dir = FSDirectory.open(spellCheckIndexDir);
        SpellChecker spell = new SpellChecker(dir);
        spell.clearIndex();
        spell.indexDictionary(new LuceneDictionary(reader, NodeDocument.TEXT_FIELD));
        spell.close();
        dir.close();
        dir = null;
        long _exit = System.currentTimeMillis();
        log.info("Took {1} (ms) to build SpellChecker index in {0}", spellCheckIndexDir.getAbsolutePath(),
                String.valueOf((_exit - _entr)));
    } catch (Exception exc) {
        log.error("Failed to build spell checker index!", exc);
    } finally {
        if (dir != null) {
            try {
                dir.close();
            } catch (Exception zzz) {
            }
        }
        if (reader != null) {
            readerProvider.closeReader(reader);
        }
    }
}

From source file:org.silverpeas.search.indexEngine.model.DidYouMeanIndexer.java

License:Open Source License

/**
 * creates or updates a spelling index. The spelling index is created or updated from an existing
 * index. The spelling index is used to suggest words when an user executes a query that returns
 * unsatisfactory results. if a spelling index already exists, only the new words contained in the
 * index source will be added. otherwise a new index will be created
 * @param field name of the field of the index source that will be used to feed the spelling index
 * @param originalIndexDirectory represents the source index path
 * @param spellIndexDirectory represents the spelling index path
 */// w w  w . j  av  a 2 s.c o m
public static void createSpellIndex(String field, String originalIndexDirectory, String spellIndexDirectory) {
    // stop the process if method parameters is null or empty
    if (!StringUtil.isDefined(field) || !StringUtil.isDefined(originalIndexDirectory)
            || !StringUtil.isDefined(spellIndexDirectory)) {
        SilverTrace.error("indexEngine", DidYouMeanIndexer.class.toString(), "root.EX_INVALID_ARG");
        return;
    }
    // initializes local variable
    IndexReader indexReader = null;

    try {
        // create a file object with given path
        File file = new File(spellIndexDirectory);
        // open original index
        FSDirectory directory = FSDirectory.open(file);
        indexReader = IndexReader.open(FSDirectory.open(new File(originalIndexDirectory)));
        // create a Lucene dictionary with the original index
        Dictionary dictionary = new LuceneDictionary(indexReader, field);
        // index the dictionary into the spelling index
        SpellChecker spellChecker = new SpellChecker(directory);
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,
                new StandardAnalyzer(Version.LUCENE_36));
        spellChecker.indexDictionary(dictionary, config, true);
        spellChecker.close();
    } catch (CorruptIndexException e) {
        SilverTrace.error("indexEngine", DidYouMeanIndexer.class.toString(), "root.EX_INDEX_FAILED", e);
    } catch (IOException e) {
        SilverTrace.error("indexEngine", DidYouMeanIndexer.class.toString(), "root.EX_LOAD_IO_EXCEPTION", e);
    } finally {
        IOUtils.closeQuietly(indexReader);
    }

}

From source file:org.watermint.sourcecolon.org.opensolaris.opengrok.web.SearchHelper.java

License:Open Source License

/**
 * If a search did not return a hit, one may use this method to obtain
 * suggestions for a new search./*w  w  w . j a va2s.  co  m*/
 * <p/>
 * <p/>
 * Parameters which should be populated/set at this time:
 * <ul>
 * <li>{@link #projects}</li>
 * <li>{@link #dataRoot}</li>
 * <li>{@link #builder}</li>
 * </ul>
 *
 * @return a possible empty list of sugeestions.
 */
public List<Suggestion> getSuggestions() {
    if (projects == null) {
        return new ArrayList<>(0);
    }
    File[] spellIndex = null;
    if (projects.isEmpty()) {
        spellIndex = new File[] { new File(dataRoot, "spellIndex") };
    } else if (projects.size() == 1) {
        spellIndex = new File[] { new File(dataRoot, "spellIndex/" + projects.first()) };
    } else {
        spellIndex = new File[projects.size()];
        int ii = 0;
        File indexDir = new File(dataRoot, "spellIndex");
        for (String proj : projects) {
            spellIndex[ii++] = new File(indexDir, proj);
        }
    }
    List<Suggestion> res = new ArrayList<>();
    List<String> dummy = new ArrayList<>();
    for (File aSpellIndex : spellIndex) {
        if (!aSpellIndex.exists()) {
            continue;
        }
        SpellChecker checker = null;
        Suggestion s = new Suggestion(aSpellIndex.getName());
        try (FSDirectory spellDirectory = FSDirectory.open(aSpellIndex)) {
            checker = new SpellChecker(spellDirectory);
            getSuggestion(builder.getFreetext(), checker, dummy);
            s.freetext = dummy.toArray(new String[dummy.size()]);
            dummy.clear();
            getSuggestion(builder.getRefs(), checker, dummy);
            s.refs = dummy.toArray(new String[dummy.size()]);
            dummy.clear();
            // TODO it seems the only true spellchecker is for
            // below field, see IndexDatabase
            // createspellingsuggestions ...
            getSuggestion(builder.getDefs(), checker, dummy);
            s.defs = dummy.toArray(new String[dummy.size()]);
            dummy.clear();
            if (s.freetext.length > 0 || s.defs.length > 0 || s.refs.length > 0) {
                res.add(s);
            }
        } catch (IOException e) {
            log.log(Level.WARNING, "Got excption while getting spelling suggestions: ", e);
        } finally {
            if (checker != null) {
                try {
                    checker.close();
                } catch (Exception x) {
                    log.log(Level.WARNING, "Got excption while closing spelling suggestions: ", x);
                }
            }
        }
    }
    return res;
}

From source file:resource.IndexFiles.java

License:Apache License

private static void createDictionary(Analyzer analyzer) throws IOException {
    Directory dictionaryDir = FSDirectory.open(new File(DICTIONARY_PATH));
    Directory indexDir = FSDirectory.open(new File(INDEX_PATH));

    IndexReader reader = DirectoryReader.open(indexDir);
    Dictionary dictionary = new LuceneDictionary(reader, "contents");
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, analyzer);

    SpellChecker spellChecker = new SpellChecker(dictionaryDir);
    spellChecker.indexDictionary(dictionary, iwc, false);
    spellChecker.close();
}