I'm trying to implement searching of users by mulitple attributes. Let's say, my Person has Name and Lastname. I want to type 'Michael Doe' and list all persons with this name and lastname. When I type only 'Michael', I want all Michaels and so on.
I tried to make it possible by using Predicates like:
private static Specification<UserEntity> containsTextInAttributes(List<String> text, List<String> attributes) {
List<String> finalTextArray = text.stream().map(e -> "%"+e+"%").collect(Collectors.toList());
return (root, query, builder) -> builder.or(root.getModel().getDeclaredSingularAttributes().stream()
.filter(a -> attributes.contains(a.getName()))
.map(a -> builder.or(finalTextArray.stream().map(e -> builder.like(root.get(a.getName()), e)).toArray(Predicate[]::new)))
.toArray(Predicate[]::new)
);
}
public static Specification<UserEntity> containsTextInAllAttributes(List<String> text) {
return containsTextInAttributes(text, Arrays.asList("lastname", "firstname", "email", "phoneNumber"));
}
However, when I search 'Michael Doe" I have all Michaels and all with Lastname Doe. How can I solve my problem?
Thank you in advance for help.