Example usage for org.apache.lucene.search.suggest.analyzing AnalyzingSuggester lookup

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

Introduction

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

Prototype

public List<LookupResult> lookup(CharSequence key, boolean onlyMorePopular, int num) throws IOException 

Source Link

Document

Look up a key and return possible completion for this key.

Usage

From source file:suonos.controllers.SearchController.java

License:Apache License

/**
 * GET /api/search/suggestions?q=rock/*from w  w w .ja v a2s . c  o m*/
 * 
 * @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);
}