1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.service.solr.impl; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
20 | import eu.clarin.cmdi.vlo.service.solr.FacetFieldsService; |
---|
21 | import eu.clarin.cmdi.vlo.service.solr.SearchResultsDao; |
---|
22 | import eu.clarin.cmdi.vlo.service.solr.SolrFacetQueryFactory; |
---|
23 | import java.util.List; |
---|
24 | import org.apache.solr.client.solrj.response.FacetField; |
---|
25 | |
---|
26 | /** |
---|
27 | * Gets FacetFields from SOLR based on a selection and the queries constructed |
---|
28 | * by the provided query factory |
---|
29 | * |
---|
30 | * @author twagoo |
---|
31 | */ |
---|
32 | public class SolrFacetFieldsService implements FacetFieldsService { |
---|
33 | |
---|
34 | private final SearchResultsDao searchResultsDao; |
---|
35 | private final SolrFacetQueryFactory queryFatory; |
---|
36 | |
---|
37 | /** |
---|
38 | * |
---|
39 | * @param searchResultsDao DAO to use to retrieve facets |
---|
40 | * @param queryFatory factory to use to construct facet queries |
---|
41 | */ |
---|
42 | public SolrFacetFieldsService(SearchResultsDao searchResultsDao, SolrFacetQueryFactory queryFatory) { |
---|
43 | this.searchResultsDao = searchResultsDao; |
---|
44 | this.queryFatory = queryFatory; |
---|
45 | } |
---|
46 | |
---|
47 | @Override |
---|
48 | public List<FacetField> getFacetFields(QueryFacetsSelection selection, List<String> facets, int valueLimit) { |
---|
49 | //if excluded facet exists and it is equal to selected fire additional query |
---|
50 | return (selection.getExcludedFacet() != null && selection.getExcludedFacet().equals(facets.get(0)))? |
---|
51 | searchResultsDao.getFacets(queryFatory.createFacetQuery(selection, facets, valueLimit), |
---|
52 | queryFatory.createExludedFacetQuery(selection, selection.getExcludedFacet(), valueLimit)) : |
---|
53 | searchResultsDao.getFacets(queryFatory.createFacetQuery(selection, facets, valueLimit)); |
---|
54 | } |
---|
55 | |
---|
56 | @Override |
---|
57 | public long getFacetFieldCount(List<String> facets) { |
---|
58 | return (long) searchResultsDao.getFacets(queryFatory.createCountFacetsQuery(facets)).size(); |
---|
59 | } |
---|
60 | |
---|
61 | } |
---|