Search in sources :

Example 1 with ClickLog

use of org.codelibs.fess.es.log.exentity.ClickLog 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 = searchService.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.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)) {
        if (fessConfig.isSearchFileProxyEnabled()) {
            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);
            }
        } else {
            return redirect(targetUrl + hash);
        }
    } else {
        return redirect(targetUrl + hash);
    }
}
Also used : FessSearchAction(org.codelibs.fess.app.web.base.FessSearchAction) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) LoggerFactory(org.slf4j.LoggerFactory) ClickLog(org.codelibs.fess.es.log.exentity.ClickLog) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) ErrorAction(org.codelibs.fess.app.web.error.ErrorAction) StreamResponse(org.lastaflute.web.response.StreamResponse) Map(java.util.Map) CharUtil(org.codelibs.fess.crawler.util.CharUtil) HtmlResponse(org.lastaflute.web.response.HtmlResponse) Logger(org.slf4j.Logger) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) URLUtil(org.codelibs.core.net.URLUtil) ActionResponse(org.lastaflute.web.response.ActionResponse) URLEncoder(java.net.URLEncoder) SearchLogHelper(org.codelibs.fess.helper.SearchLogHelper) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DfTypeUtil(org.dbflute.util.DfTypeUtil) PathMappingHelper(org.codelibs.fess.helper.PathMappingHelper) Execute(org.lastaflute.web.Execute) ViewHelper(org.codelibs.fess.helper.ViewHelper) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StreamResponse(org.lastaflute.web.response.StreamResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SearchLogHelper(org.codelibs.fess.helper.SearchLogHelper) ViewHelper(org.codelibs.fess.helper.ViewHelper) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ErrorAction(org.codelibs.fess.app.web.error.ErrorAction) ClickLog(org.codelibs.fess.es.log.exentity.ClickLog) Execute(org.lastaflute.web.Execute)

Example 2 with ClickLog

use of org.codelibs.fess.es.log.exentity.ClickLog in project fess by codelibs.

the class SearchLogHelper method processClickLogQueue.

protected void processClickLogQueue(final Queue<ClickLog> queue) {
    final Map<String, Integer> clickCountMap = new HashMap<>();
    final List<ClickLog> clickLogList = new ArrayList<>();
    for (final ClickLog clickLog : queue) {
        try {
            final SearchLogBhv searchLogBhv = ComponentUtil.getComponent(SearchLogBhv.class);
            searchLogBhv.selectEntity(cb -> {
                cb.query().setQueryId_Equal(clickLog.getQueryId());
            }).ifPresent(entity -> {
                clickLogList.add(clickLog);
                final String docId = clickLog.getDocId();
                Integer countObj = clickCountMap.get(docId);
                if (countObj == null) {
                    countObj = Integer.valueOf(1);
                } else {
                    countObj = countObj.intValue() + 1;
                }
                clickCountMap.put(docId, countObj);
            }).orElse(() -> {
                logger.warn("Not Found for SearchLog: " + clickLog);
            });
        } catch (final Exception e) {
            logger.warn("Failed to process: " + clickLog, e);
        }
    }
    if (!clickLogList.isEmpty()) {
        try {
            final ClickLogBhv clickLogBhv = ComponentUtil.getComponent(ClickLogBhv.class);
            clickLogBhv.batchInsert(clickLogList);
        } catch (final Exception e) {
            logger.warn("Failed to insert: " + clickLogList, e);
        }
    }
    if (!clickCountMap.isEmpty()) {
        final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
        try {
            searchService.bulkUpdate(builder -> {
                final FessConfig fessConfig = ComponentUtil.getFessConfig();
                searchService.getDocumentListByDocIds(clickCountMap.keySet().toArray(new String[clickCountMap.size()]), new String[] { fessConfig.getIndexFieldDocId() }, OptionalThing.of(FessUserBean.empty()), SearchRequestType.ADMIN_SEARCH).forEach(doc -> {
                    final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
                    final String docId = DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
                    if (id != null && docId != null && clickCountMap.containsKey(docId)) {
                        final Integer count = clickCountMap.get(docId);
                        final Script script = new Script("ctx._source." + fessConfig.getIndexFieldClickCount() + "+=" + count.toString());
                        final Map<String, Object> upsertMap = new HashMap<>();
                        upsertMap.put(fessConfig.getIndexFieldClickCount(), count);
                        builder.add(new UpdateRequest(fessConfig.getIndexDocumentUpdateIndex(), fessConfig.getIndexDocumentType(), id).script(script).upsert(upsertMap));
                    }
                });
            });
        } catch (final Exception e) {
            logger.warn("Failed to update clickCounts", e);
        }
    }
}
Also used : QueryResponseList(org.codelibs.fess.util.QueryResponseList) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) OptionalThing(org.dbflute.optional.OptionalThing) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) LruHashMap(org.codelibs.core.collection.LruHashMap) ClickLog(org.codelibs.fess.es.log.exentity.ClickLog) StringUtils(org.apache.commons.lang3.StringUtils) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) ArrayList(java.util.ArrayList) FessUserBean(org.codelibs.fess.mylasta.action.FessUserBean) HttpServletRequest(javax.servlet.http.HttpServletRequest) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SearchRequestParams(org.codelibs.fess.entity.SearchRequestParams) Map(java.util.Map) FavoriteLogBhv(org.codelibs.fess.es.log.exbhv.FavoriteLogBhv) SearchFieldLog(org.codelibs.fess.es.log.exentity.SearchFieldLog) UserInfo(org.codelibs.fess.es.log.exentity.UserInfo) Script(org.elasticsearch.script.Script) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Logger(org.slf4j.Logger) UserInfoBhv(org.codelibs.fess.es.log.exbhv.UserInfoBhv) ClickLogBhv(org.codelibs.fess.es.log.exbhv.ClickLogBhv) SearchLog(org.codelibs.fess.es.log.exentity.SearchLog) SearchFieldLogBhv(org.codelibs.fess.es.log.exbhv.SearchFieldLogBhv) StringUtil(org.codelibs.core.lang.StringUtil) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) SearchService(org.codelibs.fess.app.service.SearchService) List(java.util.List) SearchLogBhv(org.codelibs.fess.es.log.exbhv.SearchLogBhv) ComponentUtil(org.codelibs.fess.util.ComponentUtil) PostConstruct(javax.annotation.PostConstruct) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) LruHashMap(org.codelibs.core.collection.LruHashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) ClickLogBhv(org.codelibs.fess.es.log.exbhv.ClickLogBhv) ArrayList(java.util.ArrayList) SearchLogBhv(org.codelibs.fess.es.log.exbhv.SearchLogBhv) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SearchService(org.codelibs.fess.app.service.SearchService) ClickLog(org.codelibs.fess.es.log.exentity.ClickLog)

Aggregations

Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 StringUtil (org.codelibs.core.lang.StringUtil)2 Constants (org.codelibs.fess.Constants)2 ClickLog (org.codelibs.fess.es.log.exentity.ClickLog)2 ComponentUtil (org.codelibs.fess.util.ComponentUtil)2 DocumentUtil (org.codelibs.fess.util.DocumentUtil)2 LaRequestUtil (org.lastaflute.web.util.LaRequestUtil)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLEncoder (java.net.URLEncoder)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 PostConstruct (javax.annotation.PostConstruct)1