Search in sources :

Example 11 with StringUtil

use of org.codelibs.core.lang.StringUtil in project fess by codelibs.

the class FessXpathTransformer method putAdditionalData.

protected void putAdditionalData(final Map<String, Object> dataMap, final ResponseData responseData, final Document document) {
    // canonical
    final String canonicalUrl = getCanonicalUrl(responseData, document);
    if (canonicalUrl != null && !canonicalUrl.equals(responseData.getUrl()) && isValidUrl(canonicalUrl) && isValidCanonicalUrl(responseData.getUrl(), canonicalUrl)) {
        final Set<RequestData> childUrlSet = new HashSet<>();
        childUrlSet.add(RequestDataBuilder.newRequestData().get().url(canonicalUrl).build());
        logger.info("CANONICAL: {} -> {}", responseData.getUrl(), canonicalUrl);
        throw new ChildUrlsException(childUrlSet, this.getClass().getName() + "#putAdditionalData");
    }
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final CrawlingInfoHelper crawlingInfoHelper = ComponentUtil.getCrawlingInfoHelper();
    final String sessionId = crawlingInfoHelper.getCanonicalSessionId(responseData.getSessionId());
    final PathMappingHelper pathMappingHelper = ComponentUtil.getPathMappingHelper();
    final CrawlingConfigHelper crawlingConfigHelper = ComponentUtil.getCrawlingConfigHelper();
    final CrawlingConfig crawlingConfig = crawlingConfigHelper.get(responseData.getSessionId());
    final Date documentExpires = crawlingInfoHelper.getDocumentExpires(crawlingConfig);
    final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
    final FileTypeHelper fileTypeHelper = ComponentUtil.getFileTypeHelper();
    final DocumentHelper documentHelper = ComponentUtil.getDocumentHelper();
    final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
    String url = responseData.getUrl();
    final String indexingTarget = crawlingConfig.getIndexingTarget(url);
    url = pathMappingHelper.replaceUrl(sessionId, url);
    final String mimeType = responseData.getMimeType();
    final Map<String, String> fieldConfigMap = crawlingConfig.getConfigParameterMap(ConfigName.FIELD);
    final Map<String, String> xpathConfigMap = crawlingConfig.getConfigParameterMap(ConfigName.XPATH);
    String urlEncoding;
    final UrlQueue<?> urlQueue = CrawlingParameterUtil.getUrlQueue();
    if (urlQueue != null && urlQueue.getEncoding() != null) {
        urlEncoding = urlQueue.getEncoding();
    } else {
        urlEncoding = responseData.getCharSet();
    }
    // cid
    final String configId = crawlingConfig.getConfigId();
    if (configId != null) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldConfigId(), configId);
    }
    // expires
    if (documentExpires != null) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldExpires(), documentExpires);
    }
    // lang
    final String lang = systemHelper.normalizeHtmlLang(getSingleNodeValue(document, getLangXpath(fessConfig, xpathConfigMap), true));
    if (lang != null) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldLang(), lang);
    }
    // title
    // content
    final String body = getSingleNodeValue(document, getContentXpath(fessConfig, xpathConfigMap), prunedContent);
    putResultDataBody(dataMap, fessConfig.getIndexFieldContent(), documentHelper.getContent(crawlingConfig, responseData, body, dataMap));
    if ((Constants.TRUE.equalsIgnoreCase(fieldConfigMap.get(fessConfig.getIndexFieldCache())) || fessConfig.isCrawlerDocumentCacheEnabled()) && fessConfig.isSupportedDocumentCacheMimetypes(mimeType)) {
        if (responseData.getContentLength() > 0 && responseData.getContentLength() <= fessConfig.getCrawlerDocumentCacheMaxSizeAsInteger().longValue()) {
            String charSet = responseData.getCharSet();
            if (charSet == null) {
                charSet = Constants.UTF_8;
            }
            try (final BufferedInputStream is = new BufferedInputStream(responseData.getResponseBody())) {
                // cache
                putResultDataBody(dataMap, fessConfig.getIndexFieldCache(), new String(InputStreamUtil.getBytes(is), charSet));
                putResultDataBody(dataMap, fessConfig.getIndexFieldHasCache(), Constants.TRUE);
            } catch (final Exception e) {
                logger.warn("Failed to write a cache: {}:{}", sessionId, responseData, e);
            }
        } else {
            logger.debug("Content size is too large({} > {}): {}", responseData.getContentLength(), fessConfig.getCrawlerDocumentCacheMaxSizeAsInteger(), responseData.getUrl());
        }
    }
    // digest
    final String digest = getSingleNodeValue(document, getDigestXpath(fessConfig, xpathConfigMap), false);
    if (StringUtil.isNotBlank(digest)) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldDigest(), digest);
    } else {
        putResultDataBody(dataMap, fessConfig.getIndexFieldDigest(), documentHelper.getDigest(responseData, body, dataMap, fessConfig.getCrawlerDocumentHtmlMaxDigestLengthAsInteger()));
    }
    // segment
    putResultDataBody(dataMap, fessConfig.getIndexFieldSegment(), sessionId);
    // host
    putResultDataBody(dataMap, fessConfig.getIndexFieldHost(), getHost(url));
    // site
    putResultDataBody(dataMap, fessConfig.getIndexFieldSite(), getSite(url, urlEncoding));
    // filename
    final String fileName = getFileName(url, urlEncoding);
    if (StringUtil.isNotBlank(fileName)) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldFilename(), fileName);
    }
    // url
    putResultDataBody(dataMap, fessConfig.getIndexFieldUrl(), url);
    // created
    final Date now = systemHelper.getCurrentTime();
    putResultDataBody(dataMap, fessConfig.getIndexFieldCreated(), now);
    // anchor
    putResultDataBody(dataMap, fessConfig.getIndexFieldAnchor(), getAnchorList(document, responseData));
    // mimetype
    putResultDataBody(dataMap, fessConfig.getIndexFieldMimetype(), mimeType);
    if (fileTypeHelper != null) {
        // filetype
        putResultDataBody(dataMap, fessConfig.getIndexFieldFiletype(), fileTypeHelper.get(mimeType));
    }
    // content_length
    putResultDataBody(dataMap, fessConfig.getIndexFieldContentLength(), Long.toString(responseData.getContentLength()));
    // last_modified
    final Date lastModified = responseData.getLastModified();
    if (lastModified != null) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldLastModified(), lastModified);
        // timestamp
        putResultDataBody(dataMap, fessConfig.getIndexFieldTimestamp(), lastModified);
    } else {
        // timestamp
        putResultDataBody(dataMap, fessConfig.getIndexFieldTimestamp(), now);
    }
    // indexingTarget
    putResultDataBody(dataMap, Constants.INDEXING_TARGET, indexingTarget);
    // boost
    putResultDataBody(dataMap, fessConfig.getIndexFieldBoost(), crawlingConfig.getDocumentBoost());
    // label: labelType
    putResultDataBody(dataMap, fessConfig.getIndexFieldLabel(), labelTypeHelper.getMatchedLabelValueSet(url));
    // role: roleType
    final List<String> roleTypeList = new ArrayList<>();
    stream(crawlingConfig.getPermissions()).of(stream -> stream.forEach(p -> roleTypeList.add(p)));
    putResultDataBody(dataMap, fessConfig.getIndexFieldRole(), roleTypeList);
    // virtualHosts
    putResultDataBody(dataMap, fessConfig.getIndexFieldVirtualHost(), stream(crawlingConfig.getVirtualHosts()).get(stream -> stream.filter(StringUtil::isNotBlank).collect(Collectors.toList())));
    // id
    putResultDataBody(dataMap, fessConfig.getIndexFieldId(), crawlingInfoHelper.generateId(dataMap));
    // parentId
    String parentUrl = responseData.getParentUrl();
    if (StringUtil.isNotBlank(parentUrl)) {
        parentUrl = pathMappingHelper.replaceUrl(sessionId, parentUrl);
        putResultDataBody(dataMap, fessConfig.getIndexFieldUrl(), parentUrl);
        putResultDataBody(dataMap, fessConfig.getIndexFieldParentId(), crawlingInfoHelper.generateId(dataMap));
        // set again
        putResultDataBody(dataMap, fessConfig.getIndexFieldUrl(), url);
    }
    // thumbnail
    final String thumbnailUrl = getThumbnailUrl(responseData, document);
    if (StringUtil.isNotBlank(thumbnailUrl)) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldThumbnail(), thumbnailUrl);
    }
    // from config
    final String scriptType = crawlingConfig.getScriptType();
    final Map<String, String> scriptConfigMap = crawlingConfig.getConfigParameterMap(ConfigName.SCRIPT);
    xpathConfigMap.entrySet().stream().filter(e -> !e.getKey().startsWith("default.")).forEach(e -> {
        final String key = e.getKey();
        final String value = getSingleNodeValue(document, e.getValue(), true);
        putResultDataWithTemplate(dataMap, key, value, scriptConfigMap.get(key), scriptType);
    });
    crawlingConfig.getConfigParameterMap(ConfigName.VALUE).entrySet().stream().forEach(e -> {
        final String key = e.getKey();
        final String value = e.getValue();
        putResultDataWithTemplate(dataMap, key, value, scriptConfigMap.get(key), scriptType);
    });
}
Also used : ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) Constants(org.codelibs.fess.Constants) BufferedInputStream(java.io.BufferedInputStream) URL(java.net.URL) Date(java.util.Date) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) ConfigName(org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigName) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Locale(java.util.Locale) Document(org.w3c.dom.Document) Map(java.util.Map) AccessResultData(org.codelibs.fess.crawler.entity.AccessResultData) Config(org.codelibs.fess.es.config.exentity.CrawlingConfig.Param.Config) InputStreamUtil(org.codelibs.core.io.InputStreamUtil) ResultData(org.codelibs.fess.crawler.entity.ResultData) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) FileTypeHelper(org.codelibs.fess.helper.FileTypeHelper) RequestData(org.codelibs.fess.crawler.entity.RequestData) ComponentUtil(org.codelibs.fess.util.ComponentUtil) CrawlingInfoHelper(org.codelibs.fess.helper.CrawlingInfoHelper) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) PostConstruct(javax.annotation.PostConstruct) DocumentHelper(org.codelibs.fess.helper.DocumentHelper) XObject(org.apache.xpath.objects.XObject) UrlQueue(org.codelibs.fess.crawler.entity.UrlQueue) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) CrawlingConfigHelper(org.codelibs.fess.helper.CrawlingConfigHelper) TransformerException(javax.xml.transform.TransformerException) HashMap(java.util.HashMap) SerializeUtil(org.codelibs.core.io.SerializeUtil) CrawlingParameterUtil(org.codelibs.fess.crawler.util.CrawlingParameterUtil) CrawlingConfig(org.codelibs.fess.es.config.exentity.CrawlingConfig) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) XpathTransformer(org.codelibs.fess.crawler.transformer.impl.XpathTransformer) Node(org.w3c.dom.Node) PrunedTag(org.codelibs.fess.util.PrunedTag) NamedNodeMap(org.w3c.dom.NamedNodeMap) InputSource(org.xml.sax.InputSource) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) XPath(org.codelibs.fess.es.config.exentity.CrawlingConfig.Param.XPath) NodeList(org.w3c.dom.NodeList) MalformedURLException(java.net.MalformedURLException) StringUtil(org.codelibs.core.lang.StringUtil) DuplicateHostHelper(org.codelibs.fess.helper.DuplicateHostHelper) ValueHolder(org.codelibs.core.misc.ValueHolder) SystemHelper(org.codelibs.fess.helper.SystemHelper) PathMappingHelper(org.codelibs.fess.helper.PathMappingHelper) DOMParser(org.codelibs.nekohtml.parsers.DOMParser) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) RequestDataBuilder(org.codelibs.fess.crawler.builder.RequestDataBuilder) DocumentHelper(org.codelibs.fess.helper.DocumentHelper) CrawlingConfig(org.codelibs.fess.es.config.exentity.CrawlingConfig) ArrayList(java.util.ArrayList) PathMappingHelper(org.codelibs.fess.helper.PathMappingHelper) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Date(java.util.Date) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) TransformerException(javax.xml.transform.TransformerException) MalformedURLException(java.net.MalformedURLException) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) CrawlingConfigHelper(org.codelibs.fess.helper.CrawlingConfigHelper) SystemHelper(org.codelibs.fess.helper.SystemHelper) BufferedInputStream(java.io.BufferedInputStream) RequestData(org.codelibs.fess.crawler.entity.RequestData) CrawlingInfoHelper(org.codelibs.fess.helper.CrawlingInfoHelper) FileTypeHelper(org.codelibs.fess.helper.FileTypeHelper) HashSet(java.util.HashSet)

