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

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

Introduction

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

Prototype

@Override
    public void build(InputIterator iterator) throws IOException 

Source Link

Usage

From source file:suonos.controllers.SearchController.java

License:Apache License

/**
 * GET /api/search/suggestions?q=rock/*from w  ww  .  j  a v a2  s .com*/
 * 
 * @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);
}