1 | package eu.clarin.cmdi.vlo.dao; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
4 | import java.net.MalformedURLException; |
---|
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 | public class SolrDao { |
---|
16 | |
---|
17 | private final static Logger LOG = LoggerFactory.getLogger(SolrDao.class); |
---|
18 | private final CommonsHttpSolrServer solrServer; |
---|
19 | |
---|
20 | public SolrDao() { |
---|
21 | String solrUrl; |
---|
22 | solrUrl = VloConfig.getSolrUrl(); |
---|
23 | try { |
---|
24 | solrServer = new CommonsHttpSolrServer(solrUrl); |
---|
25 | } catch (MalformedURLException e) { |
---|
26 | throw new RuntimeException("URL: " + solrUrl, e); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | protected CommonsHttpSolrServer getSolrserver() { |
---|
31 | return solrServer; |
---|
32 | } |
---|
33 | |
---|
34 | protected QueryResponse fireQuery(SolrQuery query) { |
---|
35 | try { |
---|
36 | return solrServer.query(query); |
---|
37 | } catch (SolrServerException e) { |
---|
38 | LOG.error("Error getting data:", e); |
---|
39 | throw new RuntimeException(e); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | public SolrDocument getSolrDocument(String docId) { |
---|
44 | SolrDocument result = null; |
---|
45 | SolrQuery query = new SolrQuery(); |
---|
46 | query.setQuery("id:" + ClientUtils.escapeQueryChars(docId)); |
---|
47 | query.setFields("*"); |
---|
48 | SolrDocumentList docs = fireQuery(query).getResults(); |
---|
49 | if (docs.getNumFound() > 1) { |
---|
50 | LOG.error("Error: found multiple documents for id (will return first one): " + docId + " \nDocuments found: " + docs); |
---|
51 | result = docs.get(0); |
---|
52 | } else if (docs.getNumFound() == 1) { |
---|
53 | result = docs.get(0); |
---|
54 | } |
---|
55 | return result; |
---|
56 | } |
---|
57 | } |
---|