001/*
002 * (C) Copyright 2014 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 *     tdelprat
018 *     bdelbosc
019 */
020
021package org.nuxeo.elasticsearch.api;
022
023import java.io.IOException;
024import java.util.List;
025
026import org.elasticsearch.common.bytes.BytesReference;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.elasticsearch.commands.IndexingCommand;
029
030/**
031 * Interface to process indexing of documents
032 *
033 * @since 5.9.3
034 */
035public interface ElasticSearchIndexing {
036
037    /**
038     * Run a worker to process the {@link IndexingCommand}.
039     * <p>
040     * Asynchronous command schedules an indexing job and return.
041     * </p>
042     * <p>
043     * Synchronous command execute an indexing job using a new Tx then refresh the index so the document is searchable
044     * immediately. if the command is also recursive the children are processed asynchronously.
045     * </p>
046     * <p>
047     * If there is more than one cmd the elasticsearch request is done in bulk mode.
048     * </p>
049     *
050     * @since 7.1
051     */
052    void runIndexingWorker(List<IndexingCommand> cmds);
053
054    /**
055     * Reindex documents matching the NXQL query, This is done in an asynchronous job.
056     *
057     * @since 7.1
058     */
059    default void runReindexingWorker(String repositoryName, String nxql) {
060        runReindexingWorker(repositoryName, nxql, false);
061    }
062
063    /**
064     * Reindex documents matching the NXQL query, This is done in an asynchronous job. When syncAlias is true a call is
065     * made to sync the search alias with write alias once indexing is done.
066     *
067     * @since 9.3
068     */
069    void runReindexingWorker(String repositoryName, String nxql, boolean syncAlias);
070
071    /**
072     * Recreate an index and run an async reindexing worker.
073     *
074     * @since 9.3
075     */
076    void reindexRepository(String repositoryName);
077
078    /**
079     * Process the {@link IndexingCommand}.
080     * <p>
081     * Send indexing command to Elasticsearch, if the command is synchronous the index is refreshed so the document is
082     * searchable immediately. Recursive indexing is not taken in account except for deletion. This is not a
083     * transactional operation, a rollback will not discard the executed commands.
084     * </p>
085     *
086     * @since 7.1
087     */
088    void indexNonRecursive(IndexingCommand cmd);
089
090    /**
091     * Same as {@link ElasticSearchIndexing#indexNonRecursive(org.nuxeo.elasticsearch.commands.IndexingCommand)} but
092     * process the list command using a bulk request.
093     * </p>
094     *
095     * @since 7.1
096     */
097    void indexNonRecursive(List<IndexingCommand> cmds);
098
099    /**
100     * Returns the JSON Elasticsearch source representation of a document.
101     *
102     * @since 10.3
103     */
104    BytesReference source(DocumentModel doc) throws IOException;
105}