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.impl; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
20 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
21 | import eu.clarin.cmdi.vlo.service.SolrQueryFactory; |
---|
22 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
23 | import java.util.ArrayList; |
---|
24 | import java.util.Collection; |
---|
25 | import java.util.List; |
---|
26 | import org.apache.solr.client.solrj.SolrQuery; |
---|
27 | import org.apache.solr.client.solrj.util.ClientUtils; |
---|
28 | |
---|
29 | /** |
---|
30 | * |
---|
31 | * @author twagoo |
---|
32 | */ |
---|
33 | public class SolrQueryFactoryImpl implements SolrQueryFactory { |
---|
34 | |
---|
35 | private static final String SOLR_SEARCH_ALL = "*:*"; |
---|
36 | private final SolrQuery countQuery; |
---|
37 | private final VloConfig config; |
---|
38 | |
---|
39 | public SolrQueryFactoryImpl(VloConfig config) { |
---|
40 | this.config = config; |
---|
41 | |
---|
42 | // create the query used to count facets (will never change) |
---|
43 | countQuery = getDefaultFacetQuery(); |
---|
44 | countQuery.setRows(0); |
---|
45 | } |
---|
46 | |
---|
47 | @Override |
---|
48 | public SolrQuery createFacetQuery(List<FacetSelection> selections, String queryString) { |
---|
49 | SolrQuery query = getDefaultFacetQuery(); |
---|
50 | |
---|
51 | if (queryString == null) { |
---|
52 | query.setQuery(SOLR_SEARCH_ALL); |
---|
53 | } else { |
---|
54 | query.setQuery(ClientUtils.escapeQueryChars(queryString)); |
---|
55 | } |
---|
56 | |
---|
57 | if (selections != null) { |
---|
58 | final List<String> encodedQueries = new ArrayList(selections.size()); |
---|
59 | for (FacetSelection selection : selections) { |
---|
60 | final String facetName = selection.getFacetName(); |
---|
61 | final Collection<String> values = selection.getValue(); |
---|
62 | if (values != null) { |
---|
63 | for (String value : values) { |
---|
64 | encodedQueries.add(String.format("%s:%s", facetName, ClientUtils.escapeQueryChars(value))); |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | query.setFilterQueries(encodedQueries.toArray(new String[encodedQueries.size()])); |
---|
69 | } |
---|
70 | return query; |
---|
71 | } |
---|
72 | |
---|
73 | private SolrQuery getDefaultFacetQuery() { |
---|
74 | SolrQuery result = new SolrQuery(); |
---|
75 | result.setRows(10); |
---|
76 | result.setStart(0); |
---|
77 | result.setFields(FacetConstants.FIELD_NAME, FacetConstants.FIELD_ID, FacetConstants.FIELD_DESCRIPTION); |
---|
78 | result.setFacet(true); |
---|
79 | result.setFacetMinCount(1); |
---|
80 | result.addFacetField(config.getFacetFields()); |
---|
81 | return result; |
---|
82 | } |
---|
83 | |
---|
84 | @Override |
---|
85 | public synchronized SolrQuery createCountFacetsQuery() { |
---|
86 | return countQuery; |
---|
87 | } |
---|
88 | |
---|
89 | } |
---|