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.NuxeoException; 031import org.nuxeo.ecm.core.api.NuxeoPrincipal; 032import org.nuxeo.elasticsearch.api.ElasticSearchAdmin; 033import org.nuxeo.elasticsearch.api.ElasticSearchIndexing; 034import org.nuxeo.elasticsearch.commands.IndexingCommand; 035 036import java.util.Arrays; 037 038/** 039 * Run Elasticsearch indexing operation 040 * 041 * @since 8.1 042 */ 043@Operation(id = ElasticsearchIndexOperation.ID, category = Constants.CAT_SERVICES, label = "Elasticsearch Indexing", since = "8.1", 044 description = "Enable to index Nuxeo documents.") 045public class ElasticsearchIndexOperation { 046 047 public static final String ID = "Elasticsearch.Index"; 048 049 private static final Log log = LogFactory.getLog(Log.class); 050 051 @Context 052 protected OperationContext ctx; 053 054 @Context 055 protected ElasticSearchIndexing esi; 056 057 @Context 058 protected ElasticSearchAdmin esa; 059 060 @Context 061 protected CoreSession repo; 062 063 @OperationMethod 064 public void run() { 065 checkAccess(); 066 esi.reindexRepository(repo.getRepositoryName()); 067 } 068 069 private void checkAccess() { 070 NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal(); 071 if (principal == null || ! principal.isAdministrator()) { 072 throw new NuxeoException("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}