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