source: vlo/branches/vlo-3.3-oeaw/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/solr/impl/AbstractSolrQueryFactory.java @ 6478

Last change on this file since 6478 was 6478, checked in by davor.ostojic@oeaw.ac.at, 9 years ago

reduce number of solr requests

File size: 4.9 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 = 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                            encodedQueries.add(filterExpr);
67                            break;
68                        case NOT:
69                                for (String value : selection.getValues()) {
70                                        encodedQueries.add(createFilterQuery(facetName, value));
71                                }
72                                break;                         
73                        default:
74                            //TODO: support OR,NOT
75                            throw new UnsupportedOperationException("Unsupported selection type: " + selection.getSelectionType());
76                    }
77                }
78            }
79            if(includeFilterQueries)
80                query.setFilterQueries(encodedQueries.toArray(new String[encodedQueries.size()]));
81        }
82    }
83
84    protected final String createFilterQuery(String facetName, String value) {
85        // escape value and wrap in quotes to make literal query
86        return String.format("%s:\"%s\"", facetName, ClientUtils.escapeQueryChars(value));
87    }
88
89    /**
90     * Creates an OR filter query over the provided facet/value pairs (a query
91     * that requests all records matching ANY of the facet/value pairs)
92     *
93     * @param facetValues map with facet/value pairs that should be matched
94     * @return
95     */
96    protected final String createFilterOrQuery(Map<String, String> facetValues) {
97        // escape value and wrap in quotes to make literal query
98        final StringBuilder queryBuilder = new StringBuilder();
99        final Iterator<Map.Entry<String, String>> iterator = facetValues.entrySet().iterator();
100        while (iterator.hasNext()) {
101            final Map.Entry<String, String> facetValue = iterator.next();
102            queryBuilder.append(createFilterQuery(facetValue.getKey(), facetValue.getValue()));
103            if (iterator.hasNext()) {
104                queryBuilder.append(" OR ");
105            }
106        }
107        return queryBuilder.toString();
108    }
109
110}
Note: See TracBrowser for help on using the repository browser.