001/*
002 * (C) Copyright 2018-2020 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Benoit Delbosc
018 */
019package org.nuxeo.ecm.automation.elasticsearch;
020
021import static org.nuxeo.elasticsearch.bulk.IndexAction.ACTION_NAME;
022import static org.nuxeo.elasticsearch.bulk.IndexAction.INDEX_UPDATE_ALIAS_PARAM;
023
024import java.io.IOException;
025import java.util.Collections;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.core.Constants;
031import org.nuxeo.ecm.automation.core.annotations.Context;
032import org.nuxeo.ecm.automation.core.annotations.Operation;
033import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.Blobs;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.NuxeoException;
038import org.nuxeo.ecm.core.api.NuxeoPrincipal;
039import org.nuxeo.ecm.core.bulk.BulkService;
040import org.nuxeo.ecm.core.bulk.message.BulkCommand;
041import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
042
043/**
044 * Run Elasticsearch indexing operation using the Bulk Service
045 *
046 * @since 10.3
047 */
048@Operation(id = ElasticsearchBulkIndexOperation.ID, category = Constants.CAT_SERVICES, label = "Elasticsearch Indexing", since = "10.3", description = "Enable to index Nuxeo documents using the Bulk Service.", addToStudio = false)
049public class ElasticsearchBulkIndexOperation {
050    private static final Log log = LogFactory.getLog(ElasticsearchBulkIndexOperation.class);
051
052    public static final String ID = "Elasticsearch.BulkIndex";
053
054    @Context
055    protected CoreSession session;
056
057    @Context
058    protected OperationContext ctx;
059
060    @Context
061    protected ElasticSearchAdmin esa;
062
063    @Context
064    protected BulkService bulkService;
065
066    @OperationMethod
067    public Blob run() throws IOException {
068        checkAccess();
069        esa.dropAndInitRepositoryIndex(session.getRepositoryName(), false);
070        String commandId = submitBulkCommand("SELECT ecm:uuid FROM Document", true);
071        log.warn(String.format("Submitted index command: %s to index the entire %s repository.", commandId,
072                session.getRepositoryName()));
073        return Blobs.createJSONBlobFromValue(Collections.singletonMap("commandId", commandId));
074    }
075
076    protected String submitBulkCommand(String nxql, boolean syncAlias) {
077        String username = session.getPrincipal().getName();
078        String repository = session.getRepositoryName();
079        return bulkService.submit( //
080                new BulkCommand.Builder(ACTION_NAME, nxql, username).repository(repository)
081                                                                    .param(INDEX_UPDATE_ALIAS_PARAM, syncAlias)
082                                                                    .build());
083    }
084
085    protected void checkAccess() {
086        NuxeoPrincipal principal = ctx.getPrincipal();
087        if (principal == null || !principal.isAdministrator()) {
088            throw new NuxeoException("Unauthorized access: " + principal);
089        }
090    }
091
092    @OperationMethod
093    public Blob run(String nxql) throws IOException {
094        checkAccess();
095        String commandId = submitBulkCommand(nxql, false);
096        return Blobs.createJSONBlobFromValue(Collections.singletonMap("commandId", commandId));
097    }
098
099}