1 | package eu.clarin.cmdi.vlo.service.solr.impl; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.service.solr.SearchResultsDao; |
---|
4 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
5 | import java.util.List; |
---|
6 | import org.apache.solr.client.solrj.SolrQuery; |
---|
7 | import org.apache.solr.client.solrj.SolrServer; |
---|
8 | import org.apache.solr.client.solrj.response.FacetField; |
---|
9 | import org.apache.solr.client.solrj.response.FacetField.Count; |
---|
10 | import org.apache.solr.client.solrj.response.QueryResponse; |
---|
11 | import org.apache.solr.common.SolrDocumentList; |
---|
12 | import org.slf4j.Logger; |
---|
13 | import org.slf4j.LoggerFactory; |
---|
14 | |
---|
15 | public class SearchResultsDaoImpl extends SolrDaoImpl implements SearchResultsDao { |
---|
16 | |
---|
17 | private final static Logger logger = LoggerFactory.getLogger(SearchResultsDaoImpl.class); |
---|
18 | |
---|
19 | public SearchResultsDaoImpl(SolrServer solrServer, VloConfig config) { |
---|
20 | super(solrServer, config); |
---|
21 | } |
---|
22 | |
---|
23 | @Override |
---|
24 | public List<FacetField> getFacets(SolrQuery query) { |
---|
25 | final QueryResponse response = fireQuery(sanitise(query)); |
---|
26 | final List<FacetField> facetFields = response.getFacetFields(); |
---|
27 | if (logger.isDebugEnabled()) { |
---|
28 | if (facetFields.size() == 1) { |
---|
29 | final FacetField field = facetFields.get(0); |
---|
30 | logger.debug("Found facet field '{}' with {} values", field.getName(), field.getValueCount()); |
---|
31 | } else { |
---|
32 | logger.debug("Found {} facet fields", facetFields.size()); |
---|
33 | } |
---|
34 | } |
---|
35 | return facetFields; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | @Override |
---|
40 | public java.util.List<FacetField> getFacets(SolrQuery query, SolrQuery completeFacetValList) { |
---|
41 | final QueryResponse response = fireQuery(sanitise(query)); |
---|
42 | |
---|
43 | final List<FacetField> facetFields = getFacets(query); |
---|
44 | |
---|
45 | |
---|
46 | final QueryResponse completeList = fireQuery(sanitise(completeFacetValList)); |
---|
47 | final FacetField excludedFacet = completeList.getFacetFields().get(0); |
---|
48 | FacetField newFacet = null; |
---|
49 | Integer ind = null; |
---|
50 | |
---|
51 | for(FacetField facet: facetFields){ |
---|
52 | if(facet.getName().equals(excludedFacet.getName())){ |
---|
53 | logger.debug("Missing values for excluded Facet {} will be added", facet.getName()); |
---|
54 | newFacet = copyFacetField(facet); |
---|
55 | ind = facetFields.indexOf(facet); |
---|
56 | nextVal: for(FacetField.Count exVal: excludedFacet.getValues()){ |
---|
57 | boolean exists = false; |
---|
58 | for(FacetField.Count val: facet.getValues()){ |
---|
59 | if(exVal.getName().equals(val.getName())){ |
---|
60 | exists = true; |
---|
61 | break; |
---|
62 | } |
---|
63 | } |
---|
64 | //import missing values into the facet from the list and set count to zero |
---|
65 | if(!exists){ |
---|
66 | logger.debug("Missing value {} is added with count 0", exVal.getName()); |
---|
67 | newFacet.add(exVal.getName(), 0); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | if(ind != null){ |
---|
74 | facetFields.remove(ind); |
---|
75 | facetFields.add(ind, newFacet); |
---|
76 | } |
---|
77 | |
---|
78 | return facetFields; |
---|
79 | } |
---|
80 | |
---|
81 | private FacetField copyFacetField(FacetField from){ |
---|
82 | FacetField newFacet = new FacetField(from.getName(), from.getGap(), from.getEnd()); |
---|
83 | for(FacetField.Count val: from.getValues()) |
---|
84 | newFacet.add(val.getName(), val.getCount()); |
---|
85 | |
---|
86 | return newFacet; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | @Override |
---|
91 | public SolrDocumentList getDocuments(SolrQuery query) { |
---|
92 | QueryResponse queryResponse = fireQuery(query); |
---|
93 | final SolrDocumentList documents = queryResponse.getResults(); |
---|
94 | logger.debug("Found {} documents", documents.getNumFound()); |
---|
95 | return documents; |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|