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.pojo.FacetSelection; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import java.util.ArrayList; |
---|
22 | import java.util.Iterator; |
---|
23 | import java.util.List; |
---|
24 | import java.util.Map; |
---|
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 abstract class AbstractSolrQueryFactory { |
---|
33 | |
---|
34 | protected static final String SOLR_SEARCH_ALL = null; |
---|
35 | |
---|
36 | protected final void addQueryFacetParameters(final SolrQuery query, QueryFacetsSelection queryFacetsSelections, boolean includeFilterQueries) { |
---|
37 | final String queryString = queryFacetsSelections.getQuery(); |
---|
38 | if (queryString == null) { |
---|
39 | query.setQuery(SOLR_SEARCH_ALL); |
---|
40 | } else { |
---|
41 | // escape query content and wrap in quotes to make literal query |
---|
42 | query.setQuery(queryString); |
---|
43 | } |
---|
44 | final Map<String, FacetSelection> selections = queryFacetsSelections.getSelection(); |
---|
45 | if (selections != null) { |
---|
46 | final List<String> encodedQueries = new ArrayList(selections.size()); // assuming every facet has one selection, most common scenario |
---|
47 | for (Map.Entry<String, FacetSelection> selectionEntry : selections.entrySet()) { |
---|
48 | final String facetName = selectionEntry.getKey(); |
---|
49 | final FacetSelection selection = selectionEntry.getValue(); |
---|
50 | if (selection != null) { |
---|
51 | switch (selection.getSelectionType()) { |
---|
52 | case NOT_EMPTY: |
---|
53 | //TODO: test |
---|
54 | encodedQueries.add(String.format("%s:[* TO *]", facetName)); |
---|
55 | break; |
---|
56 | case AND: |
---|
57 | String filterExpr = ""; |
---|
58 | for (String value : selection.getValues()) { |
---|
59 | //another hack to support multi values selection for a single facet |
---|
60 | //OR expression for values from the same facet |
---|
61 | String encodedQuery = createFilterQuery(facetName, value); |
---|
62 | if(!filterExpr.isEmpty()) |
---|
63 | filterExpr += " OR "; |
---|
64 | filterExpr += encodedQuery; |
---|
65 | } |
---|
66 | |
---|
67 | encodedQueries.add(filterExpr); |
---|
68 | break; |
---|
69 | default: |
---|
70 | //TODO: support OR,NOT |
---|
71 | throw new UnsupportedOperationException("Unsupported selection type: " + selection.getSelectionType()); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | if(includeFilterQueries) |
---|
76 | query.setFilterQueries(encodedQueries.toArray(new String[encodedQueries.size()])); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | protected final String createFilterQuery(String facetName, String value) { |
---|
81 | // escape value and wrap in quotes to make literal query |
---|
82 | return String.format("%s:\"%s\"", facetName, ClientUtils.escapeQueryChars(value)); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Creates an OR filter query over the provided facet/value pairs (a query |
---|
87 | * that requests all records matching ANY of the facet/value pairs) |
---|
88 | * |
---|
89 | * @param facetValues map with facet/value pairs that should be matched |
---|
90 | * @return |
---|
91 | */ |
---|
92 | protected final String createFilterOrQuery(Map<String, String> facetValues) { |
---|
93 | // escape value and wrap in quotes to make literal query |
---|
94 | final StringBuilder queryBuilder = new StringBuilder(); |
---|
95 | final Iterator<Map.Entry<String, String>> iterator = facetValues.entrySet().iterator(); |
---|
96 | while (iterator.hasNext()) { |
---|
97 | final Map.Entry<String, String> facetValue = iterator.next(); |
---|
98 | queryBuilder.append(createFilterQuery(facetValue.getKey(), facetValue.getValue())); |
---|
99 | if (iterator.hasNext()) { |
---|
100 | queryBuilder.append(" OR "); |
---|
101 | } |
---|
102 | } |
---|
103 | return queryBuilder.toString(); |
---|
104 | } |
---|
105 | |
---|
106 | } |
---|