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

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

Introduction

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

Prototype

String SUGGEST_Q

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

Click Source Link

Document

Use the value for this parameter as the query to spell check.

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  ww .ja v  a  2s. co  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;
}