1
|
package eu.dnetlib.data.search.app;
|
2
|
|
3
|
import eu.dnetlib.api.data.IndexService;
|
4
|
import eu.dnetlib.api.data.IndexServiceException;
|
5
|
import eu.dnetlib.api.data.SearchService;
|
6
|
import eu.dnetlib.api.data.SearchServiceException;
|
7
|
import eu.dnetlib.api.enabling.ISLookUpService;
|
8
|
import eu.dnetlib.common.rmi.UnimplementedException;
|
9
|
import eu.dnetlib.data.search.app.plan.FieldRewriteRule;
|
10
|
import eu.dnetlib.data.search.app.plan.QueryRewriteRule;
|
11
|
import eu.dnetlib.data.search.transform.Transformer;
|
12
|
import eu.dnetlib.data.search.transform.TransformerException;
|
13
|
import eu.dnetlib.data.search.transform.config.SearchRegistry;
|
14
|
import eu.dnetlib.data.search.transform.formatter.Formatter;
|
15
|
import eu.dnetlib.domain.ActionType;
|
16
|
import eu.dnetlib.domain.EPR;
|
17
|
import eu.dnetlib.domain.ResourceType;
|
18
|
import eu.dnetlib.domain.data.FormattedSearchResult;
|
19
|
import eu.dnetlib.domain.data.SearchResult;
|
20
|
import eu.dnetlib.domain.data.SuggestiveResult;
|
21
|
import eu.dnetlib.domain.enabling.Notification;
|
22
|
import gr.uoa.di.driver.app.DriverServiceImpl;
|
23
|
import gr.uoa.di.driver.enabling.issn.NotificationListener;
|
24
|
import gr.uoa.di.driver.enabling.resultset.ResultSet;
|
25
|
import gr.uoa.di.driver.enabling.resultset.ResultSetFactory;
|
26
|
import gr.uoa.di.driver.util.ServiceLocator;
|
27
|
import org.apache.log4j.Logger;
|
28
|
import org.w3c.dom.Document;
|
29
|
import org.w3c.dom.Node;
|
30
|
import org.xml.sax.InputSource;
|
31
|
import org.z3950.zing.cql.CQLParseException;
|
32
|
import org.z3950.zing.cql.CQLParser;
|
33
|
|
34
|
import javax.xml.parsers.DocumentBuilder;
|
35
|
import javax.xml.parsers.DocumentBuilderFactory;
|
36
|
import javax.xml.xpath.XPath;
|
37
|
import javax.xml.xpath.XPathConstants;
|
38
|
import javax.xml.xpath.XPathExpression;
|
39
|
import javax.xml.xpath.XPathFactory;
|
40
|
import java.io.IOException;
|
41
|
import java.io.StringReader;
|
42
|
import java.util.*;
|
43
|
|
44
|
//import eu.dnetlib.utils.cql.CqlException;
|
45
|
|
46
|
public class SearchServiceImpl extends DriverServiceImpl
|
47
|
implements SearchService {
|
48
|
|
49
|
private static Logger logger = Logger.getLogger(SearchServiceImpl.class);
|
50
|
@Deprecated
|
51
|
private static Logger tlogger = Logger.getLogger("eu.dnetlib.data.search.app.Timer");
|
52
|
|
53
|
private String mdFormat = "DMF";
|
54
|
private String indexLayout = "index";
|
55
|
|
56
|
private ServiceLocator<IndexService> indexLocator = null;
|
57
|
private ServiceLocator<ISLookUpService> lookUpServiceServiceLocator = null;
|
58
|
private ResultSetFactory rsFactory = null;
|
59
|
|
60
|
private SearchRegistry transformerFactory = null;
|
61
|
private List<QueryRewriteRule> queryRules = null;
|
62
|
private Map<String, FieldRewriteRule> fieldRules = null;
|
63
|
private boolean enableBrowseCache = false;
|
64
|
|
65
|
private SearchServiceBlackboardHandler blackboardNotificationHandler = null;
|
66
|
|
67
|
private CQLParser cqlParser = null;
|
68
|
@Override
|
69
|
public void init() {
|
70
|
super.init();
|
71
|
|
72
|
String serviceId = this.getServiceEPR().getParameter("serviceId");
|
73
|
|
74
|
this.subscribe(
|
75
|
ActionType.UPDATE,
|
76
|
ResourceType.SEARCHSERVICERESOURCETYPE,
|
77
|
serviceId,
|
78
|
"RESOURCE_PROFILE/BODY/BLACKBOARD/LAST_REQUEST",
|
79
|
new NotificationListener() {
|
80
|
|
81
|
@Override
|
82
|
public void processNotification(Notification notification) {
|
83
|
blackboardNotificationHandler.notified(
|
84
|
notification.getSubscriptionId(),
|
85
|
notification.getTopic(),
|
86
|
notification.getIsId(),
|
87
|
notification.getMessage());
|
88
|
}
|
89
|
});
|
90
|
|
91
|
try {
|
92
|
String searchProfile = lookUpServiceServiceLocator.getService().getResourceProfile(serviceId);
|
93
|
if (searchProfile != null) {
|
94
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
95
|
dbf.setNamespaceAware(true);
|
96
|
DocumentBuilder db = dbf.newDocumentBuilder();
|
97
|
Document doc = db.parse(new InputSource(new StringReader(searchProfile)));
|
98
|
|
99
|
|
100
|
XPathFactory factory = XPathFactory.newInstance();
|
101
|
XPath xpath = factory.newXPath();
|
102
|
|
103
|
XPathExpression searchMdFormatExpression = xpath.compile("//SERVICE_PROPERTIES/PROPERTY[@key='mdformat']");
|
104
|
Node node = (Node) searchMdFormatExpression.evaluate(doc,XPathConstants.NODE);
|
105
|
|
106
|
if (node != null){
|
107
|
String profileMdFormat = node.getAttributes().getNamedItem("value").getTextContent();
|
108
|
if (profileMdFormat != null) {
|
109
|
logger.debug("mdformat in properties " + mdFormat );
|
110
|
logger.info("Setting mdformat to '" + profileMdFormat + "'");
|
111
|
mdFormat = profileMdFormat;
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
|
116
|
} catch (Exception e) {
|
117
|
logger.error("Fail to load search service profile with id " + serviceId + " from IS.", e);
|
118
|
}
|
119
|
|
120
|
|
121
|
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public SuggestiveResult suggestiveSearch(String query)
|
126
|
throws SearchServiceException {
|
127
|
|
128
|
throw new UnimplementedException();
|
129
|
/* logger.debug("running suggestive search for " + query);
|
130
|
|
131
|
SuggestiveResult suggestiveResult = new SuggestiveResult();
|
132
|
IndexService index = getIndexLocator().getService();
|
133
|
try {
|
134
|
Hint hint = index.suggestiveSearch("all", "query=" + text, mdFormat, "index", "SimpleRatioHeuristics");
|
135
|
String alternateTerm = hint.getAlternateTerm();
|
136
|
boolean autofollow = hint.isAutoFollowHint();
|
137
|
logger.debug("alternateTerm " + alternateTerm + " autofollow " + autofollow);
|
138
|
|
139
|
if (alternateTerm != null) {
|
140
|
suggestiveResult.setAlternativeTerm(alternateTerm);
|
141
|
suggestiveResult.setAutofollow(autofollow);
|
142
|
|
143
|
if (autofollow) {
|
144
|
logger.debug("getting epr for " + alternateTerm);
|
145
|
suggestiveResult.setEpr(search(alternateTerm));
|
146
|
|
147
|
} else {
|
148
|
logger.debug("getting epr for " + query);
|
149
|
suggestiveResult.setEpr(search(query));
|
150
|
}
|
151
|
|
152
|
} else {
|
153
|
logger.debug("getting epr for " + query);
|
154
|
suggestiveResult.setEpr(search(query));
|
155
|
}
|
156
|
|
157
|
} catch (IndexServiceException ise) {
|
158
|
logger.error("Error calling index", ise);
|
159
|
throw new SearchServiceException("Error calling index.");
|
160
|
}
|
161
|
return suggestiveResult; */
|
162
|
}
|
163
|
|
164
|
private String getCorrectLocale(String givenLocaleName){
|
165
|
String correctLocale = null;
|
166
|
if (givenLocaleName == null) {
|
167
|
return transformerFactory.getConfig().getDefaultLocale().getLanguage() + "_" + transformerFactory.getConfig().getDefaultLocale().getCountry();
|
168
|
} else {
|
169
|
correctLocale = givenLocaleName;
|
170
|
}
|
171
|
|
172
|
//get the locale
|
173
|
StringTokenizer tokenizer = new StringTokenizer(correctLocale, "_");
|
174
|
Locale requestLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
|
175
|
|
176
|
if(requestLocale == null || !transformerFactory.getConfig().getLocales().contains(requestLocale)){
|
177
|
correctLocale = transformerFactory.getConfig().getDefaultLocale().getLanguage() + "_" + transformerFactory.getConfig().getDefaultLocale().getCountry();
|
178
|
}
|
179
|
return correctLocale;
|
180
|
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public SearchResult search(String text, String transformer, String locale, int page, int size) throws SearchServiceException {
|
185
|
logger.debug("Received Query: " + text);
|
186
|
tlogger.debug("Received query " + text);
|
187
|
|
188
|
IndexService index = getIndexLocator().getService();
|
189
|
EPR epr = null;
|
190
|
String query = rewrite(text);
|
191
|
|
192
|
List<String> searchResults = null;
|
193
|
|
194
|
try {
|
195
|
long time = System.currentTimeMillis();
|
196
|
|
197
|
cqlParser = new CQLParser();
|
198
|
query = cqlParser.parse(query).toCQL();
|
199
|
|
200
|
if (logger.isDebugEnabled()) {
|
201
|
logger.debug("index lookup (all, query=" + query + ", " + mdFormat + ", index)");
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Ask index for search results
|
206
|
**/
|
207
|
|
208
|
epr = index.indexLookup("all", query, mdFormat, indexLayout);
|
209
|
|
210
|
if (logger.isDebugEnabled()) {
|
211
|
logger.debug("epr = " + epr);
|
212
|
}
|
213
|
|
214
|
time = System.currentTimeMillis() - time;
|
215
|
logger.debug("index query lasted " + time + " msec");
|
216
|
tlogger.debug("Got index response for query " + query);
|
217
|
|
218
|
} catch (IndexServiceException ise) {
|
219
|
logger.error("Error calling index.", ise);
|
220
|
throw new SearchServiceException("Error calling index.");
|
221
|
|
222
|
} catch (CQLParseException cqle) {
|
223
|
logger.error("Bad CQL query.", cqle);
|
224
|
throw new SearchServiceException("Error calling index.");
|
225
|
|
226
|
} catch (IOException ioe) {
|
227
|
logger.error("Bad CQL query.", ioe);
|
228
|
throw new SearchServiceException("Error calling index.");
|
229
|
}
|
230
|
|
231
|
if (epr == null) {
|
232
|
throw new SearchServiceException("Index returned null result set id.");
|
233
|
}
|
234
|
|
235
|
String correctLocale = getCorrectLocale(locale);
|
236
|
StringTokenizer tokenizer = new StringTokenizer(correctLocale, "_");
|
237
|
Locale requestLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
|
238
|
/**
|
239
|
* Read ResultSet for search
|
240
|
*/
|
241
|
ResultSet<String> rs = rsFactory.createResultSet(epr);
|
242
|
|
243
|
try {
|
244
|
int rsSize = rs.size();
|
245
|
int from = (page-1)*size + 1;
|
246
|
int to = (page*size < rsSize) ? page*size : rsSize;
|
247
|
|
248
|
List<String> xmls = rs.getElements(from, to);
|
249
|
|
250
|
searchResults = new ArrayList<String>();
|
251
|
Transformer tr = transformerFactory.getTransformer(transformer, requestLocale);
|
252
|
/**
|
253
|
* Transform the search records xmls
|
254
|
*/
|
255
|
if (tr != null) {
|
256
|
for (String xml:xmls) {
|
257
|
//logger.debug("xml index " + xml);
|
258
|
searchResults.add(tr.transform(xml));
|
259
|
}
|
260
|
} else { // if there is no transformer defined add the xmls as you got them
|
261
|
for (String xml:xmls) {
|
262
|
searchResults.add(xml);
|
263
|
}
|
264
|
}
|
265
|
|
266
|
} catch (TransformerException te) {
|
267
|
logger.error("Error transforming search results.", te);
|
268
|
throw new SearchServiceException("Error transforming search results.", te);
|
269
|
}
|
270
|
|
271
|
logger.debug("Search results for query "+ query + " created.");
|
272
|
logger.debug("Returned results for query " + query);
|
273
|
|
274
|
return new SearchResult(query, correctLocale, rs.size(), page, size, searchResults);
|
275
|
}
|
276
|
|
277
|
@Override
|
278
|
public SearchResult refine(String text, String transformer, String locale, Collection<String> fields) throws SearchServiceException {
|
279
|
IndexService index = getIndexLocator().getService();
|
280
|
EPR epr = null;
|
281
|
String query = rewrite(text);
|
282
|
|
283
|
List<String> browseResults = null;
|
284
|
ResultSet<String> rs = null;
|
285
|
|
286
|
/**
|
287
|
* For refine
|
288
|
*/
|
289
|
try {
|
290
|
long time = System.currentTimeMillis();
|
291
|
|
292
|
logger.debug("Refine query is: '"+query+"'");
|
293
|
|
294
|
cqlParser = new CQLParser();
|
295
|
query = cqlParser.parse(query).toCQL();
|
296
|
logger.debug("The refine query " + query);
|
297
|
|
298
|
StringBuffer buffer = new StringBuffer();
|
299
|
buffer.append("query=").append(query).append("&groupby=");
|
300
|
for (Iterator<String> iter = fields.iterator(); iter.hasNext();) {
|
301
|
String field = (String) iter.next();
|
302
|
buffer.append(field);
|
303
|
if (iter.hasNext()) {
|
304
|
buffer.append(",");
|
305
|
}
|
306
|
}
|
307
|
|
308
|
if (logger.isDebugEnabled()) {
|
309
|
logger.debug("The refine query " + query);
|
310
|
logger.debug("index refine (" + buffer.toString()
|
311
|
+ ", all, " + mdFormat + ", index)");
|
312
|
}
|
313
|
|
314
|
epr = index.getBrowsingStatistics(buffer.toString(), "all", mdFormat, indexLayout);
|
315
|
|
316
|
time = System.currentTimeMillis() - time;
|
317
|
logger.debug("index query lasted " + time + " msec");
|
318
|
|
319
|
} catch (CQLParseException cqle) {
|
320
|
logger.error("Bad CQL query.", cqle);
|
321
|
throw new SearchServiceException("Error calling index.");
|
322
|
} catch (IndexServiceException ise) {
|
323
|
logger.error("Error getting refine results.", ise);
|
324
|
throw new SearchServiceException("Error getting refine results.", ise);
|
325
|
} catch (IOException ioe) {
|
326
|
logger.error("Bad CQL query.", ioe);
|
327
|
throw new SearchServiceException("Error calling index.");
|
328
|
}
|
329
|
|
330
|
if (epr == null) {
|
331
|
throw new SearchServiceException("Index returned null result set id.");
|
332
|
}
|
333
|
|
334
|
//get the locale
|
335
|
String correctLocale = getCorrectLocale(locale);
|
336
|
StringTokenizer tokenizer = new StringTokenizer(correctLocale, "_");
|
337
|
Locale requestLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
|
338
|
|
339
|
try{
|
340
|
rs = rsFactory.createResultSet(epr);
|
341
|
|
342
|
if (logger.isDebugEnabled()) {
|
343
|
logger.debug("EPR : " + epr.getEpr());
|
344
|
}
|
345
|
|
346
|
List<String> list = rs.getElements(1, rs.size());
|
347
|
logger.debug("record list size " + list.size());
|
348
|
|
349
|
/**
|
350
|
* Transform each refine row
|
351
|
*/
|
352
|
Transformer tr = transformerFactory.getTransformer(transformer, requestLocale);
|
353
|
browseResults = new ArrayList<String>();
|
354
|
for (String row: list) {
|
355
|
browseResults.add(tr.transform(row));
|
356
|
}
|
357
|
|
358
|
} catch (TransformerException te) {
|
359
|
logger.error("Error transforming refine results.", te);
|
360
|
throw new SearchServiceException("Error transforming refine results.", te);
|
361
|
}
|
362
|
|
363
|
return new SearchResult(query, correctLocale, fields, browseResults);
|
364
|
}
|
365
|
|
366
|
@Override
|
367
|
public SearchResult searchNrefine(String text, String searchTransformer, String browseTransformer,
|
368
|
String locale, int page, int size, Collection<String> fields) throws SearchServiceException {
|
369
|
logger.info("Received Query: " + text);
|
370
|
|
371
|
IndexService index = getIndexLocator().getService();
|
372
|
EPR epr = null;
|
373
|
String query = rewrite(text);
|
374
|
|
375
|
//get the locale
|
376
|
String correctLocale = getCorrectLocale(locale);
|
377
|
StringTokenizer tokenizer = new StringTokenizer(correctLocale, "_");
|
378
|
Locale requestLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
|
379
|
|
380
|
/**
|
381
|
* search
|
382
|
* **/
|
383
|
List<String> searchResults = null;
|
384
|
|
385
|
try {
|
386
|
long time = System.currentTimeMillis();
|
387
|
|
388
|
cqlParser = new CQLParser();
|
389
|
query = cqlParser.parse(query).toCQL();
|
390
|
logger.debug("cqlParser returned query: " + query);
|
391
|
|
392
|
if (logger.isDebugEnabled()) {
|
393
|
logger.debug("index lookup (all, query=" + query + ", " + mdFormat + ", index)");
|
394
|
}
|
395
|
|
396
|
/**
|
397
|
* Ask index for search results
|
398
|
**/
|
399
|
epr = index.indexLookup("all", query, mdFormat, "index");
|
400
|
|
401
|
if (logger.isDebugEnabled()) {
|
402
|
logger.debug("epr = " + epr);
|
403
|
}
|
404
|
|
405
|
time = System.currentTimeMillis() - time;
|
406
|
logger.debug("index query lasted " + time + " msec");
|
407
|
|
408
|
} catch (IndexServiceException ise) {
|
409
|
logger.error("Error calling index.", ise);
|
410
|
throw new SearchServiceException("Error calling index.");
|
411
|
|
412
|
} catch (CQLParseException cqle) {
|
413
|
logger.error("Bad CQL query.", cqle);
|
414
|
throw new SearchServiceException("Error calling index.");
|
415
|
|
416
|
} catch (IOException ioe) {
|
417
|
logger.error("Bad CQL query.", ioe);
|
418
|
throw new SearchServiceException("Error calling index.");
|
419
|
}
|
420
|
|
421
|
if (epr == null) {
|
422
|
throw new SearchServiceException("Index returned null result set id.");
|
423
|
}
|
424
|
|
425
|
int rsSize = -1;
|
426
|
/**
|
427
|
* Read ResultSet for search
|
428
|
*/
|
429
|
ResultSet<String> rs = rsFactory.createResultSet(epr);
|
430
|
try {
|
431
|
rsSize = rs.size();
|
432
|
int from = (page-1)*size + 1;
|
433
|
int to = (page*size < rsSize) ? page*size : rsSize;
|
434
|
|
435
|
List<String> xmls = rs.getElements(from, to);
|
436
|
|
437
|
Transformer tr = transformerFactory.getTransformer(searchTransformer, requestLocale);
|
438
|
searchResults = new ArrayList<String>();
|
439
|
/**
|
440
|
* Transform the search records xmls
|
441
|
*/
|
442
|
for (String xml:xmls) {
|
443
|
searchResults.add(tr.transform(xml));
|
444
|
}
|
445
|
|
446
|
} catch (TransformerException te) {
|
447
|
logger.error("Error transforming search results.", te);
|
448
|
throw new SearchServiceException("Error transforming search results.", te);
|
449
|
}
|
450
|
/**
|
451
|
* refine
|
452
|
*/
|
453
|
List<String> browseResults = null;
|
454
|
|
455
|
try {
|
456
|
long time = System.currentTimeMillis();
|
457
|
|
458
|
cqlParser = new CQLParser();
|
459
|
query = cqlParser.parse(query).toCQL();
|
460
|
StringBuffer buffer = new StringBuffer();
|
461
|
buffer.append("query=").append(query).append("&groupby=");
|
462
|
for (Iterator<String> iter = fields.iterator(); iter.hasNext();) {
|
463
|
String field = (String) iter.next();
|
464
|
buffer.append(field);
|
465
|
if (iter.hasNext()) {
|
466
|
buffer.append(",");
|
467
|
}
|
468
|
}
|
469
|
|
470
|
if (logger.isDebugEnabled()) {
|
471
|
logger.debug("index refine (" + buffer.toString()
|
472
|
+ ", all, " + mdFormat + ", index)");
|
473
|
}
|
474
|
|
475
|
epr = index.getBrowsingStatistics(buffer.toString(), "all", mdFormat, "index");
|
476
|
|
477
|
time = System.currentTimeMillis() - time;
|
478
|
logger.debug("index query lasted " + time + " msec");
|
479
|
|
480
|
} catch (CQLParseException cqle) {
|
481
|
logger.error("Bad CQL query.", cqle);
|
482
|
throw new SearchServiceException("Error calling index.");
|
483
|
} catch (IOException ioe) {
|
484
|
logger.error("Bad CQL query.", ioe);
|
485
|
throw new SearchServiceException("Error calling index.");
|
486
|
} catch (IndexServiceException ise) {
|
487
|
logger.error("Error getting refine results.", ise);
|
488
|
throw new SearchServiceException("Error getting refine results.", ise);
|
489
|
}
|
490
|
|
491
|
if (epr == null) {
|
492
|
throw new SearchServiceException("Index returned null result set id.");
|
493
|
}
|
494
|
|
495
|
|
496
|
try{
|
497
|
rs = rsFactory.createResultSet(epr);
|
498
|
|
499
|
if (logger.isDebugEnabled()) {
|
500
|
logger.debug("EPR : " + epr.getEpr());
|
501
|
}
|
502
|
|
503
|
List<String> list = rs.getElements(1, rs.size());
|
504
|
logger.debug("record list size " + list.size());
|
505
|
|
506
|
Transformer tr = transformerFactory.getTransformer(browseTransformer, requestLocale);
|
507
|
browseResults = new ArrayList<String>();
|
508
|
for (String row: list) {
|
509
|
browseResults.add(tr.transform(row));
|
510
|
}
|
511
|
|
512
|
} catch (TransformerException te) {
|
513
|
logger.error("Error transforming refine results.", te);
|
514
|
throw new SearchServiceException("Error transforming refine results.", te);
|
515
|
|
516
|
}
|
517
|
|
518
|
return new SearchResult(query, correctLocale, rsSize, page, size, searchResults, browseResults, fields);
|
519
|
|
520
|
}
|
521
|
|
522
|
public List<String> getSearchResultsFromIndex(){
|
523
|
return null;
|
524
|
}
|
525
|
|
526
|
public List<String> getRefineResultsFromIndex(){
|
527
|
return null;
|
528
|
}
|
529
|
|
530
|
@Override
|
531
|
public FormattedSearchResult search(String queryText, String transformer,
|
532
|
String format, String locale, int page, int size)
|
533
|
throws SearchServiceException {
|
534
|
FormattedSearchResult formattedSearchResult = null;
|
535
|
|
536
|
SearchResult searchResult = search(queryText, transformer, locale, page, size);
|
537
|
Formatter formatter = transformerFactory.getFormatter(format);
|
538
|
if (formatter == null) {
|
539
|
logger.error("Error formatting search results. " + format +" formatter does not exist.");
|
540
|
throw new SearchServiceException("Error formatting search results. " + format +" formatter does not exist.");
|
541
|
}
|
542
|
|
543
|
try {
|
544
|
formattedSearchResult = new FormattedSearchResult(formatter.format(searchResult), searchResult.getTotal());
|
545
|
logger.debug("Returning formatted result: page " + page + ", size: " + size + " of total: " + searchResult.getTotal());
|
546
|
|
547
|
} catch (Exception e) {
|
548
|
logger.error("Error formatting search results.", e);
|
549
|
}
|
550
|
|
551
|
return formattedSearchResult;
|
552
|
|
553
|
}
|
554
|
|
555
|
@Override
|
556
|
public FormattedSearchResult refine(String queryText, String refineTransformer,
|
557
|
String format, String locale, Collection<String> fields) throws SearchServiceException {
|
558
|
|
559
|
FormattedSearchResult formattedSearchResult = null;
|
560
|
|
561
|
SearchResult searchResult = refine(queryText, refineTransformer, locale, fields);
|
562
|
Formatter formatter = transformerFactory.getFormatter(format);
|
563
|
if (formatter == null) {
|
564
|
logger.error("Error formatting refine results. " + format +" formatter does not exist.");
|
565
|
throw new SearchServiceException("Error formatting refine results. " + format +" formatter does not exist.");
|
566
|
}
|
567
|
|
568
|
try {
|
569
|
formattedSearchResult = new FormattedSearchResult(formatter.format(searchResult), searchResult.getTotal());
|
570
|
|
571
|
} catch (Exception e) {
|
572
|
logger.error("Error formatting refine results.", e);
|
573
|
}
|
574
|
|
575
|
return formattedSearchResult;
|
576
|
}
|
577
|
|
578
|
@Override
|
579
|
public FormattedSearchResult searchNrefine(String queryText,
|
580
|
String searchTransformer, String refineTransformer, String format,
|
581
|
String locale, int page, int size, Collection<String> fields)
|
582
|
throws SearchServiceException {
|
583
|
FormattedSearchResult formattedSearchResult = null;
|
584
|
|
585
|
SearchResult searchResult = searchNrefine(queryText, searchTransformer, refineTransformer, locale, page, size, fields);
|
586
|
Formatter formatter = transformerFactory.getFormatter(format);
|
587
|
if (formatter == null) {
|
588
|
logger.error("Error formating search and refine results. " + format +" formatter does not exist.");
|
589
|
throw new SearchServiceException("Error formating search and refine results. " + format +" formatter does not exist.");
|
590
|
}
|
591
|
|
592
|
try {
|
593
|
formattedSearchResult = new FormattedSearchResult(formatter.format(searchResult), searchResult.getTotal());
|
594
|
|
595
|
} catch (Exception e) {
|
596
|
logger.error("Error formating search results.", e);
|
597
|
}
|
598
|
|
599
|
return formattedSearchResult;
|
600
|
}
|
601
|
|
602
|
private String rewrite(String query) {
|
603
|
|
604
|
if (queryRules != null) {
|
605
|
for (QueryRewriteRule queryRule: queryRules) {
|
606
|
if (logger.isDebugEnabled()) {
|
607
|
logger.debug("Apply rule " + query);
|
608
|
}
|
609
|
query = queryRule.apply(query);
|
610
|
if (logger.isDebugEnabled()) {
|
611
|
logger.debug("Rewritten query is " + query);
|
612
|
}
|
613
|
}
|
614
|
}
|
615
|
return query;
|
616
|
}
|
617
|
|
618
|
public String getMdFormat() {
|
619
|
return mdFormat;
|
620
|
}
|
621
|
|
622
|
public void setMdFormat(String mdFormat) {
|
623
|
this.mdFormat = mdFormat;
|
624
|
}
|
625
|
|
626
|
public ServiceLocator<IndexService> getIndexLocator() {
|
627
|
return indexLocator;
|
628
|
}
|
629
|
|
630
|
public void setIndexLocator(ServiceLocator<IndexService> indexLocator) {
|
631
|
this.indexLocator = indexLocator;
|
632
|
}
|
633
|
|
634
|
public ResultSetFactory getRsFactory() {
|
635
|
return rsFactory;
|
636
|
}
|
637
|
|
638
|
public void setRsFactory(ResultSetFactory rsFactory) {
|
639
|
this.rsFactory = rsFactory;
|
640
|
}
|
641
|
|
642
|
public Collection<FieldRewriteRule> getFieldRules() {
|
643
|
return fieldRules.values();
|
644
|
}
|
645
|
|
646
|
public void setFieldRules(Collection<FieldRewriteRule> fieldRules) {
|
647
|
this.fieldRules = new HashMap<String, FieldRewriteRule>();
|
648
|
for (FieldRewriteRule rule : fieldRules) {
|
649
|
String key = rule.getFieldName();
|
650
|
if (this.fieldRules.containsKey(key)) {
|
651
|
logger.warn("Multiple rules for field " + key);
|
652
|
logger.warn("Keeping last rule " + rule.getName());
|
653
|
}
|
654
|
this.fieldRules.put(key, rule);
|
655
|
}
|
656
|
}
|
657
|
|
658
|
public List<QueryRewriteRule> getQueryRules() {
|
659
|
return queryRules;
|
660
|
}
|
661
|
|
662
|
public void setQueryRules(List<QueryRewriteRule> queryRules) {
|
663
|
this.queryRules = queryRules;
|
664
|
}
|
665
|
|
666
|
public boolean isEnableBrowseCache() {
|
667
|
return enableBrowseCache;
|
668
|
}
|
669
|
|
670
|
public void setEnableBrowseCache(boolean enableBrowseCache) {
|
671
|
this.enableBrowseCache = enableBrowseCache;
|
672
|
}
|
673
|
|
674
|
public SearchRegistry getTransformerFactory() {
|
675
|
return transformerFactory;
|
676
|
}
|
677
|
|
678
|
public void setTransformerFactory(SearchRegistry transformerFactory) {
|
679
|
this.transformerFactory = transformerFactory;
|
680
|
}
|
681
|
|
682
|
public String getIndexLayout() {
|
683
|
return indexLayout;
|
684
|
}
|
685
|
|
686
|
public void setIndexLayout(String indexLayout) {
|
687
|
this.indexLayout = indexLayout;
|
688
|
}
|
689
|
|
690
|
public SearchServiceBlackboardHandler getBlackboardNotificationHandler() {
|
691
|
return blackboardNotificationHandler;
|
692
|
}
|
693
|
|
694
|
public void setBlackboardNotificationHandler(SearchServiceBlackboardHandler blackboardNotificationHandler) {
|
695
|
this.blackboardNotificationHandler = blackboardNotificationHandler;
|
696
|
}
|
697
|
|
698
|
public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
|
699
|
this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
|
700
|
}
|
701
|
}
|