Search in sources :

Example 1 with Script

use of org.opensearch.script.Script in project fess by codelibs.

the class JsonApiManager method processFavoriteRequest.

protected void processFavoriteRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
    if (!ComponentUtil.getFessConfig().isUserFavorite()) {
        writeJsonResponse(9, null, "Unsupported operation.");
        return;
    }
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
    final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
    final FavoriteLogService favoriteLogService = ComponentUtil.getComponent(FavoriteLogService.class);
    final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
    try {
        final String docId = request.getParameter("docId");
        final String queryId = request.getParameter("queryId");
        final String[] docIds = userInfoHelper.getResultDocIds(URLDecoder.decode(queryId, Constants.UTF_8));
        if (docIds == null) {
            throw new WebApiException(6, "No searched urls.");
        }
        searchHelper.getDocumentByDocId(docId, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldLang() }, OptionalThing.empty()).ifPresent(doc -> {
            final String favoriteUrl = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
            final String userCode = userInfoHelper.getUserCode();
            if (StringUtil.isBlank(userCode)) {
                throw new WebApiException(2, "No user session.");
            }
            if (StringUtil.isBlank(favoriteUrl)) {
                throw new WebApiException(2, "URL is null.");
            }
            boolean found = false;
            for (final String id : docIds) {
                if (docId.equals(id)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new WebApiException(5, "Not found: " + favoriteUrl);
            }
            if (!favoriteLogService.addUrl(userCode, (userInfo, favoriteLog) -> {
                favoriteLog.setUserInfoId(userInfo.getId());
                favoriteLog.setUrl(favoriteUrl);
                favoriteLog.setDocId(docId);
                favoriteLog.setQueryId(queryId);
                favoriteLog.setCreatedAt(systemHelper.getCurrentTimeAsLocalDateTime());
            })) {
                throw new WebApiException(4, "Failed to add url: " + favoriteUrl);
            }
            final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
            searchHelper.update(id, builder -> {
                final Script script = ComponentUtil.getLanguageHelper().createScript(doc, "ctx._source." + fessConfig.getIndexFieldFavoriteCount() + "+=1");
                builder.setScript(script);
                final Map<String, Object> upsertMap = new HashMap<>();
                upsertMap.put(fessConfig.getIndexFieldFavoriteCount(), 1);
                builder.setUpsert(upsertMap);
                builder.setRefreshPolicy(Constants.TRUE);
            });
            writeJsonResponse(0, "\"result\":\"ok\"", (String) null);
        }).orElse(() -> {
            throw new WebApiException(6, "Not found: " + docId);
        });
    } catch (final Exception e) {
        int status;
        if (e instanceof WebApiException) {
            status = ((WebApiException) e).getStatusCode();
        } else {
            status = 1;
        }
        writeJsonResponse(status, null, e);
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a favorite request.", e);
        }
    }
}
Also used : Arrays(java.util.Arrays) FilterChain(javax.servlet.FilterChain) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) IORuntimeException(org.codelibs.core.exception.IORuntimeException) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) URLDecoder(java.net.URLDecoder) FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) ServletException(javax.servlet.ServletException) OptionalThing(org.dbflute.optional.OptionalThing) HashMap(java.util.HashMap) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) WebApiException(org.codelibs.fess.exception.WebApiException) RelatedQueryHelper(org.codelibs.fess.helper.RelatedQueryHelper) ArrayList(java.util.ArrayList) GeoInfo(org.codelibs.fess.entity.GeoInfo) HttpServletRequest(javax.servlet.http.HttpServletRequest) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) PingResponse(org.codelibs.fess.entity.PingResponse) SearchRequestParams(org.codelibs.fess.entity.SearchRequestParams) Locale(java.util.Locale) PopularWordHelper(org.codelibs.fess.helper.PopularWordHelper) Map(java.util.Map) FacetInfo(org.codelibs.fess.entity.FacetInfo) SearchHelper(org.codelibs.fess.helper.SearchHelper) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) Field(org.codelibs.fess.util.FacetResponse.Field) Script(org.opensearch.script.Script) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringUtil(org.codelibs.core.lang.StringUtil) RelatedContentHelper(org.codelibs.fess.helper.RelatedContentHelper) IOException(java.io.IOException) FacetResponse(org.codelibs.fess.util.FacetResponse) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ComponentUtil(org.codelibs.fess.util.ComponentUtil) SystemHelper(org.codelibs.fess.helper.SystemHelper) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) PostConstruct(javax.annotation.PostConstruct) HighlightInfo(org.codelibs.fess.entity.HighlightInfo) LogManager(org.apache.logging.log4j.LogManager) BaseJsonApiManager(org.codelibs.fess.api.BaseJsonApiManager) Script(org.opensearch.script.Script) FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SearchHelper(org.codelibs.fess.helper.SearchHelper) WebApiException(org.codelibs.fess.exception.WebApiException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) SystemHelper(org.codelibs.fess.helper.SystemHelper) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Script

