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.FacetConstants; |
---|
20 | import eu.clarin.cmdi.vlo.service.solr.SolrFacetQueryFactory; |
---|
21 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
22 | import java.util.List; |
---|
23 | import org.apache.solr.client.solrj.SolrQuery; |
---|
24 | |
---|
25 | /** |
---|
26 | * Implements a SOLR query factory, to be used by SOLR service implementation |
---|
27 | * for the VLO |
---|
28 | * |
---|
29 | * @author twagoo |
---|
30 | */ |
---|
31 | public class SolrFacetQueryFactoryImpl extends AbstractSolrQueryFactory implements SolrFacetQueryFactory { |
---|
32 | |
---|
33 | private final SolrQuery baseQuery; |
---|
34 | |
---|
35 | /** |
---|
36 | * |
---|
37 | */ |
---|
38 | public SolrFacetQueryFactoryImpl() { |
---|
39 | // create the base query (copied on each request to create new queries) |
---|
40 | baseQuery = new SolrQuery(); |
---|
41 | baseQuery.setRows(0); |
---|
42 | baseQuery.setFacet(true); |
---|
43 | baseQuery.setFacetMinCount(1); |
---|
44 | } |
---|
45 | |
---|
46 | @Override |
---|
47 | public SolrQuery createFacetQuery(QueryFacetsSelection queryFacetsSelections, List<String> facets, int facetValueLimit) { |
---|
48 | final SolrQuery query = getBaseQuery(facets); |
---|
49 | // we can use the 'fast' request handler here, document ranking is of no interest |
---|
50 | query.setRequestHandler(FacetConstants.SOLR_REQUEST_HANDLER_FAST); |
---|
51 | addQueryFacetParameters(query, queryFacetsSelections); |
---|
52 | query.setFacetLimit(facetValueLimit); |
---|
53 | return query; |
---|
54 | } |
---|
55 | |
---|
56 | @Override |
---|
57 | public synchronized SolrQuery createCountFacetsQuery(List<String> facets) { |
---|
58 | final SolrQuery query = getBaseQuery(facets); |
---|
59 | query.setFacetLimit(0); |
---|
60 | return query; |
---|
61 | } |
---|
62 | |
---|
63 | private SolrQuery getBaseQuery(List<String> facets) { |
---|
64 | SolrQuery query = baseQuery.getCopy(); |
---|
65 | query.addFacetField(facets.toArray(new String[facets.size()])); |
---|
66 | return query; |
---|
67 | } |
---|
68 | |
---|
69 | } |
---|