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