Match for any text into search box - spring

I use this Search specification to try to search text by title. It's working fine if I type the exact search String:
public Page<ClassCategoriesFullDTO> findClasses(ClassCategoriesSearchParams params, Pageable pageable) {
Specification<Product> spec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (params.getTitle() != null) {
predicates.add(cb.equal(root.get("title"), params.getTitle()));
}
if (params.getType() != null) {
predicates.add(cb.equal(root.get("type"), params.getType()));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
return classCategoriesService.findAll(spec, pageable).map(classCategoriesMapper::toFullDTO);
}
How I can modify the code to search into DB with every text I type into DB table?

You can use the criteria builder's (cb) like a method to search for "an" to get "surname"
static Specification<ClassCategoriesFullDTO> titleContains(String title) {
return (root, cq, cb) -> cb.like(root.get("title"), "%" + title + "%");
}
If you want to ignore the case then use:
static Specification<ClassCategoriesFullDTO> titleContains(String title) {
return (root, cq, cb) -> cb.like(cb.lower(root.get("title")), "%" + title.toLowerCase() + "%");
}

Related

How to append specification based on condition in spring data jpa

I am creating a spring boot project and working on spring data jpa and currently I am using custom query to fetch data from db based on users selection and this is my page where user can select option based on their condition https://i.imgur.com/coO3BCJ.png
So, I googled it and found we can use specification but as I am very new to specification, so, I used specification and I want that based on users choice, it should keep adding specification, so, this is my conditional specification...
Specification<UserDetails> specification =
Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage()));
if(!customSearch.getManglik().isBlank()) {
specification.and(UserDetailsSpecification.isManglik
(customSearch.getManglik()));
}
if(!customSearch.getMaritalStatus().isBlank()) {
specification.and(UserDetailsSpecification
.hasMaritalStatus(customSearch.getMaritalStatus()));
}
if(!customSearch.getReligion().isBlank()) {
specification.and(UserDetailsSpecification.hasReligion
(customSearch.getReligion()));
}
if(!customSearch.getCaste().isBlank()) {
specification.and(UserDetailsSpecification.hasCaste
(customSearch.getCaste()));
}
if(!customSearch.getLocation().isBlank()) {
specification.and(UserDetailsSpecification.hasLocation
(customSearch.getLocation()));
}
listOfCustomSearch=userDetailsRepository
.findAll(specification, pageable);
List<UserDetails> listOfAllSearchedUsers = listOfCustomSearch.getContent();
but it is not appending the specification and just filtering the data based on only
Specification<UserDetails> specification = Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage()));
so, based on users selection, so, my final query should be something like this(If user has selected all fields):
Specification<UserDetails> specification = Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage())
.and(UserDetailsSpecification.isManglik(customSearch.getManglik()) .and(UserDetailsSpecification.hasMaritalStatus(customSearch.getMaritalStatus())) .and(UserDetailsSpecification.hasReligion(customSearch.getReligion()))
.and(UserDetailsSpecification.hasCaste(customSearch.getCaste()))
.and(UserDetailsSpecification.hasLocation(customSearch.getLocation()))))
But suppose if user has selected only let suppose 3 or 4 fields, so, my final specification should be something like below:(This specification should be completely depends upon user selection)
Specification<UserDetails> specification = Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage())
.and(UserDetailsSpecification.isManglik(customSearch.getManglik())
.and(UserDetailsSpecification.hasLocation(customSearch.getLocation()))))
Currently it is not appending specification based on users selection, so, please help me in adding specification based on users selections
Finally, I found the solution and we can create customized specification like this below:
public static Specification<UserDetails> getSpecs(String gender, int fromAge, int toAge, String manglikStatus, String maritalStatus, String religion, String caste, String location){
Specification<UserDetails> spec = null;
Specification<UserDetails> temp = null;
if(!gender.isBlank() && !gender.isEmpty() && gender!=null && !gender.contains("$")) {
spec = getSpecsForGenderDetails(gender);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(fromAge!=0 || toAge!=0) {
spec = isAgeBetween(fromAge, toAge);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!manglikStatus.isBlank() && !manglikStatus.isEmpty() && manglikStatus!=null && !manglikStatus.contains("$")) {
spec = isManglik(manglikStatus);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!maritalStatus.isBlank() && !maritalStatus.isEmpty() && maritalStatus!=null && !maritalStatus.contains("$")) {
spec = hasMaritalStatus(maritalStatus);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!religion.isBlank() && !religion.isEmpty() && religion!=null && !religion.contains("$")) {
spec = hasReligion(religion);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!caste.isBlank() && !caste.isEmpty() && caste!=null && !caste.equalsIgnoreCase("select") && !caste.contains("$")) {
spec = hasCaste(caste);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!location.isBlank() && !location.isEmpty() && location!=null && !location.contains("$")) {
spec = hasLocation(location);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
return temp;
}
And based on that we can define our method like this:
private static Specification<UserDetails> getSpecsForGenderDetails(String gender) {
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.equal(root.get("gender"),gender);
});
}
private static Specification<UserDetails> isAgeBetween(int fromAge, int toAge){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.between(root.get("age"), fromAge, toAge);
});
}
private static Specification<UserDetails> isManglik(String manglikStatus){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("manglikStatus")),"%" +manglikStatus.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasMaritalStatus(String maritalStatus){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("maritalStatus")),"%" +maritalStatus.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasReligion(String religion){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("religion")),"%" +religion.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasCaste(String caste){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("caste")),"%" +caste.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasLocation(String presentState){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("presentState")),"%" +presentState.toLowerCase() +"%");
});
}

