Search in sources :

Example 1 with LanguageHelper

use of org.codelibs.fess.helper.LanguageHelper 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

SearchEngineClient (org.codelibs.fess.es.client.SearchEngineClient)1 LabelTypeHelper (org.codelibs.fess.helper.LabelTypeHelper)1 LanguageHelper (org.codelibs.fess.helper.LanguageHelper)1 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)1 Script (org.opensearch.script.Script)1