Example 12 with StringUtil

use of org.codelibs.core.lang.StringUtil in project fess by codelibs.

the class SamlCredential method getDefaultRolesAsArray.

protected String[] getDefaultRolesAsArray() {
    final List<String> list = new ArrayList<>();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String key = fessConfig.getSystemProperty("saml.attribute.role.name");
    if (StringUtil.isNotBlank(key)) {
        final List<String> nameList = attributes.get(key);
        if (nameList != null) {
            list.addAll(nameList);
        }
    }
    final String value = fessConfig.getSystemProperty("saml.default.roles");
    if (StringUtil.isNotBlank(value)) {
        split(value, ",").of(stream -> stream.forEach(list::add));
    }
    return list.stream().filter(StringUtil::isNotBlank).map(String::trim).toArray(n -> new String[n]);
}
Also used : ArrayList(java.util.ArrayList) StringUtil(org.codelibs.core.lang.StringUtil) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 13 with StringUtil

use of org.codelibs.core.lang.StringUtil in project fess by codelibs.

the class SuggestHelper method init.

@PostConstruct
public void init() {
    if (logger.isDebugEnabled()) {
        logger.debug("Initialize {}", this.getClass().getSimpleName());
    }
    fessConfig = ComponentUtil.getFessConfig();
    split(fessConfig.getSuggestFieldContents(), ",").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(contentFieldNameSet::add));
    split(fessConfig.getSuggestFieldTags(), ",").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(tagFieldNameSet::add));
    split(fessConfig.getSuggestFieldRoles(), ",").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(roleFieldNameSet::add));
    contentFieldList = Arrays.asList(stream(fessConfig.getSuggestFieldContents()).get(stream -> stream.toArray(n -> new String[n])));
    final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
    searchEngineClient.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(fessConfig.getIndexHealthTimeout());
    final SuggestSettingsBuilder settingsBuilder = SuggestSettings.builder();
    settingsBuilder.addInitialSettings("elasticsearch.type", fessConfig.getFesenType());
    settingsBuilder.bulkTimeout(fessConfig.getIndexBulkTimeout());
    settingsBuilder.clusterTimeout(fessConfig.getIndexHealthTimeout());
    settingsBuilder.indexTimeout(fessConfig.getIndexIndexTimeout());
    settingsBuilder.indicesTimeout(fessConfig.getIndexIndicesTimeout());
    settingsBuilder.searchTimeout(fessConfig.getIndexSearchTimeout());
    suggester = Suggester.builder().settings(settingsBuilder).build(searchEngineClient, fessConfig.getIndexDocumentSuggestIndex());
    suggester.settings().array().delete(SuggestSettings.DefaultKeys.SUPPORTED_FIELDS);
    split(fessConfig.getSuggestFieldIndexContents(), ",").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(field -> {
        try {
            suggester.settings().array().add(SuggestSettings.DefaultKeys.SUPPORTED_FIELDS, field);
        } catch (final SuggestSettingsException e) {
            logger.warn("Failed to add {}", field, e);
        }
    }));
    suggester.createIndexIfNothing();
    if (ComponentUtil.hasPopularWordHelper()) {
        popularWordHelper = ComponentUtil.getPopularWordHelper();
    }
}
Also used : ThreadUtil(org.codelibs.core.lang.ThreadUtil) Arrays(java.util.Arrays) Constants(org.codelibs.fess.Constants) LocalDateTime(java.time.LocalDateTime) Pair(org.codelibs.core.misc.Pair) SortBuilders(org.opensearch.search.sort.SortBuilders) HashMap(java.util.HashMap) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) BadWordBhv(org.codelibs.fess.es.config.exbhv.BadWordBhv) FieldNames(org.codelibs.fess.suggest.constants.FieldNames) SuggestSettingsException(org.codelibs.fess.suggest.exception.SuggestSettingsException) ArrayList(java.util.ArrayList) SuggestUtil(org.codelibs.fess.suggest.util.SuggestUtil) HashSet(java.util.HashSet) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StreamUtil.split(org.codelibs.core.stream.StreamUtil.split) BadWord(org.codelibs.fess.es.config.exentity.BadWord) Map(java.util.Map) CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) ElevateWordBhv(org.codelibs.fess.es.config.exbhv.ElevateWordBhv) SuggestDeleteResponse(org.codelibs.fess.suggest.index.SuggestDeleteResponse) QueryBuilders(org.opensearch.index.query.QueryBuilders) ESSourceReader(org.codelibs.fess.suggest.index.contents.document.ESSourceReader) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Suggester(org.codelibs.fess.suggest.Suggester) SearchLog(org.codelibs.fess.es.log.exentity.SearchLog) SuggestSettingsBuilder(org.codelibs.fess.suggest.settings.SuggestSettingsBuilder) StringUtil(org.codelibs.core.lang.StringUtil) ScoreFunctionBuilders(org.opensearch.index.query.functionscore.ScoreFunctionBuilders) Set(java.util.Set) ZoneId(java.time.ZoneId) Consumer(java.util.function.Consumer) SuggestSettings(org.codelibs.fess.suggest.settings.SuggestSettings) List(java.util.List) Logger(org.apache.logging.log4j.Logger) SearchLogBhv(org.codelibs.fess.es.log.exbhv.SearchLogBhv) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DateTimeFormatter(java.time.format.DateTimeFormatter) SuggestItem(org.codelibs.fess.suggest.entity.SuggestItem) PostConstruct(javax.annotation.PostConstruct) ElevateWord(org.codelibs.fess.es.config.exentity.ElevateWord) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) SuggestSettingsBuilder(org.codelibs.fess.suggest.settings.SuggestSettingsBuilder) StringUtil(org.codelibs.core.lang.StringUtil) SuggestSettingsException(org.codelibs.fess.suggest.exception.SuggestSettingsException) PostConstruct(javax.annotation.PostConstruct)

