Using Solr Spellchecker from Java
Continuing from my prevoius post Implementing Spellchecker in Solr , we'll now see how to use SorlJ API to fetch spellcheck suggestions through a Java program.
You can download a copy of complete Java Program from here.
To run the above program you will need the following jar files
public static Map<String, List<String>> getSuggestions(String queryStr){
if (queryStr != null && !queryStr.isEmpty()) {
try {
SolrServer server = new HttpSolrServer("http://localhost:8983/solr");
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/spell");
params.set("q", queryStr);
params.set("spellcheck", "true");
params.set("spellcheck.collate", "true");
QueryResponse response = server.query(params);
System.out.println("response = " + response);
if(response != null){
SpellCheckResponse scr = response.getSpellCheckResponse();
if(scr != null){
List<Suggestion> suggLst = scr.getSuggestions();
for(Suggestion sugg : suggLst){
System.out.println("Suggestion for token"+sugg.getToken()+" are: ");
System.out.println(sugg.getAlternatives());
}
System.out.println("Collated Result: "+scr.getAlternatives());
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
return suggestionsMap;
}
if (queryStr != null && !queryStr.isEmpty()) {
try {
SolrServer server = new HttpSolrServer("http://localhost:8983/solr");
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/spell");
params.set("q", queryStr);
params.set("spellcheck", "true");
params.set("spellcheck.collate", "true");
QueryResponse response = server.query(params);
System.out.println("response = " + response);
if(response != null){
SpellCheckResponse scr = response.getSpellCheckResponse();
if(scr != null){
List<Suggestion> suggLst = scr.getSuggestions();
for(Suggestion sugg : suggLst){
System.out.println("Suggestion for token"+sugg.getToken()+" are: ");
System.out.println(sugg.getAlternatives());
}
System.out.println("Collated Result: "+scr.getAlternatives());
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
return suggestionsMap;
}
You can download a copy of complete Java Program from here.
To run the above program you will need the following jar files
- apache-solr-solrj-3.6.0.jar (I am using Solr 3.6.0)
- httpclient-4.jar
- httpcore-4.jar
- httpmime-4.1.2.jar
- slf4j-log4j12-1.0.jar
- commons-logging.jar
- log4j.jar
Comments
Post a Comment