1
|
package eu.dnetlib.index.query;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import eu.dnetlib.clients.index.query.IndexQuery;
|
7
|
import eu.dnetlib.clients.index.utils.IndexFieldUtility;
|
8
|
import eu.dnetlib.cql.lucene.QueryOptions;
|
9
|
import eu.dnetlib.cql.lucene.TranslatedQuery;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.apache.solr.client.solrj.SolrQuery;
|
13
|
import org.apache.solr.common.params.ModifiableSolrParams;
|
14
|
import org.apache.solr.common.params.SolrParams;
|
15
|
|
16
|
/**
|
17
|
* The Class SolrIndexQuery.
|
18
|
*
|
19
|
* @author claudio, sandro
|
20
|
*/
|
21
|
public class SolrIndexQuery extends SolrQuery implements IndexQuery {
|
22
|
|
23
|
/**
|
24
|
* The Constant serialVersionUID.
|
25
|
*/
|
26
|
private static final long serialVersionUID = 1L;
|
27
|
|
28
|
/**
|
29
|
* logger.
|
30
|
*/
|
31
|
private static final Log log = LogFactory.getLog(SolrIndexQuery.class);
|
32
|
|
33
|
/**
|
34
|
* Instantiates a new solr index query.
|
35
|
*
|
36
|
* @param query the query
|
37
|
* @param options the options
|
38
|
*/
|
39
|
public SolrIndexQuery(final TranslatedQuery query, final Map<String, List<String>> options) {
|
40
|
this(query.asLucene(), options);
|
41
|
|
42
|
setCqlParams(query.getOptions());
|
43
|
|
44
|
log.debug("internal solr query: " + this.toString());
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Instantiates a new solr index query.
|
49
|
*
|
50
|
* @param query the query
|
51
|
* @param options the options
|
52
|
*/
|
53
|
public SolrIndexQuery(final String query, final Map<String, List<String>> options) {
|
54
|
this(query);
|
55
|
|
56
|
// TODO verify that the input options belongs to solr
|
57
|
super.add(getQueryParams(options));
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Instantiates a new solr index query.
|
62
|
*
|
63
|
* @param query the query
|
64
|
*/
|
65
|
public SolrIndexQuery(final String query) {
|
66
|
super(query);
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public IndexQuery setQueryOffset(final int offset) {
|
71
|
super.setStart(offset);
|
72
|
return this;
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public IndexQuery setQueryLimit(final int limit) {
|
77
|
super.setRows(limit);
|
78
|
return this;
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public IndexQuery setQueryFacetLimit(final int limit) {
|
83
|
super.setFacetLimit(limit);
|
84
|
return this;
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
public IndexQuery setQueryFacetOffset(final int offset) {
|
89
|
super.set("facet.offset", offset);
|
90
|
return this;
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
public IndexQuery setDSIdFilters(final List<String> dsIds) {
|
95
|
if (!isAll(dsIds)) {
|
96
|
for (String id : dsIds) {
|
97
|
addFilterQuery(IndexFieldUtility.DS_ID + ":\"" + id + "\"");
|
98
|
}
|
99
|
}
|
100
|
return this;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Checks if is all.
|
105
|
*
|
106
|
* @param dsIds the ds id
|
107
|
* @return true, if is all
|
108
|
*/
|
109
|
protected boolean isAll(final List<String> dsIds) {
|
110
|
return (dsIds != null) && (!dsIds.isEmpty()) && (dsIds.size() == 1) && dsIds.get(0).equalsIgnoreCase(IndexFieldUtility.INDEX_DSID_ALL);
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Convert our option map to a solr option parameter map.
|
115
|
*
|
116
|
* @param options input paramter map.
|
117
|
* @return solr option parameter map.
|
118
|
*/
|
119
|
private SolrParams getQueryParams(final Map<String, List<String>> options) {
|
120
|
ModifiableSolrParams params = new ModifiableSolrParams();
|
121
|
String[] typeTag = new String[] {};
|
122
|
|
123
|
for (Map.Entry<String, List<String>> entry : options.entrySet()) {
|
124
|
params.add(entry.getKey(), entry.getValue().toArray(typeTag));
|
125
|
}
|
126
|
return params;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Sets the query options.
|
131
|
*
|
132
|
* @param options the options.
|
133
|
*/
|
134
|
private void setCqlParams(final QueryOptions options) {
|
135
|
if (options != null) {
|
136
|
if (options.getSort() != null) {
|
137
|
super.addSort(options.getSort().getField(), ORDER.valueOf(options.getSort().getMode().name()));
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
|
142
|
}
|