Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,17 @@ public void forIdOrInTitle(String searchInput) {
* Searches the index and inserts the IDs into the HQL query parameters.
*/
public void performIndexSearches() {
performIndexSearches(false);
}

/**
* Searches the index and inserts the IDs into the HQL query parameters.
*/
public void performIndexSearches(boolean useScroll) {
for (var iterator = indexQueries.entrySet().iterator(); iterator.hasNext();) {
Entry<String, Pair<FilterField, String>> entry = iterator.next();
Collection<Integer> ids = indexingService.searchIds(Process.class, entry.getValue().getLeft()
.getSearchField(), entry.getValue().getRight());
.getSearchField(), entry.getValue().getRight(), useScroll);
parameters.put(entry.getKey(), ids.isEmpty() ? NO_HIT : ids);
iterator.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ public List<ProcessExportDTO> getProcessesForExport(
query.addBooleanRestriction("project.active", Boolean.TRUE);
}
query.restrictToClient(sessionClientId);
query.performIndexSearches();
query.performIndexSearches(true);

query.addInnerJoin("project proj");
query.defineSorting("id", SortOrder.ASCENDING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package org.kitodo.production.services.index;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
Expand All @@ -20,6 +21,8 @@
import org.apache.logging.log4j.Logger;
import org.hibernate.exception.DataException;
import org.hibernate.search.engine.search.projection.SearchProjection;
import org.hibernate.search.engine.search.query.SearchScroll;
import org.hibernate.search.engine.search.query.SearchScrollResult;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.massindexing.MassIndexer;
import org.hibernate.search.mapper.orm.session.SearchSession;
Expand Down Expand Up @@ -124,23 +127,54 @@ public CompletionStage<?> startIndexing(Class<? extends BaseBean> type, MassInde

/**
* Searches for a search term in a search field and returns the hit 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 useScroll
* whether to use scrolling (for large result sets like exports)
* @return ids of the found beans
*/
public Collection<Integer> searchIds(Class<? extends BaseBean> beanClass, String searchField, String value) {
public Collection<Integer> searchIds(Class<? extends BaseBean> beanClass,
String searchField,
String value,
boolean useScroll) {

SearchSession searchSession = Search.session(HibernateUtil.getSession());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While reading the code changes here: HibernateUtil.getSession() is returing an Hibernate-Session which implements AutoCloseable interface. Should here and the other places in this class this usage replaced by a try-with-ressources statement? I noticed that using the search beginning with 3.9.x consumes more resources than before and this could be a reason. Maybe this should be fixed in a separate pull request. I did not start a discussion why in a IndexingService labeled class search methods are defined and used instead doing this in a SearchService class like in 3.8.x.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will open a pull request to change it to use try-with-resource statement for the used Hibernate Session.

SearchProjection<Integer> idField = searchSession.scope(beanClass).projection().field("id", Integer.class)
SearchProjection<Integer> idField = searchSession.scope(beanClass)
.projection()
.field("id", Integer.class)
.toProjection();
List<Integer> 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());

if (!useScroll) {
List<Integer> ids = searchSession.search(beanClass)
.select(idField)
.where(f -> f.match().field(searchField).matching(value))
.fetchAll()
.hits();

logger.debug("Searching {} IDs in field \"{}\" for \"{}\": {} hits",
beanClass.getSimpleName(), searchField, value, ids.size());
return ids;
}

List<Integer> ids = new ArrayList<>();
try (SearchScroll<Integer> scroll = searchSession.search(beanClass)
.select(idField)
.where(f -> f.match().field(searchField).matching(value))
.scroll(10_000)) {
SearchScrollResult<Integer> chunk;
do {
chunk = scroll.next();
ids.addAll(chunk.hits());
} while (chunk.hasHits());
}

logger.debug("Scrolling {} IDs in field \"{}\" for \"{}\": {} hits",
beanClass.getSimpleName(), searchField, value, ids.size());
return ids;
}

Expand Down