use of org.opensearch.script.Script 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)

Example 3 with Script

use of org.opensearch.script.Script in project fess by codelibs.

the class LanguageHelper method createScript.

public Script createScript(final Map<String, Object> doc, final String code) {
    final StringBuilder buf = new StringBuilder(100);
    buf.append(code);
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String language = DocumentUtil.getValue(doc, fessConfig.getIndexFieldLang(), String.class);
    if (StringUtil.isNotBlank(language)) {
        for (final String f : langFields) {
            buf.append(";ctx._source.").append(f).append('_').append(language).append("=ctx._source.").append(f);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("update script: {}", buf);
    }
    return new Script(buf.toString());
}
Also used : Script(org.opensearch.script.Script) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 4 with Script

use of org.opensearch.script.Script in project fess by codelibs.

the class LanguageHelper method getReindexScriptSource.

public String getReindexScriptSource() {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String langField = fessConfig.getIndexFieldLang();
    final String code = Arrays.stream(langFields).map(s -> "ctx._source['" + s + "_'+ctx._source." + langField + "]=ctx._source." + s).collect(Collectors.joining(";"));
    if (logger.isDebugEnabled()) {
        logger.debug("reindex script: {}", code);
    }
    return "if(ctx._source." + langField + "!=null){" + code + "}";
}
Also used : Arrays(java.util.Arrays) DocumentUtil(org.codelibs.fess.util.DocumentUtil) Script(org.opensearch.script.Script) StringUtil(org.codelibs.core.lang.StringUtil) LanguageDetector(org.apache.tika.language.detect.LanguageDetector) Collectors(java.util.stream.Collectors) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) ComponentUtil(org.codelibs.fess.util.ComponentUtil) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct) LogManager(org.apache.logging.log4j.LogManager) LanguageResult(org.apache.tika.language.detect.LanguageResult) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 5 with Script

use of org.opensearch.script.Script in project fess by codelibs.

the class UpdateLabelJob method execute.

public String execute() {
    final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
    final LanguageHelper languageHelper = ComponentUtil.getLanguageHelper();
    final StringBuilder resultBuf = new StringBuilder();
    try {
        final long count = searchEngineClient.updateByQuery(fessConfig.getIndexDocumentUpdateIndex(), option -> {
            if (queryBuilder != null) {
                option.setQuery(queryBuilder);
            }
            return option.setFetchSource(new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldLang() }, null);
        }, (builder, hit) -> {
            try {
                final Map<String, Object> doc = hit.getSourceAsMap();
                final String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
                if (StringUtil.isNotBlank(url)) {
                    final Set<String> labelSet = labelTypeHelper.getMatchedLabelValueSet(url);
                    final Script script = languageHelper.createScript(doc, "ctx._source." + fessConfig.getIndexFieldLabel() + "=new String[]{" + labelSet.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(",")) + "}");
                    return builder.setScript(script);
                }
            } catch (final Exception e) {
                logger.warn("Failed to process {}", hit, e);
            }
            return null;
        });
        resultBuf.append(count).append(" docs").append("\n");
    } catch (final Exception e) {
        logger.error("Could not update labels.", e);
        resultBuf.append(e.getMessage()).append("\n");
    }
    return resultBuf.toString();
}
Also used : Script(org.opensearch.script.Script) LanguageHelper(org.codelibs.fess.helper.LanguageHelper) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Aggregations

FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)5 Script (org.opensearch.script.Script)5 Arrays (java.util.Arrays)3 Map (java.util.Map)3 PostConstruct (javax.annotation.PostConstruct)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 StringUtil (org.codelibs.core.lang.StringUtil)3 ComponentUtil (org.codelibs.fess.util.ComponentUtil)3 DocumentUtil (org.codelibs.fess.util.DocumentUtil)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Locale (java.util.Locale)2 Collectors (java.util.stream.Collectors)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Constants (org.codelibs.fess.Constants)2 SearchRequestParams (org.codelibs.fess.entity.SearchRequestParams)2 SearchRequestType (org.codelibs.fess.entity.SearchRequestParams.SearchRequestType)2 OptionalThing (org.dbflute.optional.OptionalThing)2