001/* 002 * (C) Copyright 2015 Nuxeo SA (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 org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.automation.OperationContext; 024import org.nuxeo.ecm.automation.core.Constants; 025import org.nuxeo.ecm.automation.core.annotations.Context; 026import org.nuxeo.ecm.automation.core.annotations.Operation; 027import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.NuxeoPrincipal; 031import org.nuxeo.elasticsearch.api.ElasticSearchAdmin; 032import org.nuxeo.elasticsearch.api.ElasticSearchIndexing; 033import org.nuxeo.elasticsearch.commands.IndexingCommand; 034 035import java.util.Arrays; 036 037/** 038 * Run Elasticsearch indexing operation 039 * 040 * @since 8.1 041 */ 042@Operation(id = ElasticsearchIndexOperation.ID, category = Constants.CAT_SERVICES, label = "Elasticsearch Indexing", since = "8.1", 043 description = "Enable to index Nuxeo documents.") 044public class ElasticsearchIndexOperation { 045 046 public static final String ID = "Elasticsearch.Index"; 047 048 private static final Log log = LogFactory.getLog(Log.class); 049 050 @Context 051 protected OperationContext ctx; 052 053 @Context 054 protected ElasticSearchIndexing esi; 055 056 @Context 057 protected ElasticSearchAdmin esa; 058 059 @Context 060 protected CoreSession repo; 061 062 @OperationMethod 063 public void run() { 064 checkAccess(); 065 esa.dropAndInitRepositoryIndex(repo.getRepositoryName()); 066 run("SELECT ecm:uuid FROM Document"); 067 } 068 069 private void checkAccess() { 070 NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal(); 071 if (principal == null || ! principal.isAdministrator()) { 072 throw new RuntimeException("Unauthorized access: " + principal); 073 } 074 } 075 076 @OperationMethod 077 public void run(String nxql) { 078 checkAccess(); 079 esi.runReindexingWorker(repo.getRepositoryName(), nxql); 080 } 081 082 @OperationMethod 083 public void run(DocumentModel doc) { 084 checkAccess(); 085 // 1. delete existing index 086 IndexingCommand cmd = new IndexingCommand(doc, IndexingCommand.Type.DELETE, false, true); 087 esi.runIndexingWorker(Arrays.asList(cmd)); 088 // 2. index recursive from path 089 cmd = new IndexingCommand(doc, IndexingCommand.Type.INSERT, false, true); 090 esi.runIndexingWorker(Arrays.asList(cmd)); 091 } 092 093}