1 | package eu.clarin.cmdi.vlo.service.solr.impl; |
---|
2 | |
---|
3 | import com.google.common.collect.Iterators; |
---|
4 | import eu.clarin.cmdi.vlo.service.solr.AutoCompleteService; |
---|
5 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.List; |
---|
8 | |
---|
9 | import org.apache.solr.client.solrj.SolrQuery; |
---|
10 | import org.apache.solr.client.solrj.SolrServer; |
---|
11 | import org.apache.solr.client.solrj.response.QueryResponse; |
---|
12 | import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion; |
---|
13 | |
---|
14 | /** |
---|
15 | * DAO that delivers suggestions for incomplete terms (autocomplete function) |
---|
16 | * |
---|
17 | * @author Thomas Eckart |
---|
18 | * @author Twan Goosen |
---|
19 | * |
---|
20 | */ |
---|
21 | public class AutoCompleteServiceImpl extends SolrDaoImpl implements AutoCompleteService { |
---|
22 | |
---|
23 | public AutoCompleteServiceImpl(SolrServer solrServer, VloConfig config) { |
---|
24 | super(solrServer, config); |
---|
25 | } |
---|
26 | |
---|
27 | /** |
---|
28 | * Returns list of suggestions for incomplete input (used for autocomplete |
---|
29 | * function) |
---|
30 | * |
---|
31 | * @param input user input |
---|
32 | * @return iterator over suggestions |
---|
33 | */ |
---|
34 | @Override |
---|
35 | public Iterator<String> getChoices(String input) { |
---|
36 | if (input != null) { |
---|
37 | |
---|
38 | final SolrQuery query = new SolrQuery(); |
---|
39 | query.setQuery(input.toLowerCase()); |
---|
40 | query.setRequestHandler("/suggest"); |
---|
41 | |
---|
42 | final QueryResponse response = fireQuery(sanitise(query)); |
---|
43 | |
---|
44 | if (response.getSpellCheckResponse() != null) { |
---|
45 | final List<Suggestion> suggestions = response.getSpellCheckResponse().getSuggestions(); |
---|
46 | if (suggestions.size() > 0) { |
---|
47 | return suggestions.get(0).getAlternatives().iterator(); |
---|
48 | } |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | return Iterators.emptyIterator(); |
---|
53 | } |
---|
54 | } |
---|