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

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

Instead of retrieving the SolrUrl? from the context of the application, retrieve the location of an external VloConfig? from the context, preferably the file used by the importer. By using the context in this way, the configuration for both the importer and the web application will be one and the same.

File size: 1.9 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.config.VloConfig;
16
17public class SolrDao {
18
19    private final static Logger LOG = LoggerFactory.getLogger(SolrDao.class);
20    private final CommonsHttpSolrServer solrServer;
21
22    public SolrDao() {
23        String solrUrl;
24        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.