source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/solr/impl/AbstractSolrQueryFactory.java @ 6666

Last change on this file since 6666 was 6666, checked in by Twan Goosen, 9 years ago

merged changes from 3.3 branch to trunk

File size: 4.1 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.service.solr.impl;
18
19import eu.clarin.cmdi.vlo.pojo.FacetSelection;
20import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
21import java.util.ArrayList;
22import java.util.Iterator;
23import java.util.List;
24import java.util.Map;
25import org.apache.solr.client.solrj.SolrQuery;
26import org.apache.solr.client.solrj.util.ClientUtils;
27
28/**
29 *
30 * @author twagoo
31 */
32public abstract class AbstractSolrQueryFactory {
33
34    protected static final String SOLR_SEARCH_ALL = "*:*";
35
36    protected final void addQueryFacetParameters(final SolrQuery query, QueryFacetsSelection queryFacetsSelections) {
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                            for (String value : selection.getValues()) {
58                                encodedQueries.add(createFilterQuery(facetName, value));
59                            }
60                            break;
61                        default:
62                            //TODO: support OR,NOT
63                            throw new UnsupportedOperationException("Unsupported selection type: " + selection.getSelectionType());
64                    }
65                }
66            }
67            query.setFilterQueries(encodedQueries.toArray(new String[encodedQueries.size()]));
68        }
69    }
70
71    protected final String createFilterQuery(String facetName, String value) {
72        // escape value and wrap in quotes to make literal query
73        return String.format("%s:\"%s\"", facetName, ClientUtils.escapeQueryChars(value));
74    }
75
76    /**
77     * Creates an OR filter query over the provided facet/value pairs (a query
78     * that requests all records matching ANY of the facet/value pairs)
79     *
80     * @param facetValues map with facet/value pairs that should be matched
81     * @return
82     */
83    protected final String createFilterOrQuery(Map<String, String> facetValues) {
84        // escape value and wrap in quotes to make literal query
85        final StringBuilder queryBuilder = new StringBuilder();
86        final Iterator<Map.Entry<String, String>> iterator = facetValues.entrySet().iterator();
87        while (iterator.hasNext()) {
88            final Map.Entry<String, String> facetValue = iterator.next();
89            queryBuilder.append(createFilterQuery(facetValue.getKey(), facetValue.getValue()));
90            if (iterator.hasNext()) {
91                queryBuilder.append(" OR ");
92            }
93        }
94        return queryBuilder.toString();
95    }
96
97}
Note: See TracBrowser for help on using the repository browser.