Revision 48900
Added by Claudio Atzori over 6 years ago
SolrIndexClient.java | ||
---|---|---|
6 | 6 |
import java.util.List; |
7 | 7 |
import java.util.Map; |
8 | 8 |
|
9 |
import eu.dnetlib.functionality.cql.CqlValueTransformerMap; |
|
10 |
import org.apache.commons.logging.Log; |
|
11 |
import org.apache.commons.logging.LogFactory; |
|
12 |
import org.apache.solr.client.solrj.SolrServer; |
|
13 |
import org.apache.solr.client.solrj.SolrServerException; |
|
14 |
import org.apache.solr.client.solrj.impl.CloudSolrServer; |
|
15 |
import org.apache.solr.client.solrj.request.LukeRequest; |
|
16 |
import org.apache.solr.client.solrj.response.LukeResponse; |
|
17 |
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo; |
|
18 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
19 |
|
|
20 | 9 |
import com.google.common.collect.BiMap; |
21 | 10 |
import com.google.common.collect.Maps; |
22 |
|
|
23 | 11 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
24 | 12 |
import eu.dnetlib.data.provision.index.rmi.GroupResult; |
25 | 13 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
14 |
import eu.dnetlib.functionality.cql.CqlValueTransformerMap; |
|
26 | 15 |
import eu.dnetlib.functionality.index.client.AbstractIndexClient; |
27 | 16 |
import eu.dnetlib.functionality.index.client.IndexClient; |
28 | 17 |
import eu.dnetlib.functionality.index.client.IndexClientException; |
29 | 18 |
import eu.dnetlib.functionality.index.client.response.BrowseEntry; |
30 | 19 |
import eu.dnetlib.functionality.index.client.response.BrowseValueEntry; |
31 | 20 |
import eu.dnetlib.functionality.index.client.response.LookupResponse; |
32 |
|
|
33 | 21 |
import eu.dnetlib.functionality.index.model.Any.ValueType; |
34 |
import eu.dnetlib.functionality.index.query.IndexQueryFactory; |
|
35 |
import eu.dnetlib.functionality.index.query.QueryLanguage; |
|
36 |
import eu.dnetlib.functionality.index.query.QueryResponseFactory; |
|
37 |
import eu.dnetlib.functionality.index.query.QueryResponseParser; |
|
38 |
import eu.dnetlib.functionality.index.query.SolrIndexQuery; |
|
39 |
import eu.dnetlib.functionality.index.query.SolrIndexQueryFactory; |
|
40 |
import eu.dnetlib.functionality.index.query.SolrIndexQueryResponse; |
|
22 |
import eu.dnetlib.functionality.index.query.*; |
|
41 | 23 |
import eu.dnetlib.functionality.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory; |
42 | 24 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
25 |
import org.apache.commons.logging.Log; |
|
26 |
import org.apache.commons.logging.LogFactory; |
|
27 |
import org.apache.solr.client.solrj.SolrClient; |
|
28 |
import org.apache.solr.client.solrj.SolrServerException; |
|
29 |
import org.apache.solr.client.solrj.impl.CloudSolrClient; |
|
30 |
import org.apache.solr.client.solrj.request.LukeRequest; |
|
31 |
import org.apache.solr.client.solrj.response.LukeResponse; |
|
32 |
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo; |
|
33 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
43 | 34 |
|
44 | 35 |
/** |
45 | 36 |
* The Class SolrIndexClient. |
46 | 37 |
*/ |
47 | 38 |
public class SolrIndexClient extends AbstractIndexClient implements IndexClient { |
48 | 39 |
|
49 |
/** The server. */
|
|
50 |
private CloudSolrServer server;
|
|
40 |
/** The client. */
|
|
41 |
private CloudSolrClient client;
|
|
51 | 42 |
|
52 | 43 |
private SolrIndexQueryFactory solrIndexQueryFactory; |
53 | 44 |
|
... | ... | |
101 | 92 |
String tquery = translatedQuery.getQuery(); |
102 | 93 |
translatedQuery.setQueryLimit(0); |
103 | 94 |
|
104 |
SolrIndexQueryResponse rsp = new SolrIndexQueryResponse(server.query(translatedQuery));
|
|
95 |
SolrIndexQueryResponse rsp = new SolrIndexQueryResponse(client.query(translatedQuery));
|
|
105 | 96 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(rsp, mdRef); |
106 | 97 |
long total = responseParser.getNumFound(); |
107 |
server.deleteByQuery(tquery);
|
|
108 |
server.commit();
|
|
98 |
client.deleteByQuery(tquery);
|
|
99 |
client.commit();
|
|
109 | 100 |
return total; |
110 | 101 |
} catch (Exception e) { |
111 | 102 |
throw new IndexClientException("unable to run delete by query: " + query, e); |
... | ... | |
174 | 165 |
final MetadataReference mdRef, |
175 | 166 |
final List<String> browseFields) throws IndexClientException { |
176 | 167 |
try { |
177 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(server.query(query));
|
|
168 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(client.query(query));
|
|
178 | 169 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(response, mdRef); |
179 | 170 |
List<BrowsingRow> results = responseParser.getBrowsingResults(); |
180 | 171 |
List<BrowseEntry> out = convertBrowseEntry(browseFields, results, responseParser.getAliases()); |
181 | 172 |
return out; |
182 |
} catch (SolrServerException e) { |
|
173 |
} catch (SolrServerException | IOException e) {
|
|
183 | 174 |
throw new IndexClientException("Error on executing a query " + originalQuery, e); |
184 | 175 |
} |
185 | 176 |
} |
... | ... | |
198 | 189 |
} |
199 | 190 |
|
200 | 191 |
/** |
201 |
* Gets the server.
|
|
192 |
* Gets the client.
|
|
202 | 193 |
* |
203 |
* @return the server
|
|
194 |
* @return the client
|
|
204 | 195 |
* @throws IndexClientException |
205 | 196 |
* the index client exception |
206 | 197 |
*/ |
207 |
public SolrServer getServer() throws IndexClientException {
|
|
208 |
if (this.server == null) {
|
|
198 |
public SolrClient getClient() throws IndexClientException {
|
|
199 |
if (this.client == null) {
|
|
209 | 200 |
String url = getUrl(); |
210 | 201 |
log.debug("create new Client " + url); |
211 |
server = new CloudSolrServer(url); |
|
212 |
server.setDefaultCollection(String.format("%s-%s-%s", getFormat(), getLayout(), getInterpretation())); |
|
202 |
client = new CloudSolrClient.Builder() |
|
203 |
.withZkHost(url) |
|
204 |
.build(); |
|
205 |
client.setDefaultCollection(String.format("%s-%s-%s", getFormat(), getLayout(), getInterpretation())); |
|
213 | 206 |
try { |
214 |
server.ping();
|
|
207 |
client.ping();
|
|
215 | 208 |
} catch (Exception e) { |
216 | 209 |
throw new IndexClientException("oops something went wrong", e); |
217 | 210 |
} |
218 | 211 |
} |
219 |
return server;
|
|
212 |
return client;
|
|
220 | 213 |
} |
221 | 214 |
|
222 | 215 |
/** |
223 |
* Sets the server.
|
|
216 |
* Sets the client.
|
|
224 | 217 |
* |
225 |
* @param server
|
|
226 |
* the server
|
|
218 |
* @param client
|
|
219 |
* the client
|
|
227 | 220 |
*/ |
228 |
public void setServer(final CloudSolrServer server) {
|
|
229 |
this.server = server;
|
|
221 |
public void setClient(final CloudSolrClient client) {
|
|
222 |
this.client = client;
|
|
230 | 223 |
} |
231 | 224 |
|
232 | 225 |
@Override |
... | ... | |
243 | 236 |
} |
244 | 237 |
|
245 | 238 |
try { |
246 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(server.query(translatedQuery));
|
|
239 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(client.query(translatedQuery));
|
|
247 | 240 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(response, mdRef); |
248 | 241 |
|
249 | 242 |
return new LookupResponse(responseParser); |
250 |
} catch (SolrServerException e) { |
|
243 |
} catch (SolrServerException | IOException e) {
|
|
251 | 244 |
throw new IndexClientException("Error on executing a query " + query, e); |
252 | 245 |
} |
253 | 246 |
|
... | ... | |
272 | 265 |
public void stop() throws IndexClientException { |
273 | 266 |
try { |
274 | 267 |
log.debug("shutdown client: " + serviceProperties.get(ZK_ADDRESS)); |
275 |
server.shutdown();
|
|
268 |
client.close();
|
|
276 | 269 |
} catch (Throwable e) { |
277 | 270 |
throw new IndexClientException(e); |
278 | 271 |
} |
... | ... | |
326 | 319 |
} |
327 | 320 |
|
328 | 321 |
private Map<String, ValueType> readFieldNamesAndTypes(final String coreName) throws SolrServerException, IOException, IndexClientException { |
329 |
// final SolrServer server = cloudServer.getSolrServer(coreName);
|
|
322 |
// final SolrServer client = cloudServer.getSolrServer(coreName);
|
|
330 | 323 |
final LukeRequest request = new LukeRequest(); |
331 | 324 |
request.setShowSchema(true); |
332 | 325 |
|
333 | 326 |
// cloudServer.setDefaultCollection(coreName); |
334 | 327 |
request.setNumTerms(0); |
335 |
final LukeResponse response = request.process(getServer());
|
|
328 |
final LukeResponse response = request.process(getClient());
|
|
336 | 329 |
final Map<String, FieldInfo> fieldInfos = response.getFieldInfo(); |
337 | 330 |
final Map<String, LukeResponse.FieldTypeInfo> fieldTypeInfos = response.getFieldTypeInfo(); |
338 | 331 |
final Map<String, ValueType> result = Maps.newHashMap(); |
Also available in: Unified diff
upgraded solr version to 6.6.0