So let's go back to our Oracle full text search. i found the performance is not so bad, you can read in the previous post what to in database level.
I usually build Predicates for Specifications. whit this solution it is very easy to extend a query if you have many criteria.
We saw that for the full text search we used the CONTAINS Oracle function in the SELECT statement. We can call Oracle predefined functions by the CriteriaBuilder.function() method. Check the solution here:
public static Specification<TDocuLanguageFO> textContains(final String text) {
return new Specification<TDocuLanguageFO>() {
@Override
public Predicate toPredicate(Root<TDocuLanguageFO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Expression<String> x = root.get(TDocuLanguageFO_.contentTxtCb);
Expression<BigDecimal> function = cb.function("CONTAINS",
BigDecimal.class,
x,
cb.literal(text));
return cb.greaterThan(function, BigDecimal.ZERO);
}
};
}
In this example I want to highlight also the literal() method of the Criteriabuilder. We have the text parameter for the search, with this literal() function we can convert it to Expression<String> from String.
And we can put the condition to the WHERE clause like this:
Specifications<TDocuLanguageFO> spec = Specifications.where(textContains(KEYWORD));
List<TDocuLanguageFO> result = dao.findAll(spec);
I usually build Predicates for Specifications. whit this solution it is very easy to extend a query if you have many criteria.
We saw that for the full text search we used the CONTAINS Oracle function in the SELECT statement. We can call Oracle predefined functions by the CriteriaBuilder.function() method. Check the solution here:
public static Specification<TDocuLanguageFO> textContains(final String text) {
return new Specification<TDocuLanguageFO>() {
@Override
public Predicate toPredicate(Root<TDocuLanguageFO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Expression<String> x = root.get(TDocuLanguageFO_.contentTxtCb);
Expression<BigDecimal> function = cb.function("CONTAINS",
BigDecimal.class,
x,
cb.literal(text));
return cb.greaterThan(function, BigDecimal.ZERO);
}
};
}
In this example I want to highlight also the literal() method of the Criteriabuilder. We have the text parameter for the search, with this literal() function we can convert it to Expression<String> from String.
And we can put the condition to the WHERE clause like this:
Specifications<TDocuLanguageFO> spec = Specifications.where(textContains(KEYWORD));
List<TDocuLanguageFO> result = dao.findAll(spec);

