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