Search in sources :

Example 1 with UpdateRequest

use of org.opensearch.action.update.UpdateRequest in project fess by codelibs.

the class SearchLogHelper method updateClickFieldInIndex.

protected void updateClickFieldInIndex(final Map<String, Integer> clickCountMap) {
    if (!clickCountMap.isEmpty()) {
        final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        try {
            final UpdateRequest[] updateRequests = searchHelper.getDocumentListByDocIds(clickCountMap.keySet().toArray(new String[clickCountMap.size()]), new String[] { fessConfig.getIndexFieldDocId(), fessConfig.getIndexFieldLang() }, OptionalThing.of(FessUserBean.empty()), SearchRequestType.ADMIN_SEARCH).stream().map(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 = ComponentUtil.getLanguageHelper().createScript(doc, "ctx._source." + fessConfig.getIndexFieldClickCount() + "+=" + count.toString());
                    final Map<String, Object> upsertMap = new HashMap<>();
                    upsertMap.put(fessConfig.getIndexFieldClickCount(), count);
                    return new UpdateRequest(fessConfig.getIndexDocumentUpdateIndex(), id).script(script).upsert(upsertMap);
                }
                return null;
            }).filter(req -> req != null).toArray(n -> new UpdateRequest[n]);
            if (updateRequests.length > 0) {
                searchHelper.bulkUpdate(builder -> {
                    for (final UpdateRequest req : updateRequests) {
                        builder.add(req);
                    }
                });
            }
        } catch (final Exception e) {
            logger.warn("Failed to update clickCounts", e);
        }
    }
}
Also used : QueryResponseList(org.codelibs.fess.util.QueryResponseList) Arrays(java.util.Arrays) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) LoadingCache(com.google.common.cache.LoadingCache) OptionalThing(org.dbflute.optional.OptionalThing) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) 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) Locale(java.util.Locale) CommonPoolUtil(org.codelibs.core.concurrent.CommonPoolUtil) Map(java.util.Map) FavoriteLogBhv(org.codelibs.fess.es.log.exbhv.FavoriteLogBhv) UserInfo(org.codelibs.fess.es.log.exentity.UserInfo) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) SearchLogEvent(org.codelibs.fess.entity.SearchLogEvent) UserInfoBhv(org.codelibs.fess.es.log.exbhv.UserInfoBhv) ClickLogBhv(org.codelibs.fess.es.log.exbhv.ClickLogBhv) SearchLog(org.codelibs.fess.es.log.exentity.SearchLog) OptionalEntity(org.dbflute.optional.OptionalEntity) CaseFormat(com.google.common.base.CaseFormat) Script(org.opensearch.script.Script) StringUtil(org.codelibs.core.lang.StringUtil) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) Logger(org.apache.logging.log4j.Logger) SearchLogBhv(org.codelibs.fess.es.log.exbhv.SearchLogBhv) ComponentUtil(org.codelibs.fess.util.ComponentUtil) PostConstruct(javax.annotation.PostConstruct) UpdateRequest(org.opensearch.action.update.UpdateRequest) Queue(java.util.Queue) CacheBuilder(com.google.common.cache.CacheBuilder) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Script(org.opensearch.script.Script) UpdateRequest(org.opensearch.action.update.UpdateRequest) HashMap(java.util.HashMap) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CaseFormat (com.google.common.base.CaseFormat)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1