/** * Reindex documents matching the NXQL query, This is done in an asynchronous job. * * @since 7.1 */ default void runReindexingWorker(String repositoryName, String nxql) { runReindexingWorker(repositoryName, nxql, false); }
@Override protected void doWork() { ElasticSearchIndexing esi = Framework.getService(ElasticSearchIndexing.class); openSystemSession(); int bucketSize = Math.min(documentCount, getBucketSize()); List<String> ids = new ArrayList<>(bucketSize); for (DocumentLocation doc : getDocuments()) { ids.add(doc.getIdRef().value); if ((ids.size() % bucketSize) == 0) { esi.indexNonRecursive(getIndexingCommands(session, ids)); ids.clear(); TransactionHelper.commitOrRollbackTransaction(); TransactionHelper.startTransaction(); } } if (!ids.isEmpty()) { esi.indexNonRecursive(getIndexingCommands(session, ids)); ids.clear(); } if (syncAlias) { log.warn(String.format("Re-indexing job: %s completed.", getSchedulePath().getParentPath())); ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class); esa.syncSearchAndWriteAlias(esa.getIndexNameForRepository(repositoryName)); } }
@Override public void afterCompletion(int status) { try { if (getAllCommands().isEmpty()) { // return and un hook the current listener even if there's no commands to index // unless, during next transaction this listener won't be hooked to it return; } if (Status.STATUS_MARKED_ROLLBACK == status || Status.STATUS_ROLLEDBACK == status) { return; } List<IndexingCommand> commandList = new ArrayList<>(); for (IndexingCommands cmds : getAllCommands().values()) { for (IndexingCommand cmd : cmds.getCommands()) { commandList.add(cmd); } } ElasticSearchIndexing esi = Framework.getService(ElasticSearchIndexing.class); esi.runIndexingWorker(commandList); } finally { isEnlisted.set(false); getAllCommands().clear(); useSyncIndexing.set(null); } }
@OperationMethod public void run() { checkAccess(); esi.reindexRepository(repo.getRepositoryName()); }
@Override protected void compute(CoreSession session, List<String> documentIds, Map<String, Serializable> properties) { long now = Timestamp.currentTimeMicros(); String indexName = getIndexName(session, properties); DocumentModelList docs = loadDocuments(session, documentIds); ElasticSearchIndexing esi = Framework.getService(ElasticSearchIndexing.class); for (DocumentModel doc : docs) { try { append(new IndexRequest(indexName, DOC_TYPE, doc.getId()).source(esi.source(doc), XContentType.JSON) .versionType(VersionType.EXTERNAL) .version(now)); } catch (IOException e) { throw new NuxeoException("Cannot build source for document: " + doc.getId(), e); } } }
/** * Index synchronously reordered children. * * @param parentDocument */ public void indexReorderedChildren(DocumentModel parentDocument) { // Filter on trashed and version children Filter filter = new Filter() { private static final long serialVersionUID = 6829091503899474742L; @Override public boolean accept(DocumentModel docModel) { boolean notTrashed = !StringUtils.equals(LifeCycleConstants.DELETED_STATE, docModel.getCurrentLifeCycleState()); return !docModel.isVersion() && notTrashed; } }; // Direct children DocumentModelIterator childrenIterator = this.session.getChildrenIterator(parentDocument.getRef(), null, null, filter); if (childrenIterator != null) { List<IndexingCommand> cmds = new ArrayList<>(2); // Commands while (childrenIterator.hasNext()) { cmds.add(new IndexingCommand(childrenIterator.next(), IndexingCommand.Type.UPDATE, true, false)); } // Synchronous indexing ElasticSearchIndexing esi = (ElasticSearchIndexing) Framework.getService(ElasticSearchIndexing.class); esi.indexNonRecursive(cmds); } }
@OperationMethod public void run(DocumentModel doc) { checkAccess(); // 1. delete existing index IndexingCommand cmd = new IndexingCommand(doc, IndexingCommand.Type.DELETE, false, true); esi.runIndexingWorker(Arrays.asList(cmd)); // 2. wait for the deletion to be completed try { esa.prepareWaitForIndexing().get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new NuxeoException("Interrupted"); } catch (ExecutionException e) { throw new NuxeoException(e); } // 3. index recursive from path cmd = new IndexingCommand(doc, IndexingCommand.Type.INSERT, false, true); esi.runIndexingWorker(Arrays.asList(cmd)); }
@Override protected void doIndexingWork(ElasticSearchIndexing esi, List<IndexingCommand> cmds) { long now = Timestamp.currentTimeMicros(); for (IndexingCommand cmd : cmds) { cmd.setOrder(now); } esi.indexNonRecursive(cmds); WorkManager wm = Framework.getService(WorkManager.class); for (IndexingCommand cmd : cmds) { if (needRecurse(cmd)) { wm.schedule(getWorker(cmd)); } } }
@OperationMethod public void run(String nxql) { checkAccess(); esi.runReindexingWorker(repo.getRepositoryName(), nxql); }
@Override protected void doIndexingWork(ElasticSearchIndexing esi, List<IndexingCommand> cmds) { if (cmds.isEmpty()) { return; } IndexingCommand cmd = cmds.get(0); DocumentModel doc = getDocument(cmd); if (doc == null) { return; } DocumentModelIterator iter = session.getChildrenIterator(doc.getRef()); while (iter.hasNext()) { // Add a session save to process cache invalidation session.save(); DocumentModel child = iter.next(); IndexingCommand childCommand = cmd.clone(child); esi.indexNonRecursive(childCommand); if (child.isFolder()) { ChildrenIndexingWorker subWorker = new ChildrenIndexingWorker(childCommand); WorkManager wm = Framework.getService(WorkManager.class); wm.schedule(subWorker); } } }