source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/AutoCompleteDao.java @ 4629

Last change on this file since 4629 was 4629, checked in by Twan Goosen, 10 years ago

Landing page link now working. Document queries no longer get sanitised. Added some inline comments to document query factory

  • Property svn:mime-type set to text/plain
File size: 1.7 KB
Line 
1package eu.clarin.cmdi.vlo.service.impl;
2
3import eu.clarin.cmdi.vlo.config.VloConfig;
4import java.util.ArrayList;
5import java.util.Iterator;
6import java.util.List;
7
8import org.apache.solr.client.solrj.SolrQuery;
9import org.apache.solr.client.solrj.SolrServer;
10import org.apache.solr.client.solrj.response.QueryResponse;
11import 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 */
19public 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}
Note: See TracBrowser for help on using the repository browser.