I searched the Internet for several hours to try and get an answer, but to no avail.
What I'm trying to do is AND AND 'N' the number of Disjunctions to refine searches based on a query with a space.
I have “Unity,” which can be any combination of one person or organization, or several people / organizations, or any combination above.
So, if I'm looking for "Smith", I want to match any company or people with the name "Smith", easily.
However, if someone had to type “Smith w” and want matches on different combos (OR on the Face and Organization).
So, I get hit only for Unity (a combination of people and / or organizations) that has BOTH or a company with a blacksmith And any organization
ICriteria query = Session.CreateCriteria(typeof(Unity), "u")
.CreateCriteria("Organisations", "o", JoinType.LeftOuterJoin)
.CreateCriteria("u.People", "p", JoinType.LeftOuterJoin)
.SetResultTransformer(new DistinctRootEntityResultTransformer())
.SetMaxResults(10);
Disjunction keywordsCriteria = Restrictions.Disjunction();
if (searchTerms.Contains(' ')) {
foreach (var keyword in searchTerms.Split(' ')) {
var term = string.Format("%{0}%", keyword);
keywordsCriteria
.Add(Restrictions.Like("p.FirstName", term))
.Add(Restrictions.Like("p.LastName", term))
.Add(Restrictions.Like("o.Name", term));
}
}
else
{
var term = string.Format("%{0}%", searchTerms);
keywordsCriteria
.Add(Restrictions.Like("p.FirstName", term))
.Add(Restrictions.Like("p.LastName", term))
.Add(Restrictions.Like("o.Name", term));
}
query.Add(keywordsCriteria);
return query.List<Unity>();
The result is
where(p2_.Name1 like '%smith%'
or p2_.Name3 like '%smith%'
or o1_.EntityName like '%smith%'
or p2_.Name1 like '%w%'
or p2_.Name3 like '%w%'
or o1_.EntityName like '%w%' )
WHERE ((p2_.Name1 like '%smith%'
or p2_.Name3 like '%smith%'
or o1_.EntityName like '%smith%' )
AND(
p2_.Name1 like '%w%'
or p2_.Name3 like '%w%'
or o1_.EntityName like '%w%' ))