source: vlo/branches/vlo-2.13-param/vlo_webapp/src/main/java/eu/clarin/cmdi/vlo/dao/SolrDao.java @ 2597

Last change on this file since 2597 was 2597, checked in by keeloo, 11 years ago

Some more work on replacing the parameter mechanism.

File size: 2.0 KB
Line 
1package eu.clarin.cmdi.vlo.dao;
2
3import java.net.MalformedURLException;
4
5import org.apache.solr.client.solrj.SolrQuery;
6import org.apache.solr.client.solrj.SolrServerException;
7import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
8import org.apache.solr.client.solrj.response.QueryResponse;
9import org.apache.solr.client.solrj.util.ClientUtils;
10import org.apache.solr.common.SolrDocument;
11import org.apache.solr.common.SolrDocumentList;
12import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14
15import eu.clarin.cmdi.vlo.Configuration;
16import eu.clarin.cmdi.vlo.config.VloConfig;
17
18public class SolrDao {
19
20    private final static Logger LOG = LoggerFactory.getLogger(SolrDao.class);
21    private final CommonsHttpSolrServer solrServer;
22
23    public SolrDao() {
24        String solrUrl = VloConfig.get().getSolrUrl();
25        try {
26            solrServer = new CommonsHttpSolrServer(solrUrl);
27        } catch (MalformedURLException e) {
28            throw new RuntimeException("URL: " + solrUrl, e);
29        }
30    }
31
32    protected CommonsHttpSolrServer getSolrserver() {
33        return solrServer;
34    }
35
36    protected QueryResponse fireQuery(SolrQuery query) {
37        try {
38            return solrServer.query(query);
39        } catch (SolrServerException e) {
40            LOG.error("Error getting data:", e);
41            throw new RuntimeException(e);
42        }
43    }
44
45    public SolrDocument getSolrDocument(String docId) {
46        SolrDocument result = null;
47        SolrQuery query = new SolrQuery();
48        query.setQuery("id:" + ClientUtils.escapeQueryChars(docId));
49        query.setFields("*");
50        SolrDocumentList docs = fireQuery(query).getResults();
51        if (docs.getNumFound() > 1) {
52            LOG.error("Error: found multiple documents for id (will return first one): " + docId + " \nDocuments found: " + docs);
53            result = docs.get(0);
54        } else if (docs.getNumFound() == 1) {
55            result = docs.get(0);
56        }
57        return result;
58    }
59}
Note: See TracBrowser for help on using the repository browser.