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.pojo.QueryFacetsSelection; |
---|
20 | import java.util.ArrayList; |
---|
21 | import java.util.Collection; |
---|
22 | import java.util.List; |
---|
23 | import java.util.Map; |
---|
24 | import org.apache.solr.client.solrj.SolrQuery; |
---|
25 | import org.apache.solr.client.solrj.util.ClientUtils; |
---|
26 | |
---|
27 | /** |
---|
28 | * |
---|
29 | * @author twagoo |
---|
30 | */ |
---|
31 | public abstract class AbstractSolrQueryFactory { |
---|
32 | |
---|
33 | private static final String SOLR_SEARCH_ALL = "*:*"; |
---|
34 | |
---|
35 | protected void addQueryFacetParameters(final SolrQuery query, QueryFacetsSelection queryFacetsSelections) { |
---|
36 | final String queryString = queryFacetsSelections.getQuery(); |
---|
37 | if (queryString == null) { |
---|
38 | query.setQuery(SOLR_SEARCH_ALL); |
---|
39 | } else { |
---|
40 | query.setQuery(ClientUtils.escapeQueryChars(queryString)); |
---|
41 | } |
---|
42 | final Map<String, Collection<String>> selections = queryFacetsSelections.getSelection(); |
---|
43 | if (selections != null) { |
---|
44 | final List<String> encodedQueries = new ArrayList(selections.size()); // assuming every facet has one selection, most common scenario |
---|
45 | for (Map.Entry<String, Collection<String>> selection : selections.entrySet()) { |
---|
46 | final String facetName = selection.getKey(); |
---|
47 | final Collection<String> values = selection.getValue(); |
---|
48 | if (values != null) { |
---|
49 | for (String value : values) { |
---|
50 | encodedQueries.add(String.format("%s:%s", facetName, ClientUtils.escapeQueryChars(value))); |
---|
51 | } |
---|
52 | } |
---|
53 | } |
---|
54 | query.setFilterQueries(encodedQueries.toArray(new String[encodedQueries.size()])); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | } |
---|