Skip to content

fissoft/Fissoft.EntityFramework.Fts

Repository files navigation

Fissoft.EntityFramework.Fts

Full Text Search for Microsoft SQL Server with Entity Framework

NuGet Install

install from nuget Build status release CodeFactor

PM> Install-Package Fissoft.EntityFramework.Fts

Demo

Execute init code on start or static ctor.

    DbInterceptors.Init();

When search you can use the code following.

    var text = FullTextSearchModelUtil.Contains("code");
    db.Tables.Where(c=>c.Fullname.Contains(text));
    var text = FullTextSearchModelUtil.FreeText("code ef");
    db.Tables.Where(c=>c.Fullname.Contains(text));
    var text = FullTextSearchModelUtil.ContainsAll("code ef");
    db.Tables.Where(c=>c.Name.Contains(text)); //c.Name could be any string property of model
    var text = FullTextSearchModelUtil.FreeTextAll("code ef");
    db.Tables.Where(c=>c.Name.Contains(text)); //c.Name could be any string property of model
    var text = FullTextSearchModelUtil.Contains("a b",true);
    var query = db.TestModel.Where(c => c.Name.Contains(text)).ToList(); 
    // Should return results that contain BOTH words. For the second param = false, should return records with either of the words

Multi field query

var query = db.TestModel
                    .Where(c => (c.Name+c.Text).Contains(text))
                    .ToList();

will generate the sql

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Text] AS [Text], 
    [Extent1].[Name] AS [Name]
    FROM [dbo].[TestModels] AS [Extent1]
    WHERE CONTAINS(([Extent1].[Name] , [Extent1].[Text]),@p__linq__0);

Reference:

http://www.entityframework.info/Home/FullTextSearch

About .NET Core

In .NET core 2.1 you can use offical freetext method to search.

Install the package Microsoft.EntityFrameworkCore.SqlServer.

   using Microsoft.EntityFrameworkCore;
   var result = db.TestModel.Where(c => EF.Functions.FreeText(c.Text, "search"));

EF.Docs# Use FreeText( and soon Contains )functions