1 | package eu.clarin.cmdi.vlo.dao; |
---|
2 | |
---|
3 | import java.net.MalformedURLException; |
---|
4 | |
---|
5 | import org.apache.solr.client.solrj.SolrQuery; |
---|
6 | import org.apache.solr.client.solrj.SolrServerException; |
---|
7 | import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; |
---|
8 | import org.apache.solr.client.solrj.response.QueryResponse; |
---|
9 | import org.apache.solr.client.solrj.util.ClientUtils; |
---|
10 | import org.apache.solr.common.SolrDocument; |
---|
11 | import org.apache.solr.common.SolrDocumentList; |
---|
12 | import org.slf4j.Logger; |
---|
13 | import org.slf4j.LoggerFactory; |
---|
14 | |
---|
15 | import eu.clarin.cmdi.vlo.Configuration; |
---|
16 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
17 | |
---|
18 | public 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 | } |
---|