Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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,11 +224,19 @@ public void forIdOrInTitle(String searchInput) {
* Searches the index and inserts the IDs into the HQL query parameters.
*/
public void performIndexSearches() {
List<Pair<String, String>> terms = new ArrayList<>();
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());
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<Integer> ids = indexingService.searchIds(Process.class, terms);
Collection<Integer> finalIds = ids.isEmpty() ? NO_HIT : ids;

for (var iterator = indexQueries.entrySet().iterator(); iterator.hasNext();) {
Entry<String, Pair<FilterField, String>> entry = iterator.next();
parameters.put(entry.getKey(), finalIds);
iterator.remove();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,24 +125,41 @@
}

/**
* 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<Integer> searchIds(Class<? extends BaseBean> beanClass, String searchField, String value) {
public Collection<Integer> searchIds(Class<? extends BaseBean> beanClass, List<Pair<String, String>> terms) {
SearchSession searchSession = Search.session(HibernateUtil.getSession());
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());
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<Integer> ids = query.fetchAll().hits();

logger.debug(
"Searching {} IDs with terms {}: {} hits",
beanClass.getSimpleName(),
terms.stream()
.map(t -> t.getLeft() + "=\"" + t.getRight() + "\"")
.collect(Collectors.joining(", ")),
ids.size()
);
return ids;
}

Expand Down
Loading