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