Using a Registered Function in Nhibernate ICriteria

253 Views Asked by At

I have som trouble using fulltext search in NHibernate ICriteria

I have registered freetext and contains as below:

public class CMSSQLDialect : MsSql2008Dialect   {
    public CMSSQLDialect()
    {
        RegisterFunction("freetext", new StandardSQLFunction("freetext", null));
        RegisterFunction("contains", new StandardSQLFunction("contains", null));
    }
}

Now i want to use the functions in an ICriteria like:

var store = sessionFactory.CreateCriteria(typeof(Comito.CMS.Domain.Entity.Document.Document));
var stringSearchProjection = Projections.SqlFunction("freetext", NHibernateUtil.StringClob, Projections.Property("BodyText"));

Trying to add the projection to the search:

store.Add(Restrictions.Eq(stringSearchProjection , '*mysearch*'));

(of course) returns

SELECT  this_.BodyText as y0_ FROM TableName WHERE  freetext(this_.BodyText) = 'dav' 

But how do I then implement the freetext in ICriteria

1

There are 1 best solutions below

0
Radim Köhler On

In this case, we do not need to register SQL function for projections. We can just use it in the WHERE clause like that:

store.Add(
    Expression.Sql(
        "CONTAINS(BodyText, ?)",
        "*mysearch*",
        NHibernateUtil.String
    )
);

The Expression is NHibernate.Criterion.Expression