I am trying to implement auto complete using Solr's Spellcheck response. I am able to get the response for the given query,but I am unable to get the terms suggested to a List of strings in Solrj.
Solr response:
{
"responseHeader":{
"status":0,
"QTime":1},
"spellcheck":{
"suggestions":[
"stac",{
"numFound":4,
"startOffset":0,
"endOffset":3,
"suggestion":["stack",
"stacking"]}]}}
SolrJ Code:
SolrClient solrClients=new HttpSolrClient.Builder("http://localhost:8983/solr/star/").build();
SolrQuery sq = new SolrQuery();
sq.setRequestHandler("/suggest");
sq.set("spellcheck", true);
sq.set("spellcheck.dictionary", "suggestDictionary");
sq.set("suggest.q",query);
sq.setQuery(query);
QueryResponse rsp = solrClients.query(sq);
I am trying to get the suggested words into a List of strings,but unable to find a solution.
Thanks in advance.
You are nearly there. Once you get the QueryRespnse, you need to extract the SpellCheckResponse and then get the suggestions from there in the following way:
QueryResponse queryResponse = solrClients.query(sq);
SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse();
List<Suggestion> suggestions = spellCheckResponse.getSuggestions();
it's worth mentioning that you have also the option of getting suggestions in a map as:
Map<String, Suggestion> suggestionsMap = spellCheckResponse.getSuggestionMap();
Once you have the suggestion that you want (Iterating over the list or getting the value from the map), you can get the list of alternatives using this code:
List<String suggestedWords> = spellCheckResponse.getAlternatives();