source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/solr/impl/SolrDaoImpl.java @ 4661

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

Package structure: separated solr services from other services and moved all panels into new wicket.panels package

File size: 3.9 KB
Line 
1package eu.clarin.cmdi.vlo.service.solr.impl;
2
3import eu.clarin.cmdi.vlo.config.VloConfig;
4import java.util.HashSet;
5import java.util.Set;
6import org.apache.solr.client.solrj.SolrQuery;
7import org.apache.solr.client.solrj.SolrServer;
8import org.apache.solr.client.solrj.SolrServerException;
9import org.apache.solr.client.solrj.response.QueryResponse;
10import org.apache.solr.client.solrj.util.ClientUtils;
11import org.apache.solr.common.SolrDocument;
12import org.apache.solr.common.SolrDocumentList;
13import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
16public class SolrDaoImpl {
17   
18    private final static Logger logger = LoggerFactory.getLogger(SolrDaoImpl.class);
19    private final SolrServer solrServer;
20    private final VloConfig config;
21
22    public SolrDaoImpl(SolrServer solrServer, VloConfig config) {
23        this.solrServer = solrServer;
24        this.config = config;
25    }
26
27    protected SolrServer getSolrserver() {
28        return solrServer;
29    }
30
31    /**
32     * Basic sanitising of Solr queries.
33     *
34     * TODO: Move this to QueryFacetSelection level??
35     *
36     * Query is based on the URL to the VLO web application. Also, explain about
37     * the URL and ?fq=language:dutch Assume filters have the form a:b like for
38     * example language:dutch
39     *
40     * @param query
41     * @return
42     */
43    protected SolrQuery sanitise(SolrQuery query) {
44
45        // String [] facetsFromConfig;
46        // try and get the filters facets from the query
47        String[] filtersInQuery;
48        filtersInQuery = query.getFilterQueries();
49
50        if (filtersInQuery == null) {
51            // the query does not contain filters
52        } else {
53            // get the facets from the configuration file
54            // facetsFromConfig = VloConfig.getFacetFields();
55
56            // present the facets from the config file as a list to a new set
57            Set<String> facetsDefined;
58            facetsDefined = new HashSet<String>(config.getAllFacetFields());
59
60            // check the filters in the query by name
61            for (String filter : filtersInQuery) {
62                // split up a filter, look at the string preceeding the semicolon
63                String facetInFilter = filter.split(":")[0];
64
65                if (facetsDefined.contains(facetInFilter)) {
66                    // facet in the filter is in the set that is defined by the config file
67                } else {
68                    if (facetInFilter.startsWith("_")) {
69                        // this facet is hidden, do not consider it
70                    } else {
71                        // the filter name does not match a facet in the facet
72                        query.removeFilterQuery(filter);
73                    }
74                }
75            }
76        }
77
78        // finally, return the sanitised query
79        return query;
80    }
81
82    protected QueryResponse fireQuery(SolrQuery query) {
83        try {
84            logger.debug("Executing query: {}", query);
85            final QueryResponse response = solrServer.query(query);
86            logger.trace("Response: {}", response);
87            return response;
88        } catch (SolrServerException e) {
89            logger.error("Error getting data:", e);
90            throw new RuntimeException(e);
91        }
92    }
93
94    public SolrDocument getSolrDocument(String docId) {
95        if (docId == null) {
96            throw new NullPointerException("Cannot get SOLR document for null docId");
97        }
98        SolrDocument result = null;
99        SolrQuery query = new SolrQuery();
100        query.setQuery("id:" + ClientUtils.escapeQueryChars(docId));
101        query.setFields("*");
102        SolrDocumentList docs = fireQuery(query).getResults();
103        if (docs.getNumFound() > 1) {
104            logger.error("Error: found multiple documents for id (will return first one): " + docId + " \nDocuments found: " + docs);
105            result = docs.get(0);
106        } else if (docs.getNumFound() == 1) {
107            result = docs.get(0);
108        }
109        return result;
110    }
111}
Note: See TracBrowser for help on using the repository browser.