Example usage for org.apache.solr.client.solrj.response Suggestion getTerm

List of usage examples for org.apache.solr.client.solrj.response Suggestion getTerm

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.response Suggestion getTerm.

Prototype

public String getTerm() 

Source Link

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  ww  w  .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;
}