Spring Boot & JPA: CriterialBuilder check if String contains Expression<String>

1.) I want to get an Expression using an Expression and a String Pattern.
It should become true if the Expression is included in the String Pattern not the other way.
The string pattern can look like "MFD" or "MF" or any other combination of the three letters M,F,D. The Expression is either M, F or D.
So far I have the following code, but i think there is a better solution.
public class AthleteSpecification implements Specification<Athlete> {
private String gender;
public AthleteSpecification(String gender) {
super();
this.gender = gender;
}
#Override
public Predicate toPredicate(Root<Athlete> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate p = cb.disjunction();
if (this.gender == null) {
return cb.conjunction();
} else {
Expression<Boolean> isMaleOrFemaleorDivers;
if (this.gender != null) {
// This is a working solution but not nice
Expression<Boolean> isMale = cb.equal(root.get("gender"), gender.contains("M") ? "M" : "");
Expression<Boolean> isMaleOrFemale = cb.or(isMale,
cb.equal(root.get("gender"), gender.contains("F") ? "F" : ""));
isMaleOrFemaleorDivers = cb.or(isMaleOrFemale,
cb.equal(root.get("gender"), gender.contains("D") ? "D" : ""));
// This is not a solution because i think it only checks if the String is in the Expression<String> not the other way
// Expression<Integer> test = cb.locate(root.get("gender"), gender);
// isMaleOrFemaleorDivers = cb.greaterThan(test, 0);
} else {
isMaleOrFemaleorDivers = cb.conjunction();
}
p.getExpressions().add(cb.and(isNameMatching, isMaleOrFemaleorDivers));
return p;
}
}
}

Spring JPA Specifications Not In Query

Here the IN criteria query using Spring JPA Specification is working fine. but I don't know how to use "NOT IN"..
So How can i use the NOT IN criteria query using Spring JPA Specifications.
SearchSpecification<User> spec = new CommonSpecification<Case>(new SearchCriteria("client.id", Operator.NOT_IN, clientIdList));
#Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate predicate = null;
switch (searchCriteria.getOperator()) {
case IN:
if (searchCriteria.getValue() instanceof List<?>) {
predicate = getFieldPath(searchCriteria.getKey(), root)
.in(((List<?>) searchCriteria.getValue()).toArray());
}
break;
case NOT_IN:
//What to do???
break;
default:
break;
}
return predicate;
}
private Path<Object> getFieldPath(String key, Root<T> root) {
Path<Object> fieldPath = null;
if (key.contains(".")) {
String[] fields = key.split("\\.");
for (String field : fields) {
if (fieldPath == null) {
fieldPath = root.get(field);
} else {
fieldPath = fieldPath.get(field);
}
}
} else {
fieldPath = root.get(key);
}
return fieldPath;
}
Use the Predicate.not() method; the code is the same as in the IN case, just add .not():
case NOT_IN:
if (searchCriteria.getValue() instanceof List<?>) {
predicate = getFieldPath(searchCriteria.getKey(), root)
.in(((List<?>) searchCriteria.getValue()).toArray())
.not();
}
break;
This functionality also works fine..
case NOT_IN:
if (searchCriteria.getValue() instanceof List<?>) {
predicate = criteriaBuilder.not(getFieldPath(searchCriteria.getKey(), root).in(((List<?>) searchCriteria.getValue()).toArray()));
}
break;

Parse: findInBackground only returns results for currentUser

I've searched and searched with no luck.
FYI: I'm a newbie...I'm trying to query a class for non-current user data. I've verified that user_objectID and userID are correct and that Parse has these files, but the list shows up as 0. When I switch userID to my ID then it works.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Workouts");
query.whereEqualTo("user_objectID", userID);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
Log.v(TAG, userID + ": " + list.size());
for (ParseObject each : list) {
Log.v(TAG, each.getDouble("distance") + "");
}
} else {
Log.v(TAG, e.getLocalizedMessage());
}
}
});
What am I doing wrong? Is it a setting? Am I not using the right query paramters?
Thanks so much for any insight.
Tyler

