Example usage for org.apache.lucene.search.suggest DocumentDictionary DocumentDictionary

List of usage examples for org.apache.lucene.search.suggest DocumentDictionary DocumentDictionary

Introduction

In this page you can find the example usage for org.apache.lucene.search.suggest DocumentDictionary DocumentDictionary.

Prototype

public DocumentDictionary(IndexReader reader, String field, String weightField) 

Source Link

Document

Creates a new dictionary with the contents of the fields named field for the terms and weightField for the weights that will be used for the corresponding terms.

Usage

From source file:org.lukhnos.lucenestudy.Suggester.java

License:MIT License

/**
 * Rebuild a suggestion index from the document index.
 *
 * This method iterates through the entire document index and makes sure that only unique titles
 * are indexed.//from w w  w  .j  a  va  2s . co  m
 *
 * @param indexRoot The parent directory inside which both the document index and the suggestion
 *                  index lives.
 * @throws IOException
 */
public static void rebuild(String indexRoot) throws IOException {
    Path indexRootPath = Paths.get(indexRoot);
    Path suggestionPath = getSuggestionIndexPath(indexRootPath);

    // Delete the suggestion index if it exists.
    if (Files.exists(suggestionPath)) {
        Util.deletePath(suggestionPath);
    }

    // Create the suggestion index.
    Analyzer analyzer = Indexer.getAnalyzer();
    Directory suggestionDir = FSDirectory.open(getSuggestionIndexPath(indexRootPath));
    AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(suggestionDir, analyzer);

    // Open the document index.
    Directory indexDir = FSDirectory.open(Indexer.getMainIndexPath(indexRootPath));
    IndexReader reader = DirectoryReader.open(indexDir);

    // Get a document iterator.
    DocumentDictionary docDict = new DocumentDictionary(reader, Indexer.TITLE_FIELD_NAME, null);
    InputIterator iterator = docDict.getEntryIterator();
    Set<BytesRef> titleSet = new HashSet<>();
    BytesRef next;
    while ((next = iterator.next()) != null) {
        if (titleSet.contains(next)) {
            continue;
        }

        titleSet.add(next);
        suggester.add(next, null, 0, null);
    }

    reader.close();

    suggester.commit();
    suggester.close();
}

From source file:spimedb.SpimeDB.java

License:Apache License

@Nullable
private Lookup suggester() {

    Lookup suggester = this.suggester;

    if (suggester == null || lastWrite - lastSuggesterCreated > minSuggesterUpdatePeriod) {
        suggester = null; //re-create since it is invalidated

        Lock l = locker.lock("_suggester");

        try {//ww  w  . ja v  a  2 s .c  o  m
            if (this.suggester != null)
                return this.suggester; //created while waiting for the lock

            FreeTextSuggester nextSuggester = new FreeTextSuggester(new SimpleAnalyzer());

            read((nameDictReader) -> {

                if (nameDictReader.maxDoc() == 0)
                    return;

                DocumentDictionary nameDict = new DocumentDictionary(nameDictReader, NObject.NAME,
                        NObject.NAME);

                Stopwatch time = Stopwatch.createStarted();

                try {
                    nextSuggester.build(nameDict);
                } catch (IOException f) {
                    logger.error("suggester build {}", f);
                }

                this.suggester = nextSuggester;

                lastSuggesterCreated = now();
                logger.info("suggester built size={} {}ms", nextSuggester.getCount(),
                        time.elapsed(MILLISECONDS));

                //time.reset();

                //                    FacetsCollector fc = new FacetsCollector();
                //                    FacetsCollector.search(new IndexSearcher(nameDictReader), new MatchAllDocsQuery(), Integer.MAX_VALUE,
                //                            fc
                //                    );
                //                    logger.info("facets updated, count={} {}ms", fc., time.elapsed(MILLISECONDS));

            });

            return this.suggester;
        } finally {
            l.unlock();
        }

    } else {
        return suggester;
    }

}