Skip to content

Commit

Permalink
Alteration to index-discovery script to only (re-)index specific type…
Browse files Browse the repository at this point in the history
… of IndexableObject

Not compatible with `-b` option since this clears entire index first (& expect to rebuild it in its entirety)
Compatible with `-f` to force reindex specific type of IndexableObject
  • Loading branch information
MarieVerdonck committed Dec 29, 2023
1 parent 5a43e6b commit e4da12e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 14 additions & 2 deletions dspace-api/src/main/java/org/dspace/discovery/IndexClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package org.dspace.discovery;

import static org.dspace.discovery.IndexClientOptions.TYPE_OPTION;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
Expand All @@ -15,6 +17,7 @@

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
Expand Down Expand Up @@ -51,6 +54,11 @@ public void internalRun() throws Exception {
return;
}

String type = null;
if (commandLine.hasOption(TYPE_OPTION)) {
type = commandLine.getOptionValue(TYPE_OPTION);
}

/** Acquire from dspace-services in future */
/**
* new DSpace.getServiceManager().getServiceByName("org.dspace.discovery.SolrIndexer");
Expand Down Expand Up @@ -113,6 +121,10 @@ public void internalRun() throws Exception {
} else if (indexClientOptions == IndexClientOptions.BUILD ||
indexClientOptions == IndexClientOptions.BUILDANDSPELLCHECK) {
handler.logInfo("(Re)building index from scratch.");
if (StringUtils.isNotBlank(type)) {
handler.logWarning(String.format("Type option, %s, not applicable for entire index rebuild option, b" +
", type will be ignored", TYPE_OPTION));
}
indexer.deleteIndex();
indexer.createIndex(context);
if (indexClientOptions == IndexClientOptions.BUILDANDSPELLCHECK) {
Expand All @@ -133,14 +145,14 @@ public void internalRun() throws Exception {
} else if (indexClientOptions == IndexClientOptions.UPDATE ||
indexClientOptions == IndexClientOptions.UPDATEANDSPELLCHECK) {
handler.logInfo("Updating Index");
indexer.updateIndex(context, false);
indexer.updateIndex(context, false, type);
if (indexClientOptions == IndexClientOptions.UPDATEANDSPELLCHECK) {
checkRebuildSpellCheck(commandLine, indexer);
}
} else if (indexClientOptions == IndexClientOptions.FORCEUPDATE ||
indexClientOptions == IndexClientOptions.FORCEUPDATEANDSPELLCHECK) {
handler.logInfo("Updating Index");
indexer.updateIndex(context, true);
indexer.updateIndex(context, true, type);
if (indexClientOptions == IndexClientOptions.FORCEUPDATEANDSPELLCHECK) {
checkRebuildSpellCheck(commandLine, indexer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

package org.dspace.discovery;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.dspace.discovery.indexobject.factory.IndexObjectFactoryFactory;

/**
* This Enum holds all the possible options and combinations for the Index discovery script
Expand All @@ -29,6 +34,8 @@ public enum IndexClientOptions {
FORCEUPDATEANDSPELLCHECK,
HELP;

public static final String TYPE_OPTION = "t";

/**
* This method resolves the CommandLine parameters to figure out which action the index-discovery script should
* perform
Expand Down Expand Up @@ -71,11 +78,15 @@ protected static IndexClientOptions getIndexClientOption(CommandLine commandLine

protected static Options constructOptions() {
Options options = new Options();
List<String> indexableObjectTypes = IndexObjectFactoryFactory.getInstance().getIndexFactories().stream()
.map((indexFactory -> indexFactory.getType())).collect(Collectors.toList());

options
.addOption("r", "remove", true, "remove an Item, Collection or Community from index based on its handle");
options.addOption("i", "index", true,
"add or update an Item, Collection or Community based on its handle or uuid");
options.addOption(TYPE_OPTION, "type", true, "reindex only specific type of " +
"(re)indexable objects; options: " + Arrays.toString(indexableObjectTypes.toArray()));
options.addOption("c", "clean", false,
"clean existing index removing any documents that no longer exist in the db");
options.addOption("d", "delete", false,
Expand Down

0 comments on commit e4da12e

Please sign in to comment.