source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/solr/impl/SolrDaoImpl.java @ 6676

Last change on this file since 6676 was 6676, checked in by Twan Goosen, 9 years ago

merged changes from 3.3 branch to trunk

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