Freetext Extension in Entity Framework Code First
I posted before a solution for adding custom SQL functions to Entity Framework Code First as extension methods. This time I am going to show how we can do something similar for the FREETEXT function of SQL Server. Please note that this example will only work if you have the Fulltext Search component installed and your table is indexed.
OK, so we want to have an extension method like this:
[DbFunction("CodeFirstDatabaseSchema", "FREETEXT")]
public static Boolean Freetext(this String column, String value)
{
return column.Contains(value);
}
In order for Entity Framework to recognize it, we need to write our own convention, this is because Entity Framework only recognizes out of the box a number of SQL Server built-in functions. We can write one as this:
public class FreetextConvention : IStoreModelConvention<EdmModel>
{
public static readonly FreetextConvention Instance = new FreetextConvention();
public void Apply(EdmModel item, DbModel model)
{
var valueParameter = FunctionParameter.Create("column", this.GetStorePrimitiveType(model, PrimitiveTypeKind.String), ParameterMode.In);
var formatParameter = FunctionParameter.Create("value", this.GetStorePrimitiveType(model, PrimitiveTypeKind.String), ParameterMode.In);
var returnValue = FunctionParameter.Create("result", this.GetStorePrimitiveType(model, PrimitiveTypeKind.Boolean), ParameterMode.ReturnValue);
var function = this.CreateAndAddFunction(item, "FREETEXT", new[] { valueParameter, formatParameter }, new[] { returnValue });
}
protected EdmFunction CreateAndAddFunction(EdmModel item, String name, IList<FunctionParameter> parameters, IList<FunctionParameter> returnValues)
{
var payload = new EdmFunctionPayload { StoreFunctionName = name, Parameters = parameters, ReturnParameters = returnValues, Schema = this.GetDefaultSchema(item), IsBuiltIn = true };
var function = EdmFunction.Create(name, this.GetDefaultNamespace(item), item.DataSpace, payload, null);
item.AddItem(function);
return (function);
}
protected EdmType GetStorePrimitiveType(DbModel model, PrimitiveTypeKind typeKind)
{
return (model.ProviderManifest.GetStoreType(TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(typeKind))).EdmType);
}
protected String GetDefaultNamespace(EdmModel layerModel)
{
return (layerModel.GlobalItems.OfType<EdmType>().Select(t => t.NamespaceName).Distinct().Single());
}
protected String GetDefaultSchema(EdmModel layerModel)
{
return (layerModel.Container.EntitySets.Select(s => s.Schema).Distinct().SingleOrDefault());
}
}
This registers a FREETEXT function with two string parameters and returning a boolean. All is fine, we add it to the DbContext in OnModelCreating:
modelBuilder.Conventions.Add(FreetextConvention.Instance);
You might have noticed the usage of a Instance static field, this is because, since the FreetextConvention class is stateless, there’s no point in creating many of them, we can just use the same instance.
Now, if we issue a LINQ query as:
var customers = ctx.Customers.Where(x => x.Name.Freetext("ricardo")).ToList();
It will fail miserably, complaining about this SQL fragment:
WHERE ((FREETEXT(name, N'ricardo') = 1)
The “= 1” part is here because the function is prototyped as boolean, which maps to SQL Server’s BIT data type, and the value for true is 1. Apparently, SQL Server does not support comparisons of some functions with 1; but if we run it as:
WHERE ((FREETEXT(name, N'ricardo'))
without the explicit comparison, it works perfectly. So, all we have to do is get rid of “= 1”. Fortunately, Entity Framework, as of version 6, offers some very nice extensibility points. There are at least two ways by which we can achieve this:
- By intercepting the command tree;
- By intercepting the raw SQL.
Here we will use option #2 and leave command trees for another post.
We need to identity something with a format of “FREETEXT(something) = 1”. We can do it using a regular expression, and the interception of the SQL command can be achieved by implementing IDbCommandInterceptor (no reference documentation yet, but I have reported it and it will soon be fixed, hopefully) and registering one such instance in the DbInterception (same) static class. An IDbCommandInterceptor implementation might look like this:
public class FreetextInterceptor : IDbCommandInterceptor
{
public static readonly FreetextInterceptor Instance = new FreetextInterceptor();
private static readonly Regex FreetextRegex = new Regex(@"FREETEXT\(([^)]+\))\) = 1");
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<Int32> interceptionContext)
{
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<Int32> interceptionContext)
{
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
var matches = FreetextRegex.Matches(command.CommandText);
if (matches.Count > 0)
{
command.CommandText = FreetextRegex.Replace(command.CommandText, "FREETEXT($1)");
}
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<Object> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<Object> interceptionContext)
{
}
}
You can see that the only method we’re interested in is ReaderExecuting (again, no documentation available), with is the one that will be called just before a SQL SELECT query is sent to the database. In here we analyze the CommandText property of the DbCommand and get rid of the “= 1” clause, using a regular expression. Finally, we need to register the interceptor before we issue the query, maybe in the static constructor of our DbContext:
DbInterception.Add(FreetextInterceptor.Instance);
And now we can finally execute our query:
var customers = ctx.Customers.Where(x => x.Name.Freetext("ricardo")).ToList();
And that’s it. Don’t forget that in order for this to work, you need to enable Full Text Search.