Example usage for org.apache.lucene.search.suggest.analyzing AnalyzingInfixSuggester AnalyzingInfixSuggester

List of usage examples for org.apache.lucene.search.suggest.analyzing AnalyzingInfixSuggester AnalyzingInfixSuggester

Introduction

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

Prototype

public AnalyzingInfixSuggester(Directory dir, Analyzer analyzer) throws IOException 

Source Link

Document

Create a new instance, loading from a previously built AnalyzingInfixSuggester directory, if it exists.

Usage

From source file:com.semantic.util.suggest.NewSuggestionHints.java

public void setIndexReader(IndexReader indexReader) {
    this.indexReader = indexReader;
    /* only build 1 time */
    if (suggester == null) {
        Dictionary directory = new MultiFieldTermLuceneDirectory(indexReader, fields);
        try {//w w w . j a  va 2  s. c  o m
            suggester = new AnalyzingInfixSuggester(
                    //                        new File("./suggestidx"),
                    new RAMDirectory(), new StandardAnalyzer());
            suggester.build(directory);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
}

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

License:MIT License

/**
 * Open a suggestion index./*  www.j a v  a2s .  c om*/
 * @param indexRoot The parent directory inside which the suggestion index lives.
 * @throws IOException
 */
public Suggester(String indexRoot) throws IOException {
    indexRootPath = Paths.get(indexRoot);
    Analyzer analyzer = Indexer.getAnalyzer();
    Directory suggestionDir = FSDirectory.open(getSuggestionIndexPath(indexRootPath));
    suggester = new AnalyzingInfixSuggester(suggestionDir, analyzer);
    suggestionCount = DEFAULT_SUGGESTION_COUNT;
}

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 2  s . 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:suonos.controllers.SearchController.java

License:Apache License

/**
 * GET /api/search/suggestions?q=rock//from  w ww  .jav  a 2s. c om
 * 
 * @throws IOException
 */
@Action
public Object suggestions() throws IOException {
    QueryResults<?> objects;

    String q = ctx.param("q");

    NIOFSDirectory directory = lib.luceneIndex().getLuceneDirectory();
    IndexReader rdr = DirectoryReader.open(directory);
    Dictionary dict = new LuceneDictionary(rdr, "track_title");

    // Problem is track_title is all lower case!!! ??
    //
    // Also need a composite "search" field for searching:
    // ie seach "bach", or search "schiff" should return all albums
    // containing terms bach, schiff, etc.
    // "<artists> <composers> <arrangers> <album title>"
    //
    // would we ever search for tracks:
    // "artists composers arrangers title"
    //
    // Could search for tracks and then get the album id from lucene????
    //
    List<LookupResult> results;

    if (false) {
        // http://lucene.apache.org/core/4_4_0/suggest/index.html
        AnalyzingSuggester as1 = new AnalyzingSuggester(new StandardAnalyzer());
        as1.build(dict);
        results = as1.lookup(q, false, 10);

    } else {
        // Returns suggestions.
        // Eg: for "ba" -
        // "bach", "bat", "banner"
        //
        try (AnalyzingInfixSuggester as = new AnalyzingInfixSuggester(directory, new StandardAnalyzer())) {
            as.build(dict);

            results = as.lookup(q, false, 10);
        }
    }

    // Return back as a json object.
    //
    return jsonData(results);
}