use of org.codelibs.fess.helper.ViewHelper in project fess by codelibs.
the class QueryResponseList method parseSearchHit.
protected Map<String, Object> parseSearchHit(final FessConfig fessConfig, final String hlPrefix, final SearchHit searchHit) {
final Map<String, Object> docMap = new HashMap<>(32);
if (searchHit.getSourceAsMap() == null) {
searchHit.getFields().forEach((key, value) -> {
docMap.put(key, value.getValue());
});
} else {
docMap.putAll(searchHit.getSourceAsMap());
}
final ViewHelper viewHelper = ComponentUtil.getViewHelper();
final Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
try {
if (highlightFields != null) {
highlightFields.values().stream().forEach(highlightField -> {
final String text = viewHelper.createHighlightText(highlightField);
if (text != null) {
docMap.put(hlPrefix + highlightField.getName(), text);
}
});
if (Constants.TEXT_FRAGMENT_TYPE_HIGHLIGHT.equals(fessConfig.getQueryHighlightTextFragmentType())) {
docMap.put(Constants.TEXT_FRAGMENTS, viewHelper.createTextFragmentsByHighlight(highlightFields.values().toArray(n -> new HighlightField[n])));
}
}
} catch (final Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Could not create a highlighting value: {}", docMap, e);
}
}
if (Constants.TEXT_FRAGMENT_TYPE_QUERY.equals(fessConfig.getQueryHighlightTextFragmentType())) {
docMap.put(Constants.TEXT_FRAGMENTS, viewHelper.createTextFragmentsByQuery());
}
// ContentTitle
if (viewHelper != null) {
docMap.put(fessConfig.getResponseFieldContentTitle(), viewHelper.getContentTitle(docMap));
docMap.put(fessConfig.getResponseFieldContentDescription(), viewHelper.getContentDescription(docMap));
docMap.put(fessConfig.getResponseFieldUrlLink(), viewHelper.getUrlLink(docMap));
docMap.put(fessConfig.getResponseFieldSitePath(), viewHelper.getSitePath(docMap));
}
if (!docMap.containsKey(Constants.SCORE)) {
docMap.put(Constants.SCORE, searchHit.getScore());
}
if (!docMap.containsKey(fessConfig.getIndexFieldId())) {
docMap.put(fessConfig.getIndexFieldId(), searchHit.getId());
}
return docMap;
}
use of org.codelibs.fess.helper.ViewHelper in project fess by codelibs.
the class GoAction method index.
// ===================================================================================
// Attribute
//
// ===================================================================================
// Hook
// ======
// ===================================================================================
// Search Execute
// ==============
@Execute
public ActionResponse index(final GoForm form) throws IOException {
validate(form, messages -> {
}, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
if (isLoginRequired()) {
return redirectToLogin();
}
Map<String, Object> doc = null;
try {
doc = searchHelper.getDocumentByDocId(form.docId, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldConfigId() }, getUserBean()).orElse(null);
} catch (final Exception e) {
logger.warn("Failed to request: {}", form.docId, e);
}
if (doc == null) {
saveError(messages -> messages.addErrorsDocidNotFound(GLOBAL, form.docId));
return redirect(ErrorAction.class);
}
final String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (url == null) {
saveError(messages -> messages.addErrorsDocumentNotFound(GLOBAL, form.docId));
return redirect(ErrorAction.class);
}
if (fessConfig.isSearchLog()) {
final String userSessionId = userInfoHelper.getUserCode();
if (userSessionId != null) {
final SearchLogHelper searchLogHelper = ComponentUtil.getSearchLogHelper();
final ClickLog clickLog = new ClickLog();
clickLog.setUrlId((String) doc.get(fessConfig.getIndexFieldId()));
clickLog.setUrl(url);
clickLog.setRequestedAt(systemHelper.getCurrentTimeAsLocalDateTime());
clickLog.setQueryRequestedAt(DfTypeUtil.toLocalDateTime(Long.parseLong(form.rt)));
clickLog.setUserSessionId(userSessionId);
clickLog.setDocId(form.docId);
clickLog.setQueryId(form.queryId);
if (form.order != null) {
clickLog.setOrder(form.order);
}
searchLogHelper.addClickLog(clickLog);
}
}
String hash;
if (StringUtil.isNotBlank(form.hash)) {
final String value = URLUtil.decode(form.hash, Constants.UTF_8);
final StringBuilder buf = new StringBuilder(value.length() + 100);
for (final char c : value.toCharArray()) {
if (CharUtil.isUrlChar(c) || c == ' ') {
buf.append(c);
} else {
try {
buf.append(URLEncoder.encode(String.valueOf(c), Constants.UTF_8));
} catch (final UnsupportedEncodingException e) {
// NOP
}
}
}
hash = buf.toString();
} else {
hash = StringUtil.EMPTY;
}
final String targetUrl = pathMappingHelper.replaceUrl(url);
if (!isFileSystemPath(targetUrl)) {
return HtmlResponse.fromRedirectPathAsIs(DocumentUtil.encodeUrl(targetUrl + hash));
}
if (!fessConfig.isSearchFileProxyEnabled()) {
return HtmlResponse.fromRedirectPathAsIs(targetUrl + hash);
}
final ViewHelper viewHelper = ComponentUtil.getViewHelper();
try {
final StreamResponse response = viewHelper.asContentResponse(doc);
if (response.getHttpStatus().orElse(200) == 404) {
logger.debug("Not found: {}", targetUrl);
saveError(messages -> messages.addErrorsNotFoundOnFileSystem(GLOBAL, targetUrl));
return redirect(ErrorAction.class);
}
return response;
} catch (final Exception e) {
logger.warn("Failed to load: {}", doc, e);
saveError(messages -> messages.addErrorsNotLoadFromServer(GLOBAL, targetUrl));
return redirect(ErrorAction.class);
}
}
Aggregations