Sitecore ContentSearch LINQ Unsupported expression node type: Parameter

I get the following exception message when trying to run the ToList method for the Sitecore ContentSearch LINQ query in the following code block:
Unsupported expression node type: Parameter. This could be due to ordering of
expression statements. For example filtering after a select expression like this :
queryable.Select(i => new { i.Id, i.Title }).Where(i => d.Title > "A"
public virtual List<SearchResultItem> RunQuery(SearchParam param, bool showAllVersions, bool firstLoad)
{
Assert.ArgumentNotNull(Index, "Sitecore.SharedSource.Search");
var resultCollection = new List<SearchResultItem>();
try
{
using (var context = this.Index.CreateSearchContext())
{
var result = context.GetQueryable<SearchResultItem>()
.Where(x => HasFullText(x, param.FullTextQuery) &&
HasLanguage(x, param.Language) &&
HasRelation(x, param.RelatedIds) &&
HasTemplate(x, param.TemplateIds) &&
HasLocation(x, param.LocationIds)
);
resultCollection = result.ToList();
}
}
catch (Exception exception)
{
Log.Error(exception.StackTrace, this);
throw;
}
return resultCollection;
}
I can't figure out what causes this issue and I can't seem to reproduce the issue with standard .NET LINQ queries, in a standard Console Application (source code at the end).
Here is the source code for the HasLanguage, HasRelation, HasTemplate and HasLocation functions. I expect it has something to do with those because when I remove them and replace them with their implementation (where possible) I get no errors. However, when left inside the query they are not even accessed (tried debugging):
protected bool HasRefinements(SearchResultItem pseudoResult, SafeDictionary<string> refinements)
{
if (refinements.Count <= 0) return false;
foreach (var refinement in refinements)
{
var fieldName = refinement.Key.ToLowerInvariant();
var fieldValue = refinement.Value;
if (pseudoResult.GetField(fieldName).Value.Equals(IdHelper.ProcessGUIDs(fieldValue)))
{
return true;
}
}
return false;
}
protected bool HasLanguage(SearchResultItem pseudoResult, string language)
{
if (String.IsNullOrEmpty(language)) return false;
return pseudoResult.GetField(BuiltinFields.Language).Equals(language.ToLowerInvariant());
}
protected bool HasFullText(SearchResultItem pseudoResult, string searchText)
{
if (String.IsNullOrEmpty(searchText)) return false;
return pseudoResult.Content.Contains(searchText);
}
protected bool HasId(SearchResultItem pseudoResult, string fieldName, string filter)
{
if (String.IsNullOrEmpty(fieldName) || String.IsNullOrEmpty(filter)) return false;
var values = IdHelper.ParseId(filter);
foreach (var value in values.Where(ID.IsID))
{
if (pseudoResult.GetField(fieldName).Value.Equals(IdHelper.ProcessGUIDs(value)))
{
return true;
}
}
return false;
}
protected bool HasTemplate(SearchResultItem pseudoResult, string templateIds)
{
if (String.IsNullOrEmpty(templateIds)) return false;
templateIds = IdHelper.NormalizeGuid(templateIds);
return pseudoResult.TemplateId.ToString().Equals(templateIds);
}
protected bool HasLocation(SearchResultItem pseudoResult, string locationIds)
{
return HasId(pseudoResult, BuiltinFields.Path, locationIds);
}
protected bool HasRelation(SearchResultItem pseudoResult, string ids)
{
return HasId(pseudoResult, BuiltinFields.Links, ids);
}
And here is the source code for my test application using regular LINQ queries:
static void Main(string[] args)
{
Program p = new Program();
p.Process();
}
public void Process()
{
List<Boolean> flags = new List<Boolean>();
flags.Add(true);
flags.Add(false);
flags.Add(false);
flags.Add(true);
flags.Add(false);
bool b = true;
try
{
List<Boolean> trueFlags = flags
.Where<Boolean>(x => IsTrue(x, b))
.ToList();
Console.WriteLine(trueFlags.ToString());
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
public bool IsTrue(bool x, bool b)
{
return x ^ b;
}
I can't seem to find anything on this exception message on the internet.
Sitecore LINQ isn't really like normal LINQ to objects in .NET. Like LINQ to SQL isnt like normal LINQ.
What actually happens is that Sitecore parses the expression tree and "converts/translates" your conditions to a Search Query. By default this is to a Lucene query expression - but using a SearchProvider it could also translate to a SOLR expression or Coveo expression.
In the same way LINQ to SQL translates to a SQL expression.
You can see that the LINQ is actually an IQueryable and not IEnumerable.
When working on the IQuerable Sitecore must know how to translate it to the search expression. Sitecore doesn't know how to translate your properties and methods in the LINQ expression and that is why you get the error.
You should change your expression to only hold something that can be translated or create a predicate. You should look into the PredicateBuilder

Resources