NHibernate AND'ing dynamic Disjunction

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%' /* @p0 */
     or p2_.Name3 like '%smith%' /* @p1 */
     or o1_.EntityName like '%smith%' /* @p2 */
     or p2_.Name1 like '%w%' /* @p3 */
     or p2_.Name3 like '%w%' /* @p4 */
     or o1_.EntityName like '%w%' /* @p5 */)

WHERE  ((p2_.Name1 like '%smith%' /* @p0 */
     or p2_.Name3 like '%smith%' /* @p1 */
     or o1_.EntityName like '%smith%' /* @p2 */)
     AND(
     p2_.Name1 like '%w%' /* @p3 */
     or p2_.Name3 like '%w%' /* @p4 */
     or o1_.EntityName like '%w%' /* @p5 */))
+3
2

if (!searchTerms.Contains(' '))
{
    Disjunction keywordsCriteria = Restrictions.Disjunction();
    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);
}
else
{
    Conjunction conjunction = Restrictions.Conjunction();
    foreach (var keyword in searchTerms.Split(' '))
    {
        Disjunction disjunction = Restrictions.Disjunction();
        var term = string.Format("%{0}%", keyword);
            disjunction
            .Add(Restrictions.Like("p.FirstName", term))
            .Add(Restrictions.Like("p.LastName", term))
            .Add(Restrictions.Like("o.Name", term));
        conjunction.Add(disjunction);
    }
    query.Add(conjunction);
}
+2

, , , , .

, !

if (searchTerms.Contains(' ')) {
            foreach (var keyword in searchTerms.Split(' ')) {
                var term = string.Format("%{0}%", keyword);
                Disjunction keywordsCriteria = Restrictions.Disjunction(); // <<--
                keywordsCriteria
                    .Add(Restrictions.Like("p.FirstName", term))
                    .Add(Restrictions.Like("p.LastName", term))
                    .Add(Restrictions.Like("o.Name", term));
                query.Add(keywordsCriteria);
            }
        }
        else
        {
            var term = string.Format("%{0}%", searchTerms);
            Disjunction keywordsCriteria = Restrictions.Disjunction(); // <<--
            keywordsCriteria
                .Add(Restrictions.Like("p.FirstName", term))
                .Add(Restrictions.Like("p.LastName", term))
                .Add(Restrictions.Like("o.Name", term));
            query.Add(keywordsCriteria);
        }
+1

Source: https://habr.com/ru/post/1763537/


All Articles