DS-3981 Improve IndexClient usage & options

This commit is contained in:
April Herron
2021-11-16 15:25:26 -05:00
parent ff8e002bbc
commit b8a81ac824
5 changed files with 69 additions and 62 deletions

View File

@@ -61,13 +61,14 @@ public class IndexClient extends DSpaceRunnable<IndexDiscoveryScriptConfiguratio
indexer.unIndexContent(context, commandLine.getOptionValue("r"));
} else if (indexClientOptions == IndexClientOptions.CLEAN) {
handler.logInfo("Cleaning Index");
indexer.cleanIndex(false);
} else if (indexClientOptions == IndexClientOptions.FORCECLEAN) {
handler.logInfo("Cleaning Index");
indexer.cleanIndex(true);
indexer.cleanIndex();
} else if (indexClientOptions == IndexClientOptions.DELETE) {
handler.logInfo("Deleting Index");
indexer.deleteIndex();
} else if (indexClientOptions == IndexClientOptions.BUILD ||
indexClientOptions == IndexClientOptions.BUILDANDSPELLCHECK) {
handler.logInfo("(Re)building index from scratch.");
indexer.deleteIndex();
indexer.createIndex(context);
if (indexClientOptions == IndexClientOptions.BUILDANDSPELLCHECK) {
checkRebuildSpellCheck(commandLine, indexer);
@@ -125,16 +126,14 @@ public class IndexClient extends DSpaceRunnable<IndexDiscoveryScriptConfiguratio
handler.logInfo("Indexed " + count + " object" + (count > 1 ? "s" : "") + " in " + seconds + " seconds");
} else if (indexClientOptions == IndexClientOptions.UPDATE ||
indexClientOptions == IndexClientOptions.UPDATEANDSPELLCHECK) {
handler.logInfo("Updating and Cleaning Index");
indexer.cleanIndex(false);
handler.logInfo("Updating Index");
indexer.updateIndex(context, false);
if (indexClientOptions == IndexClientOptions.UPDATEANDSPELLCHECK) {
checkRebuildSpellCheck(commandLine, indexer);
}
} else if (indexClientOptions == IndexClientOptions.FORCEUPDATE ||
indexClientOptions == IndexClientOptions.FORCEUPDATEANDSPELLCHECK) {
handler.logInfo("Updating and Cleaning Index");
indexer.cleanIndex(true);
handler.logInfo("Updating Index");
indexer.updateIndex(context, true);
if (indexClientOptions == IndexClientOptions.FORCEUPDATEANDSPELLCHECK) {
checkRebuildSpellCheck(commandLine, indexer);

View File

@@ -17,7 +17,7 @@ import org.apache.commons.cli.Options;
public enum IndexClientOptions {
REMOVE,
CLEAN,
FORCECLEAN,
DELETE,
BUILD,
BUILDANDSPELLCHECK,
OPTIMIZE,
@@ -41,11 +41,9 @@ public enum IndexClientOptions {
} else if (commandLine.hasOption("r")) {
return IndexClientOptions.REMOVE;
} else if (commandLine.hasOption("c")) {
if (commandLine.hasOption("f")) {
return IndexClientOptions.FORCECLEAN;
} else {
return IndexClientOptions.CLEAN;
}
} else if (commandLine.hasOption("d")) {
return IndexClientOptions.DELETE;
} else if (commandLine.hasOption("b")) {
if (commandLine.hasOption("s")) {
return IndexClientOptions.BUILDANDSPELLCHECK;
@@ -83,6 +81,9 @@ public enum IndexClientOptions {
options.addOption("c", "clean", false,
"clean existing index removing any documents that no longer exist in the db");
options.getOption("c").setType(boolean.class);
options.addOption("d", "delete", false,
"delete all records from existing index");
options.getOption("d").setType(boolean.class);
options.addOption("b", "build", false, "(re)build index, wiping out current one if it exists");
options.getOption("b").setType(boolean.class);
options.addOption("s", "spellchecker", false, "Rebuild the spellchecker, can be combined with -b and -f.");

View File

@@ -53,8 +53,9 @@ public interface IndexingService {
void updateIndex(Context context, boolean force, String type);
void cleanIndex(boolean force) throws IOException,
SQLException, SearchServiceException;
void cleanIndex() throws IOException, SQLException, SearchServiceException;
void deleteIndex();
void commit() throws SearchServiceException;

View File

@@ -333,17 +333,31 @@ public class SolrServiceImpl implements SearchService, IndexingService {
}
}
/**
* Removes all documents from the Lucene index
*/
public void deleteIndex() {
try {
final List<IndexFactory> indexableObjectServices = indexObjectServiceFactory.
getIndexFactories();
for (IndexFactory indexableObjectService : indexableObjectServices) {
indexableObjectService.deleteAll();
}
} catch (IOException | SolrServerException e) {
log.error("Error cleaning discovery index: " + e.getMessage(), e);
}
}
/**
* Iterates over all documents in the Lucene index and verifies they are in
* database, if not, they are removed.
*
* @param force whether or not to force a clean index
* @throws IOException IO exception
* @throws SQLException sql exception
* @throws SearchServiceException occurs when something went wrong with querying the solr server
*/
@Override
public void cleanIndex(boolean force) throws IOException, SQLException, SearchServiceException {
public void cleanIndex() throws IOException, SQLException, SearchServiceException {
Context context = new Context();
context.turnOffAuthorisationSystem();
@@ -351,13 +365,6 @@ public class SolrServiceImpl implements SearchService, IndexingService {
if (solrSearchCore.getSolr() == null) {
return;
}
if (force) {
final List<IndexFactory> indexableObjectServices = indexObjectServiceFactory.
getIndexFactories();
for (IndexFactory indexableObjectService : indexableObjectServices) {
indexableObjectService.deleteAll();
}
} else {
// First, we'll just get a count of the total results
SolrQuery countQuery = new SolrQuery("*:*");
countQuery.setRows(0); // don't actually request any data
@@ -401,7 +408,6 @@ public class SolrServiceImpl implements SearchService, IndexingService {
start += batch;
}
}
} catch (IOException | SQLException | SolrServerException e) {
log.error("Error cleaning discovery index: " + e.getMessage(), e);
} finally {

View File

@@ -1327,7 +1327,7 @@ public class DatabaseUtils {
// Reindex Discovery completely
// Force clean all content
this.indexer.cleanIndex(true);
this.indexer.deleteIndex();
// Recreate the entire index (overwriting existing one)
this.indexer.createIndex(context);
// Rebuild spell checker (which is based on index)