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 com.google.common.collect.ImmutableMap; |
---|
20 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
21 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
22 | import eu.clarin.cmdi.vlo.service.solr.SolrDocumentQueryFactory; |
---|
23 | import java.util.Collection; |
---|
24 | import org.apache.solr.client.solrj.SolrQuery; |
---|
25 | |
---|
26 | /** |
---|
27 | * |
---|
28 | * @author twagoo |
---|
29 | */ |
---|
30 | public class SolrDocumentQueryFactoryImpl extends AbstractSolrQueryFactory implements SolrDocumentQueryFactory { |
---|
31 | |
---|
32 | /** |
---|
33 | * Template query for new document queries |
---|
34 | */ |
---|
35 | private final SolrQuery defaultQueryTemplate; |
---|
36 | |
---|
37 | /** |
---|
38 | * |
---|
39 | * @param documentFields fields that should be included in document queries |
---|
40 | */ |
---|
41 | public SolrDocumentQueryFactoryImpl(Collection<String> documentFields) { |
---|
42 | defaultQueryTemplate = new SolrQuery(); |
---|
43 | defaultQueryTemplate.setFields(documentFields.toArray(new String[]{})); |
---|
44 | // //TODO: qf (all fields with weights - make configurable (later) |
---|
45 | // defaultQueryTemplate.setParam(DisMaxParams.QF, "name^20 description^10"); |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | public SolrQuery createDocumentQuery(QueryFacetsSelection selection, int first, int count) { |
---|
50 | // make a query to get all documents that match the selection criteria |
---|
51 | final SolrQuery query = getDefaultDocumentQuery(); |
---|
52 | // apply selection |
---|
53 | addQueryFacetParameters(query, selection); |
---|
54 | // set offset and limit |
---|
55 | query.setStart(first); |
---|
56 | query.setRows(count); |
---|
57 | return query; |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public SolrQuery createDocumentQuery(String docId) { |
---|
62 | // make a query to look up a specific document by its ID |
---|
63 | final SolrQuery query = getDefaultDocumentQuery(); |
---|
64 | // consider all documents |
---|
65 | query.setQuery(SOLR_SEARCH_ALL); |
---|
66 | // filter by ID |
---|
67 | // check for ID value in both 'id' and 'self link' fields, both ought to |
---|
68 | // be unique and self link use to be ID in old VLO, so this should keep old |
---|
69 | // URL's valid with a minimal likelihood of clashes |
---|
70 | final ImmutableMap<String, String> idOrQueryMap = ImmutableMap.<String, String>builder() |
---|
71 | .put(FacetConstants.FIELD_ID, docId) |
---|
72 | .put(FacetConstants.FIELD_SELF_LINK, docId) |
---|
73 | .build(); |
---|
74 | query.addFilterQuery(createFilterOrQuery(idOrQueryMap)); |
---|
75 | |
---|
76 | // one result max |
---|
77 | query.setRows(1); |
---|
78 | return query; |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | private SolrQuery getDefaultDocumentQuery() { |
---|
83 | return defaultQueryTemplate.getCopy(); |
---|
84 | } |
---|
85 | } |
---|