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.List; |
---|
25 | import org.apache.solr.client.solrj.SolrQuery; |
---|
26 | import org.apache.solr.client.solrj.util.ClientUtils; |
---|
27 | |
---|
28 | /** |
---|
29 | * |
---|
30 | * @author twagoo |
---|
31 | */ |
---|
32 | public class SolrQueryFactoryImpl implements SolrQueryFactory { |
---|
33 | |
---|
34 | private static final String SOLR_SEARCH_ALL = "*:*"; |
---|
35 | private final VloConfig config; |
---|
36 | |
---|
37 | public SolrQueryFactoryImpl(VloConfig config) { |
---|
38 | this.config = config; |
---|
39 | } |
---|
40 | |
---|
41 | @Override |
---|
42 | public SolrQuery createFacetQuery(List<FacetSelection> selections, String queryString) { |
---|
43 | SolrQuery query = getDefaultFacetQuery(); |
---|
44 | |
---|
45 | if (queryString == null) { |
---|
46 | query.setQuery(SOLR_SEARCH_ALL); |
---|
47 | } else { |
---|
48 | query.setQuery(ClientUtils.escapeQueryChars(queryString)); |
---|
49 | } |
---|
50 | |
---|
51 | if (selections != null) { |
---|
52 | final List<String> encodedQueries = new ArrayList(selections.size()); |
---|
53 | for (FacetSelection selection : selections) { |
---|
54 | String facet = selection.getFacet().getName(); |
---|
55 | for (String value : selection.getValue()) { |
---|
56 | encodedQueries.add(String.format("%s:%s", facet, ClientUtils.escapeQueryChars(value))); |
---|
57 | } |
---|
58 | } |
---|
59 | query.setFilterQueries(encodedQueries.toArray(new String[encodedQueries.size()])); |
---|
60 | } |
---|
61 | return query; |
---|
62 | } |
---|
63 | |
---|
64 | private SolrQuery getDefaultFacetQuery() { |
---|
65 | SolrQuery result = new SolrQuery(); |
---|
66 | result.setRows(10); |
---|
67 | result.setStart(0); |
---|
68 | result.setFields(FacetConstants.FIELD_NAME, FacetConstants.FIELD_ID, FacetConstants.FIELD_DESCRIPTION); |
---|
69 | result.setFacet(true); |
---|
70 | result.setFacetMinCount(1); |
---|
71 | result.addFacetField(config.getFacetFields()); |
---|
72 | return result; |
---|
73 | } |
---|
74 | } |
---|