Example usage for org.apache.solr.spelling.suggest SuggesterParams SUGGEST_BUILD

List of usage examples for org.apache.solr.spelling.suggest SuggesterParams SUGGEST_BUILD

Introduction

In this page you can find the example usage for org.apache.solr.spelling.suggest SuggesterParams SUGGEST_BUILD.

Prototype

String SUGGEST_BUILD

To view the source code for org.apache.solr.spelling.suggest SuggesterParams SUGGEST_BUILD.

Click Source Link

Document

Whether to build the index or not.

Usage

From source file:org.schedoscope.metascope.index.SolrQueryExecutor.java

License:Apache License

public List<String> suggest(String userInput) {
    List<String> suggestions = new LinkedList<String>();

    SolrQuery query = new SolrQuery();
    query.setParam(CommonParams.QT, "/suggest");
    query.setParam("suggest", true);
    query.setParam(SuggesterParams.SUGGEST_BUILD, true);
    query.setParam(SuggesterParams.SUGGEST_DICT, "metascope");
    query.setParam(SuggesterParams.SUGGEST_Q, userInput);

    /* execute the query */
    QueryResponse queryResponse = null;//from   w w w .ja  v a  2s .  c  o  m
    try {
        queryResponse = solrClient.query(query);
    } catch (Exception e) {
        e.printStackTrace();
    }

    List<Suggestion> currentSuggestions = new LinkedList<Suggestion>();
    if (queryResponse != null) {
        SuggesterResponse suggestorRespone = queryResponse.getSuggesterResponse();
        if (suggestorRespone != null) {
            Map<String, List<Suggestion>> suggestorResponeMap = suggestorRespone.getSuggestions();
            for (Entry<String, List<Suggestion>> e : suggestorResponeMap.entrySet()) {
                for (Suggestion suggestion : e.getValue()) {
                    int counter = 0;
                    for (Suggestion s : currentSuggestions) {
                        if (s.getWeight() > suggestion.getWeight()) {
                            counter++;
                        }
                    }
                    currentSuggestions.add(counter, suggestion);
                }
            }
        }
    }

    for (Suggestion suggestion : currentSuggestions) {
        suggestions.add(suggestion.getTerm());
    }

    return suggestions;
}