1
|
package eu.dnetlib.functionality.index.client.solr;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.text.SimpleDateFormat;
|
5
|
import java.util.*;
|
6
|
|
7
|
import com.google.common.collect.BiMap;
|
8
|
import com.google.common.collect.Maps;
|
9
|
import eu.dnetlib.data.provision.index.rmi.BrowsingRow;
|
10
|
import eu.dnetlib.data.provision.index.rmi.GroupResult;
|
11
|
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
|
12
|
import eu.dnetlib.functionality.cql.CqlValueTransformerMap;
|
13
|
import eu.dnetlib.functionality.index.client.IndexClient;
|
14
|
import eu.dnetlib.functionality.index.client.IndexClientException;
|
15
|
import eu.dnetlib.functionality.index.client.response.BrowseEntry;
|
16
|
import eu.dnetlib.functionality.index.client.response.BrowseValueEntry;
|
17
|
import eu.dnetlib.functionality.index.client.response.LookupResponse;
|
18
|
import eu.dnetlib.functionality.index.model.Any.ValueType;
|
19
|
import eu.dnetlib.functionality.index.query.*;
|
20
|
import eu.dnetlib.functionality.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory;
|
21
|
import eu.dnetlib.functionality.index.solr.feed.StreamingInputDocumentFactory;
|
22
|
import eu.dnetlib.functionality.index.utils.MetadataReference;
|
23
|
import eu.dnetlib.miscutils.datetime.HumanTime;
|
24
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
25
|
import org.apache.commons.lang3.StringUtils;
|
26
|
import org.apache.commons.logging.Log;
|
27
|
import org.apache.commons.logging.LogFactory;
|
28
|
import org.apache.solr.client.solrj.SolrClient;
|
29
|
import org.apache.solr.client.solrj.SolrQuery;
|
30
|
import org.apache.solr.client.solrj.SolrServerException;
|
31
|
import org.apache.solr.client.solrj.impl.CloudSolrClient;
|
32
|
import org.apache.solr.client.solrj.request.LukeRequest;
|
33
|
import org.apache.solr.client.solrj.response.LukeResponse;
|
34
|
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo;
|
35
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
36
|
import org.apache.solr.client.solrj.response.UpdateResponse;
|
37
|
import org.apache.solr.common.SolrInputDocument;
|
38
|
|
39
|
/**
|
40
|
* The Class SolrIndexClient.
|
41
|
*/
|
42
|
public class SolrIndexClient implements IndexClient {
|
43
|
|
44
|
private static final Log log = LogFactory.getLog(SolrIndexClient.class);
|
45
|
|
46
|
private static final String INDEX_RECORD_RESULT_FIELD = "dnetResult";
|
47
|
|
48
|
private static String ZK_ADDRESS = "address";
|
49
|
|
50
|
/** The format. */
|
51
|
private String format;
|
52
|
|
53
|
/** The layout. */
|
54
|
private String layout;
|
55
|
|
56
|
/** The interpretation. */
|
57
|
private String interpretation;
|
58
|
|
59
|
protected Map<String, String> serviceProperties;
|
60
|
|
61
|
/** The client. */
|
62
|
private CloudSolrClient client;
|
63
|
|
64
|
private SolrIndexQueryFactory solrIndexQueryFactory;
|
65
|
|
66
|
/** The query response factory. */
|
67
|
private QueryResponseFactory<QueryResponse> queryResponseFactory;
|
68
|
|
69
|
private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory;
|
70
|
|
71
|
/**
|
72
|
* The Constructor.
|
73
|
*
|
74
|
* @param format
|
75
|
* the format
|
76
|
* @param layout
|
77
|
* the layout
|
78
|
* @param interpretation
|
79
|
* the interpretation
|
80
|
* @param serviceProperties
|
81
|
* the service properties
|
82
|
* @param tMapFactory
|
83
|
*/
|
84
|
public SolrIndexClient(final String format, final String layout, final String interpretation, final Map<String, String> serviceProperties,
|
85
|
final SolrIndexQueryFactory indexQueryFactory, final QueryResponseFactory<QueryResponse> queryResponseFactory,
|
86
|
final SolrTypeBasedCqlValueTransformerMapFactory tMapFactory) {
|
87
|
this.format = format;
|
88
|
this.layout = layout;
|
89
|
this.interpretation = interpretation;
|
90
|
this.serviceProperties = serviceProperties;
|
91
|
this.solrIndexQueryFactory = indexQueryFactory;
|
92
|
this.queryResponseFactory = queryResponseFactory;
|
93
|
this.tMapFactory = tMapFactory;
|
94
|
|
95
|
log.debug(String.format("Created a new instance of the index of type %s-%s-%s", format, layout, interpretation));
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Do delete.
|
100
|
*
|
101
|
* @param query
|
102
|
* the CQL query
|
103
|
* @return true, if do delete
|
104
|
* @throws IndexServiceException
|
105
|
* the index service exception
|
106
|
*/
|
107
|
@Override
|
108
|
public long delete(final String query) throws IndexClientException {
|
109
|
try {
|
110
|
log.debug("delete by query: " + query);
|
111
|
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation());
|
112
|
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef);
|
113
|
String tquery = translatedQuery.getQuery();
|
114
|
translatedQuery.setQueryLimit(0);
|
115
|
|
116
|
SolrIndexQueryResponse rsp = new SolrIndexQueryResponse(client.query(translatedQuery));
|
117
|
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(rsp, mdRef);
|
118
|
long total = responseParser.getNumFound();
|
119
|
client.deleteByQuery(tquery);
|
120
|
client.commit();
|
121
|
return total;
|
122
|
} catch (Exception e) {
|
123
|
throw new IndexClientException("unable to run delete by query: " + query, e);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* {@inheritDoc}
|
129
|
*
|
130
|
* @throws IndexClientException
|
131
|
*
|
132
|
* @see IndexClient#browse(String, List, int)
|
133
|
*/
|
134
|
@Override
|
135
|
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max) throws IndexClientException {
|
136
|
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation());
|
137
|
SolrIndexQuery translatedQuery = buildBrowseQuery(query, browseFields, max, mdRef);
|
138
|
return executeBrowseQuery(query, translatedQuery, mdRef, browseFields);
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* {@inheritDoc}
|
143
|
*
|
144
|
* @throws IndexClientException
|
145
|
*
|
146
|
* @see IndexClient#browse(String, List, int, List)
|
147
|
*/
|
148
|
@Override
|
149
|
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max, final List<String> filterQuery)
|
150
|
throws IndexClientException {
|
151
|
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation());
|
152
|
SolrIndexQuery translatedQuery = buildBrowseQuery(query, browseFields, max, mdRef);
|
153
|
if (filterQuery != null) {
|
154
|
log.debug("Filter Query:");
|
155
|
for (String fq : filterQuery) {
|
156
|
translatedQuery.addFilterQuery(fq);
|
157
|
log.debug("- " + fq);
|
158
|
}
|
159
|
}
|
160
|
return executeBrowseQuery(query, translatedQuery, mdRef, browseFields);
|
161
|
|
162
|
}
|
163
|
|
164
|
private SolrIndexQuery buildBrowseQuery(final String query, final List<String> browseFields, final int max, final MetadataReference mdRef)
|
165
|
throws IndexClientException {
|
166
|
log.debug("Browse request for the index collection for query:" + query);
|
167
|
|
168
|
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef);
|
169
|
translatedQuery.setFacet(true);
|
170
|
if (browseFields != null) {
|
171
|
List<String> browsableFields = solrIndexQueryFactory.getBrowsableFields(browseFields, mdRef);
|
172
|
log.debug("Browsing fields:");
|
173
|
for (String field : browsableFields) {
|
174
|
translatedQuery.addFacetField(field);
|
175
|
log.debug("- " + field);
|
176
|
|
177
|
}
|
178
|
translatedQuery.setFacetLimit(max);
|
179
|
log.debug("max number of browsing field :" + max);
|
180
|
}
|
181
|
return translatedQuery;
|
182
|
}
|
183
|
|
184
|
private List<BrowseEntry> executeBrowseQuery(final String originalQuery,
|
185
|
final SolrIndexQuery query,
|
186
|
final MetadataReference mdRef,
|
187
|
final List<String> browseFields) throws IndexClientException {
|
188
|
try {
|
189
|
SolrIndexQueryResponse response = new SolrIndexQueryResponse(client.query(query));
|
190
|
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(response, mdRef);
|
191
|
List<BrowsingRow> results = responseParser.getBrowsingResults();
|
192
|
List<BrowseEntry> out = convertBrowseEntry(browseFields, results, responseParser.getAliases());
|
193
|
return out;
|
194
|
} catch (SolrServerException | IOException e) {
|
195
|
throw new IndexClientException("Error on executing a query " + originalQuery, e);
|
196
|
}
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* Creates the connection.
|
201
|
*
|
202
|
* @return the string
|
203
|
* @throws IndexClientException
|
204
|
* the index client exception
|
205
|
*/
|
206
|
private String getUrl() throws IndexClientException {
|
207
|
String address = serviceProperties.get(ZK_ADDRESS);
|
208
|
if (StringUtils.isBlank(address)) {
|
209
|
throw new IndexClientException("Unable to load a solr client, missing zk address");
|
210
|
}
|
211
|
return address;
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Gets the client.
|
216
|
*
|
217
|
* @return the client
|
218
|
* @throws IndexClientException
|
219
|
* the index client exception
|
220
|
*/
|
221
|
public SolrClient getClient() throws IndexClientException {
|
222
|
if (this.client == null) {
|
223
|
String url = getUrl();
|
224
|
log.debug("create new Client " + url);
|
225
|
client = new CloudSolrClient.Builder().withZkHost(url).build();
|
226
|
client.setDefaultCollection(String.format("%s-%s-%s", getFormat(), getLayout(), getInterpretation()));
|
227
|
try {
|
228
|
client.ping();
|
229
|
} catch (Exception e) {
|
230
|
throw new IndexClientException("oops something went wrong", e);
|
231
|
}
|
232
|
}
|
233
|
return client;
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Sets the client.
|
238
|
*
|
239
|
* @param client
|
240
|
* the client
|
241
|
*/
|
242
|
public void setClient(final CloudSolrClient client) {
|
243
|
this.client = client;
|
244
|
}
|
245
|
|
246
|
@Override
|
247
|
public LookupResponse lookup(final String query, final List<String> filterQuery, final int from, final int to) throws IndexClientException {
|
248
|
log.debug("lookup request for the index collection for query:" + query);
|
249
|
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation());
|
250
|
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef);
|
251
|
translatedQuery.setQueryOffset(from);
|
252
|
translatedQuery.setQueryLimit(to - from + 1);
|
253
|
if (filterQuery != null) {
|
254
|
for (String fq : filterQuery) {
|
255
|
translatedQuery.addFilterQuery(fq);
|
256
|
}
|
257
|
}
|
258
|
|
259
|
try {
|
260
|
SolrIndexQueryResponse response = new SolrIndexQueryResponse(client.query(translatedQuery));
|
261
|
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(response, mdRef);
|
262
|
|
263
|
return new LookupResponse(responseParser);
|
264
|
} catch (SolrServerException | IOException e) {
|
265
|
throw new IndexClientException("Error on executing a query " + query, e);
|
266
|
}
|
267
|
|
268
|
}
|
269
|
|
270
|
@Override
|
271
|
public CqlValueTransformerMap getCqlValueTransformerMap(final MetadataReference mdRef) throws IndexClientException {
|
272
|
try {
|
273
|
return tMapFactory.getIt(readFieldNamesAndTypes());
|
274
|
} catch (Exception e) {
|
275
|
throw new IndexClientException(e);
|
276
|
}
|
277
|
}
|
278
|
|
279
|
@Override
|
280
|
public IndexQueryFactory getIndexQueryFactory() {
|
281
|
return solrIndexQueryFactory;
|
282
|
}
|
283
|
|
284
|
@Override
|
285
|
public void close() throws IOException {
|
286
|
log.debug("shutdown client: " + serviceProperties.get(ZK_ADDRESS));
|
287
|
client.close();
|
288
|
}
|
289
|
|
290
|
public int feed(final String record, final String indexDsId, final UnaryFunction<String, String> toIndexRecord) throws IndexClientException {
|
291
|
return feed(record, indexDsId, toIndexRecord, true);
|
292
|
}
|
293
|
|
294
|
public int feed(final String record, final String indexDsId, final UnaryFunction<String, String> toIndexRecord, final boolean commit)
|
295
|
throws IndexClientException {
|
296
|
try {
|
297
|
final SolrInputDocument doc = prepareSolrDocument(record, indexDsId, toIndexRecord);
|
298
|
if ((doc == null) || doc.isEmpty()) throw new IndexClientException("Invalid solr document");
|
299
|
return feed(doc, commit);
|
300
|
} catch (final Throwable e) {
|
301
|
throw new IndexClientException("Error feeding document", e);
|
302
|
}
|
303
|
}
|
304
|
|
305
|
public int feed(final SolrInputDocument document) throws IndexClientException {
|
306
|
return feed(document, true);
|
307
|
}
|
308
|
|
309
|
public int feed(final List<SolrInputDocument> document) throws IndexClientException {
|
310
|
try {
|
311
|
final UpdateResponse res = client.add(document);
|
312
|
log.debug("feed time for single records, elapsed time: " + HumanTime.exactly(res.getElapsedTime()));
|
313
|
if (res.getStatus() != 0) { throw new IndexClientException("bad status: " + res.getStatus()); }
|
314
|
return res.getStatus();
|
315
|
} catch (final Throwable e) {
|
316
|
throw new IndexClientException("Error feeding document", e);
|
317
|
}
|
318
|
}
|
319
|
|
320
|
public int feed(final SolrInputDocument document, final boolean commit) throws IndexClientException {
|
321
|
try {
|
322
|
final UpdateResponse res = client.add(document);
|
323
|
log.debug("feed time for single records, elapsed time: " + HumanTime.exactly(res.getElapsedTime()));
|
324
|
if (res.getStatus() != 0) { throw new IndexClientException("bad status: " + res.getStatus()); }
|
325
|
if (commit) {
|
326
|
client.commit();
|
327
|
}
|
328
|
return res.getStatus();
|
329
|
} catch (final Throwable e) {
|
330
|
throw new IndexClientException("Error feeding document", e);
|
331
|
}
|
332
|
}
|
333
|
|
334
|
public void feed(final List<SolrInputDocument> docs, final AfterFeedingCallback callback) throws IndexClientException {
|
335
|
feed(docs, callback, true);
|
336
|
}
|
337
|
|
338
|
public void feed(final List<SolrInputDocument> docs, final AfterFeedingCallback callback, final boolean commit) throws IndexClientException {
|
339
|
try {
|
340
|
if (docs.isEmpty()) {
|
341
|
log.debug("Empty list of documents. Calling callback, if needed.");
|
342
|
if (callback != null) {
|
343
|
callback.doAfterFeeding(null);
|
344
|
}
|
345
|
return;
|
346
|
}
|
347
|
final UpdateResponse res = client.add(docs);
|
348
|
|
349
|
log.debug("feed time for " + docs.size() + " records, elapsed tipe: : " + HumanTime.exactly(res.getElapsedTime()));
|
350
|
|
351
|
if (commit) {
|
352
|
client.commit();
|
353
|
}
|
354
|
if (callback != null) {
|
355
|
callback.doAfterFeeding(res);
|
356
|
}
|
357
|
if (res.getStatus() != 0) throw new IndexClientException("bad status: " + res.getStatus());
|
358
|
} catch (final Throwable e) {
|
359
|
throw new IndexClientException("Error feeding documents", e);
|
360
|
}
|
361
|
}
|
362
|
|
363
|
public SolrInputDocument prepareSolrDocument(final String record, final String indexDsId, final UnaryFunction<String, String> toIndexRecord)
|
364
|
throws IndexClientException {
|
365
|
try {
|
366
|
final StreamingInputDocumentFactory documentFactory = new StreamingInputDocumentFactory();
|
367
|
|
368
|
final String version = (new SimpleDateFormat("yyyy-MM-dd\'T\'hh:mm:ss\'Z\'")).format(new Date());
|
369
|
final String indexRecord = toIndexRecord.evaluate(record);
|
370
|
|
371
|
if (log.isDebugEnabled()) {
|
372
|
log.debug("***************************************\nSubmitting index record:\n" + indexRecord + "\n***************************************\n");
|
373
|
}
|
374
|
|
375
|
return documentFactory.parseDocument(version, indexRecord, indexDsId, INDEX_RECORD_RESULT_FIELD);
|
376
|
} catch (final Throwable e) {
|
377
|
throw new IndexClientException("Error creating solr document", e);
|
378
|
}
|
379
|
}
|
380
|
|
381
|
public boolean isRecordIndexed(final String id) throws IndexClientException {
|
382
|
final QueryResponse res = query("objidentifier:\"" + id + "\"", null);
|
383
|
return res.getResults().size() > 0;
|
384
|
}
|
385
|
|
386
|
public int remove(final String id) throws IndexClientException {
|
387
|
return remove(id, true);
|
388
|
}
|
389
|
|
390
|
public UpdateResponse commit() throws IndexClientException {
|
391
|
try {
|
392
|
return client.commit();
|
393
|
} catch (SolrServerException | IOException e) {
|
394
|
throw new IndexClientException(e);
|
395
|
}
|
396
|
}
|
397
|
|
398
|
public int remove(final String id, final boolean commit) throws IndexClientException {
|
399
|
try {
|
400
|
final UpdateResponse res = client.deleteByQuery("objidentifier:\"" + id + "\"");
|
401
|
if (commit) {
|
402
|
client.commit();
|
403
|
}
|
404
|
return res.getResponse().size();
|
405
|
} catch (final Throwable e) {
|
406
|
throw new IndexClientException("Error removing documents", e);
|
407
|
}
|
408
|
}
|
409
|
|
410
|
public int count(final String query) throws IndexClientException {
|
411
|
final QueryResponse res = query(query, 0);
|
412
|
return res.getResults().size();
|
413
|
}
|
414
|
|
415
|
public QueryResponse query(final String query, Integer rows) throws IndexClientException {
|
416
|
try {
|
417
|
final SolrQuery solrQuery = new SolrQuery();
|
418
|
solrQuery.setQuery(query);
|
419
|
if(rows != null && rows >= 0) {
|
420
|
solrQuery.setRows(rows);
|
421
|
}
|
422
|
return client.query(solrQuery);
|
423
|
} catch (final Throwable e) {
|
424
|
throw new IndexClientException("Error searching documents", e);
|
425
|
}
|
426
|
}
|
427
|
|
428
|
public UpdateResponse deleteByQuery(final String query) throws IndexClientException {
|
429
|
try {
|
430
|
return client.deleteByQuery(query);
|
431
|
} catch (final Throwable e) {
|
432
|
throw new IndexClientException("Error searching documents", e);
|
433
|
}
|
434
|
}
|
435
|
|
436
|
public interface AfterFeedingCallback {
|
437
|
|
438
|
void doAfterFeeding(final UpdateResponse response);
|
439
|
}
|
440
|
|
441
|
/**
|
442
|
* Gets the format.
|
443
|
*
|
444
|
* @return the format
|
445
|
*/
|
446
|
public String getFormat() {
|
447
|
return format;
|
448
|
}
|
449
|
|
450
|
/**
|
451
|
* Sets the format.
|
452
|
*
|
453
|
* @param format
|
454
|
* the format
|
455
|
*/
|
456
|
public void setFormat(final String format) {
|
457
|
this.format = format;
|
458
|
}
|
459
|
|
460
|
/**
|
461
|
* Gets the layout.
|
462
|
*
|
463
|
* @return the layout
|
464
|
*/
|
465
|
public String getLayout() {
|
466
|
return layout;
|
467
|
}
|
468
|
|
469
|
/**
|
470
|
* Sets the layout.
|
471
|
*
|
472
|
* @param layout
|
473
|
* the layout
|
474
|
*/
|
475
|
public void setLayout(final String layout) {
|
476
|
this.layout = layout;
|
477
|
}
|
478
|
|
479
|
/**
|
480
|
* Gets the interpretation.
|
481
|
*
|
482
|
* @return the interpretation
|
483
|
*/
|
484
|
public String getInterpretation() {
|
485
|
return interpretation;
|
486
|
}
|
487
|
|
488
|
/**
|
489
|
* Sets the interpretation.
|
490
|
*
|
491
|
* @param interpretation
|
492
|
* the interpretation
|
493
|
*/
|
494
|
public void setInterpretation(final String interpretation) {
|
495
|
this.interpretation = interpretation;
|
496
|
}
|
497
|
|
498
|
public Map<String, String> getServiceProperties() {
|
499
|
return serviceProperties;
|
500
|
}
|
501
|
|
502
|
public void setServiceProperties(final Map<String, String> serviceProperties) {
|
503
|
this.serviceProperties = serviceProperties;
|
504
|
}
|
505
|
|
506
|
// HELPERS
|
507
|
|
508
|
private List<BrowseEntry> convertBrowseEntry(final List<String> browseFields, final List<BrowsingRow> results, final BiMap<String, String> aliases) {
|
509
|
|
510
|
Map<String, BrowseEntry> mapResult = new HashMap<String, BrowseEntry>();
|
511
|
for (BrowsingRow row : results) {
|
512
|
for (GroupResult groupResult : row.getGroupResult()) {
|
513
|
String name = groupResult.getName();
|
514
|
List<BrowseValueEntry> valuesEntry;
|
515
|
BrowseEntry entry;
|
516
|
if (mapResult.containsKey(name)) {
|
517
|
entry = mapResult.get(name);
|
518
|
valuesEntry = entry.getValues();
|
519
|
if (valuesEntry == null) {
|
520
|
valuesEntry = new ArrayList<BrowseValueEntry>();
|
521
|
entry.setValues(valuesEntry);
|
522
|
}
|
523
|
|
524
|
} else {
|
525
|
entry = new BrowseEntry();
|
526
|
entry.setField(name);
|
527
|
entry.setLabel(name);
|
528
|
valuesEntry = new ArrayList<BrowseValueEntry>();
|
529
|
entry.setValues(valuesEntry);
|
530
|
mapResult.put(name, entry);
|
531
|
}
|
532
|
String value = groupResult.getValue();
|
533
|
int count = groupResult.getCount();
|
534
|
BrowseValueEntry entryValue = new BrowseValueEntry(value, count);
|
535
|
valuesEntry.add(entryValue);
|
536
|
}
|
537
|
}
|
538
|
List<BrowseEntry> out = new ArrayList<BrowseEntry>();
|
539
|
for (String b : browseFields) {
|
540
|
String inverse = null;
|
541
|
if (aliases != null) {
|
542
|
inverse = aliases.get(b) != null ? aliases.get(b) : aliases.inverse().get(b);
|
543
|
}
|
544
|
if (mapResult.containsKey(b)) {
|
545
|
out.add(mapResult.get(b));
|
546
|
} else if (mapResult.containsKey(inverse) == true) {
|
547
|
BrowseEntry data = mapResult.get(inverse);
|
548
|
data.setField(b);
|
549
|
out.add(data);
|
550
|
}
|
551
|
}
|
552
|
return out;
|
553
|
}
|
554
|
|
555
|
private Map<String, ValueType> readFieldNamesAndTypes() throws SolrServerException, IOException, IndexClientException {
|
556
|
|
557
|
final LukeRequest request = new LukeRequest();
|
558
|
request.setShowSchema(true);
|
559
|
|
560
|
request.setNumTerms(0);
|
561
|
final LukeResponse response = request.process(getClient());
|
562
|
final Map<String, FieldInfo> fieldInfos = response.getFieldInfo();
|
563
|
final Map<String, LukeResponse.FieldTypeInfo> fieldTypeInfos = response.getFieldTypeInfo();
|
564
|
final Map<String, ValueType> result = Maps.newHashMap();
|
565
|
for (FieldInfo fieldInfo : fieldInfos.values()) {
|
566
|
LukeResponse.FieldTypeInfo fieldTypeInfo = fieldTypeInfos.get(fieldInfo.getType());
|
567
|
final String fieldName = fieldTypeInfo.getName().toLowerCase();
|
568
|
final ValueType fieldType = resolveSolrTypeClassName(fieldName);
|
569
|
result.put(fieldInfo.getName(), fieldType);
|
570
|
}
|
571
|
return result;
|
572
|
}
|
573
|
|
574
|
private ValueType resolveSolrTypeClassName(final String solrTypeName) {
|
575
|
if (solrTypeName.contains("LongField")) {
|
576
|
return ValueType.LONG;
|
577
|
} else if (solrTypeName.contains("IntField")) {
|
578
|
return ValueType.LONG;
|
579
|
} else if (solrTypeName.contains("short")) {
|
580
|
return ValueType.LONG;
|
581
|
} else if (solrTypeName.contains("float")) {
|
582
|
return ValueType.DOUBLE;
|
583
|
} else if (solrTypeName.contains("double")) {
|
584
|
return ValueType.DOUBLE;
|
585
|
} else if (solrTypeName.contains("date")) {
|
586
|
return ValueType.DATETIME;
|
587
|
} else {
|
588
|
return ValueType.STRING;
|
589
|
}
|
590
|
}
|
591
|
|
592
|
}
|