diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/BeanQuery.java b/Kitodo/src/main/java/org/kitodo/production/services/data/BeanQuery.java index 297e2ba7da2..425e138227c 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/BeanQuery.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/BeanQuery.java @@ -224,11 +224,19 @@ public void forIdOrInTitle(String searchInput) { * Searches the index and inserts the IDs into the HQL query parameters. */ public void performIndexSearches() { + List> terms = new ArrayList<>(); for (var iterator = indexQueries.entrySet().iterator(); iterator.hasNext();) { Entry> entry = iterator.next(); - Collection ids = indexingService.searchIds(Process.class, entry.getValue().getLeft() - .getSearchField(), entry.getValue().getRight()); - parameters.put(entry.getKey(), ids.isEmpty() ? NO_HIT : ids); + String field = entry.getValue().getLeft().getSearchField(); + String token = entry.getValue().getRight(); + terms.add(Pair.of(field, token)); + } + Collection ids = indexingService.searchIds(Process.class, terms); + Collection finalIds = ids.isEmpty() ? NO_HIT : ids; + + for (var iterator = indexQueries.entrySet().iterator(); iterator.hasNext();) { + Entry> entry = iterator.next(); + parameters.put(entry.getKey(), finalIds); iterator.remove(); } } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java b/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java index 7d486da75f1..4ed941cf7a1 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java @@ -15,7 +15,9 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.CompletionStage; +import java.util.stream.Collectors; +import org.apache.commons.lang3.tuple.Pair; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hibernate.exception.DataException; @@ -123,24 +125,43 @@ public CompletionStage startIndexing(Class type, MassInde } /** - * Searches for a search term in a search field and returns the hit IDs. + * Searches for entities matching all given field/value terms and returns their IDs. * * @param beanClass * class of beans to search for - * @param searchField - * search field to search on - * @param value - * value to be found in the search field + * @param terms + * list of field/value pairs to match (AND-combined) * @return ids of the found beans */ - public Collection searchIds(Class beanClass, String searchField, String value) { + public Collection searchIds(Class beanClass, List> terms) { SearchSession searchSession = Search.session(HibernateUtil.getSession()); SearchProjection idField = searchSession.scope(beanClass).projection().field("id", Integer.class) .toProjection(); - List ids = searchSession.search(beanClass).select(idField).where(function -> function.match().field( - searchField).matching(value)).fetchAll().hits(); - logger.debug("Searching {} IDs in field \"{}\" for \"{}\": {} hits", beanClass.getSimpleName(), searchField, - value, ids.size()); + var query = searchSession.search(beanClass) + .select(idField) + .where(f -> { + var bool = f.bool(); + for (var term : terms) { + bool.must( + f.match() + .field(term.getLeft()) + .matching(term.getRight()) + ); + } + return bool; + }); + List ids = query.fetchAll().hits(); + + String termSummary = terms.stream() + .distinct() + .map(t -> t.getLeft() + "=\"***\"") + .collect(Collectors.joining(", ")); + logger.debug( + "Searching {} IDs with terms {}: {} hits", + beanClass.getSimpleName(), + termSummary, + ids.size() + ); return ids; } diff --git a/Kitodo/src/test/java/org/kitodo/production/services/command/KitodoScriptServiceIT.java b/Kitodo/src/test/java/org/kitodo/production/services/command/KitodoScriptServiceIT.java index 7d5f3bf20fd..c3b7a07948d 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/command/KitodoScriptServiceIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/command/KitodoScriptServiceIT.java @@ -554,7 +554,7 @@ public void shouldAddDataWithMultipleScripts() throws Exception { assertEquals(6, process.getSortHelperMetadata()); assertEquals(2, process.getSortHelperDocstructs()); - String script = "action:addData " + "key:" + metadataKey + " value:legal note;" + "key:" + metadataKey + " value:secondNote"; + String script = "action:addData key:" + metadataKey + " \"value:legal note\"; key:" + metadataKey + " \"value:secondNote\""; List processes = new ArrayList<>(); processes.add(process); KitodoScriptService kitodoScript = ServiceManager.getKitodoScriptService();