Example 14 with StringUtil

use of org.codelibs.core.lang.StringUtil in project fess by codelibs.

the class SystemHelper method setLogLevel.

public void setLogLevel(final String level) {
    final Level logLevel = Level.toLevel(level, Level.WARN);
    System.setProperty(Constants.FESS_LOG_LEVEL, logLevel.toString());
    split(ComponentUtil.getFessConfig().getLoggingAppPackages(), ",").of(stream -> stream.map(String::trim).filter(StringUtil::isNotEmpty).forEach(s -> Configurator.setLevel(s, logLevel)));
}
Also used : ThreadUtil(org.codelibs.core.lang.ThreadUtil) Constants(org.codelibs.fess.Constants) IORuntimeException(org.codelibs.core.exception.IORuntimeException) UserMessagesCreator(org.lastaflute.core.message.supplier.UserMessagesCreator) ActionValidator(org.lastaflute.web.validation.ActionValidator) LoadingCache(com.google.common.cache.LoadingCache) Date(java.util.Date) Level(org.apache.logging.log4j.Level) FessActionValidator(org.codelibs.fess.validation.FessActionValidator) StringUtils(org.apache.commons.lang3.StringUtils) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) InetAddress(java.net.InetAddress) PreDestroy(javax.annotation.PreDestroy) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StreamUtil.split(org.codelibs.core.stream.StreamUtil.split) ULocale(com.ibm.icu.util.ULocale) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) GsaConfigParser(org.codelibs.fess.util.GsaConfigParser) Path(java.nio.file.Path) ParameterUtil(org.codelibs.fess.util.ParameterUtil) TimeZone(java.util.TimeZone) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) Logger(org.apache.logging.log4j.Logger) OsProbe(org.opensearch.monitor.os.OsProbe) ComponentUtil(org.codelibs.fess.util.ComponentUtil) Entry(java.util.Map.Entry) PostConstruct(javax.annotation.PostConstruct) LaServletContextUtil(org.lastaflute.web.util.LaServletContextUtil) Pattern(java.util.regex.Pattern) CacheBuilder(com.google.common.cache.CacheBuilder) TypicalAction(org.lastaflute.web.TypicalAction) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FessSystemException(org.codelibs.fess.exception.FessSystemException) LocalDateTime(java.time.LocalDateTime) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pair(org.codelibs.core.misc.Pair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) FessUserBean(org.codelibs.fess.mylasta.action.FessUserBean) LocaleUtils(org.apache.commons.lang3.LocaleUtils) ResourceUtil(org.codelibs.fess.util.ResourceUtil) Calendar(java.util.Calendar) RequestManager(org.lastaflute.web.servlet.request.RequestManager) Configurator(org.apache.logging.log4j.core.config.Configurator) CharUtil(org.codelibs.fess.crawler.util.CharUtil) HtmlResponse(org.lastaflute.web.response.HtmlResponse) FessMessages(org.codelibs.fess.mylasta.action.FessMessages) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Properties(java.util.Properties) Files(java.nio.file.Files) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) URLEncoder(java.net.URLEncoder) ServletContext(javax.servlet.ServletContext) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) Level(org.apache.logging.log4j.Level) StringUtil(org.codelibs.core.lang.StringUtil)

