Search in sources :

Example 1 with SuggesterException

use of org.codelibs.fess.suggest.exception.SuggesterException 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

Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 PostConstruct (javax.annotation.PostConstruct)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 StringUtil (org.codelibs.core.lang.StringUtil)1 StreamUtil.stream (org.codelibs.core.stream.StreamUtil.stream)1 SearchRequestType (org.codelibs.fess.entity.SearchRequestParams.SearchRequestType)1 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)1 SuggesterException (org.codelibs.fess.suggest.exception.SuggesterException)1 PopularWordsRequestBuilder (org.codelibs.fess.suggest.request.popularwords.PopularWordsRequestBuilder)1 ComponentUtil (org.codelibs.fess.util.ComponentUtil)1