source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/AbstractSolrQueryFactory.java @ 4566

Last change on this file since 4566 was 4566, checked in by twagoo, 10 years ago

separated document query factory from facet query factory (both in interface and implementation)

File size: 2.3 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.impl;
18
19import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
20import java.util.ArrayList;
21import java.util.Collection;
22import java.util.List;
23import java.util.Map;
24import org.apache.solr.client.solrj.SolrQuery;
25import org.apache.solr.client.solrj.util.ClientUtils;
26
27/**
28 *
29 * @author twagoo
30 */
31public 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}
Note: See TracBrowser for help on using the repository browser.