Example 15 with StringUtil

use of org.codelibs.core.lang.StringUtil in project fess by codelibs.

the class PopularWordHelper method getWordList.

public List<String> getWordList(final SearchRequestType searchRequestType, final String seed, final String[] tags, final String[] roles, final String[] fields, final String[] excludes) {
    final String baseSeed = seed != null ? seed : fessConfig.getSuggestPopularWordSeed();
    final String[] baseTags = tags != null ? tags : fessConfig.getSuggestPopularWordTagsAsArray();
    final String[] baseRoles = roles != null ? roles : ComponentUtil.getRoleQueryHelper().build(searchRequestType).stream().filter(StringUtil::isNotBlank).toArray(n -> new String[n]);
    final String[] baseFields = fields != null ? fields : fessConfig.getSuggestPopularWordFieldsAsArray();
    final String[] baseExcludes = excludes != null ? excludes : fessConfig.getSuggestPopularWordExcludesAsArray();
    try {
        return cache.get(getCacheKey(baseSeed, baseTags, baseRoles, baseFields, baseExcludes), () -> {
            final List<String> wordList = new ArrayList<>();
            final SuggestHelper suggestHelper = ComponentUtil.getSuggestHelper();
            final PopularWordsRequestBuilder popularWordsRequestBuilder = suggestHelper.suggester().popularWords().setSize(fessConfig.getSuggestPopularWordSizeAsInteger()).setWindowSize(fessConfig.getSuggestPopularWordWindowSizeAsInteger()).setQueryFreqThreshold(fessConfig.getSuggestPopularWordQueryFreqAsInteger());
            popularWordsRequestBuilder.setSeed(baseSeed);
            stream(baseTags).of(stream -> stream.forEach(tag -> popularWordsRequestBuilder.addTag(tag)));
            stream(baseRoles).of(stream -> stream.forEach(role -> popularWordsRequestBuilder.addRole(role)));
            stream(baseFields).of(stream -> stream.forEach(field -> popularWordsRequestBuilder.addField(field)));
            stream(baseExcludes).of(stream -> stream.forEach(exclude -> popularWordsRequestBuilder.addExcludeWord(exclude)));
            try {
                popularWordsRequestBuilder.execute().getResponse().getItems().stream().forEach(item -> wordList.add(item.getText()));
            } catch (final SuggesterException e) {
                logger.warn("Failed to generate popular words.", e);
            }
            return wordList;
        });
    } catch (final ExecutionException e) {
        logger.warn("Failed to load popular words.", e);
    }
    return Collections.emptyList();
}
Also used : StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) StringUtil(org.codelibs.core.lang.StringUtil) PopularWordsRequestBuilder(org.codelibs.fess.suggest.request.popularwords.PopularWordsRequestBuilder) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException) ComponentUtil(org.codelibs.fess.util.ComponentUtil) PostConstruct(javax.annotation.PostConstruct) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) ArrayList(java.util.ArrayList) PopularWordsRequestBuilder(org.codelibs.fess.suggest.request.popularwords.PopularWordsRequestBuilder) StringUtil(org.codelibs.core.lang.StringUtil) ExecutionException(java.util.concurrent.ExecutionException) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException)

Aggregations

StringUtil (org.codelibs.core.lang.StringUtil)46 ComponentUtil (org.codelibs.fess.util.ComponentUtil)41 Constants (org.codelibs.fess.Constants)40 StreamUtil.stream (org.codelibs.core.stream.StreamUtil.stream)38 Collectors (java.util.stream.Collectors)35 List (java.util.List)30 StreamUtil.split (org.codelibs.core.stream.StreamUtil.split)27 PermissionHelper (org.codelibs.fess.helper.PermissionHelper)26 ArrayList (java.util.ArrayList)25 Resource (javax.annotation.Resource)20 CrudMode (org.codelibs.fess.app.web.CrudMode)19 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)19 OptionalThing (org.dbflute.optional.OptionalThing)19 Execute (org.lastaflute.web.Execute)19 Map (java.util.Map)17 Stream (java.util.stream.Stream)17 SystemHelper (org.codelibs.fess.helper.SystemHelper)17 OptionalEntity (org.dbflute.optional.OptionalEntity)17 LogManager (org.apache.logging.log4j.LogManager)16 Logger (org.apache.logging.log4j.Logger)16