Search in sources :

Example 16 with SearchEngineClient

use of org.codelibs.fess.es.client.SearchEngineClient 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)

Example 17 with SearchEngineClient

use of org.codelibs.fess.es.client.SearchEngineClient in project fess by codelibs.

the class PurgeDocJob method execute.

public String execute() {
    final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final StringBuilder resultBuf = new StringBuilder();
    // clean up
    final QueryBuilder queryBuilder = QueryBuilders.rangeQuery(fessConfig.getIndexFieldExpires()).to("now");
    try {
        searchEngineClient.deleteByQuery(fessConfig.getIndexDocumentUpdateIndex(), queryBuilder);
    } catch (final Exception e) {
        logger.error("Could not delete expired documents: {}", queryBuilder.toString(), e);
        resultBuf.append(e.getMessage()).append("\n");
    }
    return resultBuf.toString();
}
Also used : SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) QueryBuilder(org.opensearch.index.query.QueryBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 18 with SearchEngineClient

use of org.codelibs.fess.es.client.SearchEngineClient in project fess by codelibs.

the class AdminSearchlistAction method getDoc.

public static OptionalEntity<Map<String, Object>> getDoc(final CreateForm form) {
    final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
    switch(form.crudMode) {
        case CrudMode.CREATE:
            final Map<String, Object> entity = new HashMap<>();
            entity.put(fessConfig.getIndexFieldDocId(), systemHelper.generateDocId(entity));
            return OptionalEntity.of(entity);
        case CrudMode.EDIT:
            final String docId;
            if (form.doc != null) {
                docId = (String) form.doc.get(fessConfig.getIndexFieldDocId());
            } else {
                docId = null;
            }
            if (StringUtil.isNotBlank(docId)) {
                return searchEngineClient.getDocument(fessConfig.getIndexDocumentUpdateIndex(), builder -> {
                    builder.setQuery(QueryBuilders.termQuery(fessConfig.getIndexFieldDocId(), docId));
                    return true;
                });
            }
            break;
        default:
            break;
    }
    return OptionalEntity.empty();
}
Also used : SystemHelper(org.codelibs.fess.helper.SystemHelper) HashMap(java.util.HashMap) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Aggregations

SearchEngineClient (org.codelibs.fess.es.client.SearchEngineClient)18 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)12 IndexingHelper (org.codelibs.fess.helper.IndexingHelper)7 LogManager (org.apache.logging.log4j.LogManager)4 Logger (org.apache.logging.log4j.Logger)4 StringUtil (org.codelibs.core.lang.StringUtil)4 SystemHelper (org.codelibs.fess.helper.SystemHelper)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 StreamUtil.stream (org.codelibs.core.stream.StreamUtil.stream)3 ComponentUtil (org.codelibs.fess.util.ComponentUtil)3 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 PostConstruct (javax.annotation.PostConstruct)2 ThreadUtil (org.codelibs.core.lang.ThreadUtil)2 Constants (org.codelibs.fess.Constants)2 ContainerNotAvailableException (org.codelibs.fess.exception.ContainerNotAvailableException)2