From b7f6236f9dcc10692e5184f9ccaea8045a55006f Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Wed, 28 Aug 2024 15:56:12 +0200 Subject: [PATCH] Delete ElasticSearch support from Kitodo - Core --- Kitodo/pom.xml | 4 - .../services/data/base/SearchService.java | 200 ------------------ .../test/java/org/kitodo/MockDatabase.java | 72 +------ 3 files changed, 7 insertions(+), 269 deletions(-) diff --git a/Kitodo/pom.xml b/Kitodo/pom.xml index ca0652e0097..44d4a01f73f 100644 --- a/Kitodo/pom.xml +++ b/Kitodo/pom.xml @@ -177,10 +177,6 @@ org.apache.avalon.framework avalon-framework-impl - - org.elasticsearch.client - transport - org.jdom jdom2 diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java index b1ea505dc87..72f68c70383 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java @@ -11,26 +11,14 @@ package org.kitodo.production.services.data.base; -import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery; -import static org.elasticsearch.index.query.QueryBuilders.termsQuery; - import java.io.IOException; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.index.query.BoolQueryBuilder; -import org.elasticsearch.index.query.Operator; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.sort.SortBuilder; -import org.elasticsearch.search.sort.SortBuilders; -import org.kitodo.data.database.beans.BaseBean; import org.kitodo.data.database.beans.BaseIndexedBean; import org.kitodo.data.database.enums.IndexAction; import org.kitodo.data.database.exceptions.DAOException; @@ -38,7 +26,6 @@ import org.kitodo.data.elasticsearch.exceptions.CustomResponseException; import org.kitodo.data.exceptions.DataException; import org.kitodo.data.interfaces.DataInterface; -import org.primefaces.model.SortOrder; /** * Class for implementing methods used by all service classes which search in @@ -266,16 +253,6 @@ protected List convertJSONObjectsToInterfaces(List> jsonO return results; } - /** - * Builds a ElasticSearch query for list of Ids. - * - * @param ids as a List of Integer - * @return query as QueryBuilder - */ - public QueryBuilder createSetQueryForIds(List ids) { - return termsQuery("_id", ids); - } - /** * Get id from JSON object returned form ElasticSearch. * @@ -293,183 +270,6 @@ public Integer getIdFromJSONObject(Map jsonObject) { return 0; } - /** - * Create query for set of data. - * - * @param key - * JSON key for searched object - * @param values - * set of values for searched objects or some objects related to - * searched object - * @param contains - * determine if results should contain given value or should not - * contain given value - * @return query - */ - protected QueryBuilder createSetQuery(String key, Set values, boolean contains) { - if (contains && !values.isEmpty()) { - return termsQuery(key, values); - } else if (!contains && Objects.nonNull(values)) { - BoolQueryBuilder boolQuery = new BoolQueryBuilder(); - return boolQuery.mustNot(termsQuery(key, values)); - } else { - return matchQuery(key, 0); - } - } - - protected QueryBuilder createSetQuery(String key, List> values, boolean contains) { - Set valuesIds = new HashSet<>(); - for (Map value : values) { - valuesIds.add(getIdFromJSONObject(value)); - } - - return createSetQuery(key, valuesIds, contains); - } - - protected QueryBuilder createSetQueryForBeans(String key, List values, boolean contains) { - Set valuesIds = new HashSet<>(); - for (BaseBean value : values) { - valuesIds.add(value.getId()); - } - - return createSetQuery(key, valuesIds, contains); - } - - /** - * Used for cases where operator is not necessary to create query - checking - * only for one parameter. - * - * @param key - * JSON key for searched object - * @param id - * id value for searched object or some object related to searched - * object - * @param contains - * determine if results should contain given value or should not - * contain given value - * @return query - */ - protected QueryBuilder createSimpleQuery(String key, Integer id, boolean contains) { - if (contains && Objects.nonNull(id)) { - return matchQuery(key, id); - } else if (!contains && Objects.nonNull(id)) { - BoolQueryBuilder boolQuery = new BoolQueryBuilder(); - return boolQuery.mustNot(matchQuery(key, id)); - } else { - return matchQuery(key, 0); - } - } - - /** - * Used for cases where operator is not necessary to create query - checking - * only for one parameter. - * - * @param key - * JSON key for searched object - * @param condition - * id value for searched object or some object related to searched - * object - * @param contains - * determine if results should contain given value or should not - * contain given value - * @return query - */ - protected QueryBuilder createSimpleQuery(String key, Boolean condition, boolean contains) { - if (contains && Objects.nonNull(condition)) { - return matchQuery(key, condition); - } else if (!contains && Objects.nonNull(condition)) { - BoolQueryBuilder boolQuery = new BoolQueryBuilder(); - return boolQuery.mustNot(matchQuery(key, condition)); - } else { - return matchQuery(key, false); - } - } - - /** - * Used for cases where operator is not necessary to create query - checking - * only for one parameter. - * - * @param key - * JSON key for searched object - * @param value - * JSON value for searched object - * @param contains - * determine if results should contain given value or should not - * contain given value - * @return query - */ - protected QueryBuilder createSimpleQuery(String key, String value, boolean contains) { - if (contains) { - return matchQuery(key, value); - } else { - BoolQueryBuilder boolQuery = new BoolQueryBuilder(); - return boolQuery.mustNot(matchQuery(key, value)); - } - } - - /** - * Used for cases where operator is necessary to create query - checking for - * more than one parameter. - * - * @param key - * JSON key for searched object - * @param value - * JSON value for searched object - * @param contains - * determine if results should contain given value or should not - * contain given value - * @param operator - * as Operator AND or OR - useful when value contains more than one - * word - * @return query - */ - protected QueryBuilder createSimpleQuery(String key, String value, boolean contains, Operator operator) { - if (Objects.isNull(operator)) { - operator = Operator.OR; - } - - if (contains) { - return matchQuery(key, value).operator(operator); - } else { - BoolQueryBuilder boolQuery = new BoolQueryBuilder(); - return boolQuery.mustNot(matchQuery(key, value).operator(operator)); - } - } - - protected QueryBuilder createSimpleWildcardQuery(String key, String value) { - return queryStringQuery(key + ".keyword: *" + value + "*"); - } - - /** - * Converts properties' values returned from ElasticSearch index. - * - * @param object - * JSONObject - * @return display properties as list of Integers - */ - @SuppressWarnings("unchecked") - private List getRelatedPropertyFor(Map object, String key) { - if (Objects.nonNull(object)) { - List> jsonArray = (List>) object.get(key); - List ids = new ArrayList<>(); - for (Map singleObject : jsonArray) { - ids.add((Integer) singleObject.get("id")); - } - return ids; - } - return new ArrayList<>(); - } - - protected SortBuilder getSortBuilder(String sortField, SortOrder sortOrder) { - if (!Objects.equals(sortField, null) && Objects.equals(sortOrder, SortOrder.ASCENDING)) { - return SortBuilders.fieldSort(sortField).order(org.elasticsearch.search.sort.SortOrder.ASC); - } else if (!Objects.equals(sortField, null) && Objects.equals(sortOrder, SortOrder.DESCENDING)) { - return SortBuilders.fieldSort(sortField).order(org.elasticsearch.search.sort.SortOrder.DESC); - } else { - return null; - } - } - /** * Removes all objects from index, which are no longer in Database. * @param baseIndexedBeansId the list of beans to check for missing db eintries. diff --git a/Kitodo/src/test/java/org/kitodo/MockDatabase.java b/Kitodo/src/test/java/org/kitodo/MockDatabase.java index 77d900a2628..ca3bab1cc20 100644 --- a/Kitodo/src/test/java/org/kitodo/MockDatabase.java +++ b/Kitodo/src/test/java/org/kitodo/MockDatabase.java @@ -11,7 +11,13 @@ package org.kitodo; -import java.io.File; +import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp; +import static com.xebialabs.restito.semantics.Condition.get; +import static com.xebialabs.restito.semantics.Condition.parameter; + +import com.xebialabs.restito.semantics.Action; +import com.xebialabs.restito.server.StubServer; + import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -24,7 +30,6 @@ import java.time.ZoneId; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -33,21 +38,12 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; -import java.util.function.Supplier; -import com.xebialabs.restito.semantics.Action; -import com.xebialabs.restito.server.StubServer; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.awaitility.Awaitility; import org.awaitility.Durations; -import org.elasticsearch.common.io.FileSystemUtils; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.node.InternalSettingsPreparer; -import org.elasticsearch.node.Node; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.transport.Netty4Plugin; import org.h2.tools.Server; import org.hibernate.Session; import org.hibernate.Transaction; @@ -55,7 +51,6 @@ import org.kitodo.api.externaldatamanagement.SearchInterfaceType; import org.kitodo.api.schemaconverter.FileFormat; import org.kitodo.api.schemaconverter.MetadataFormat; -import org.kitodo.config.ConfigMain; import org.kitodo.data.database.beans.Authority; import org.kitodo.data.database.beans.Batch; import org.kitodo.data.database.beans.Client; @@ -91,7 +86,6 @@ import org.kitodo.exceptions.WorkflowException; import org.kitodo.production.enums.ObjectType; import org.kitodo.production.enums.ProcessState; -import org.kitodo.production.helper.Helper; import org.kitodo.production.process.ProcessGenerator; import org.kitodo.production.security.password.SecurityPasswordEncoder; import org.kitodo.production.services.ServiceManager; @@ -100,21 +94,13 @@ import org.kitodo.test.utils.ProcessTestUtils; import org.kitodo.test.utils.TestConstants; -import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp; -import static com.xebialabs.restito.semantics.Condition.get; -import static com.xebialabs.restito.semantics.Condition.parameter; - /** * Insert data to test database. */ public class MockDatabase { - private static Node node; - private static String testIndexName; private static final String GLOBAL_ASSIGNABLE = "_globalAssignable"; private static final String CLIENT_ASSIGNABLE = "_clientAssignable"; - private static final String HTTP_TRANSPORT_PORT = "9305"; - private static final String TARGET = "target"; private static final String CHOICE = "choice"; private static final String TEST = "test"; private static final String FIRST_VALUE = "first value"; @@ -146,26 +132,9 @@ public static void startNode() throws Exception { } public static void startNodeWithoutMapping() throws Exception { - String nodeName = Helper.generateRandomString(6); - final String port = ConfigMain.getParameter("elasticsearch.port", "9205"); - - testIndexName = ConfigMain.getParameter("elasticsearch.index", "testindex"); - - Settings settings = prepareNodeSettings(port, nodeName); - - removeOldDataDirectories("target/" + nodeName); - - if (node != null) { - stopNode(); - } - Supplier nodeNameSupplier = () -> nodeName; - node = new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class), nodeNameSupplier); - node.start(); } public static void stopNode() throws Exception { - node.close(); - node = null; } public static void setUpAwaitility() { @@ -241,33 +210,6 @@ public static void insertForDataEditorTesting() throws Exception { insertDataEditorSettings(); } - private static class ExtendedNode extends Node { - ExtendedNode(Settings preparedSettings, Collection> classpathPlugins, - Supplier nodeNameSupplier) { - super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, Collections.emptyMap(), - Paths.get("target"), nodeNameSupplier), classpathPlugins, false); - } - } - - private static void removeOldDataDirectories(String dataDirectory) throws Exception { - File dataDir = new File(dataDirectory); - if (dataDir.exists()) { - FileSystemUtils.deleteSubDirectories(dataDir.toPath()); - } - } - - private static Settings prepareNodeSettings(String httpPort, String nodeName) { - return Settings.builder().put("node.name", nodeName) - .put("path.data", TARGET) - .put("path.logs", TARGET) - .put("path.home", TARGET) - .put("http.type", "netty4") - .put("http.port", httpPort) - .put("transport.tcp.port", HTTP_TRANSPORT_PORT) - .put("transport.type", "netty4") - .put("action.auto_create_index", "false").build(); - } - public static void insertAuthorities() throws DAOException { List authorities = new ArrayList<>();