Dmitrij Polyanin
Dmitrij Polyanin

Reputation: 505

How to make full text search with Linq2db?

I have .Net Core 2.2 project with Linq2db provider for MySql.

How can I make search using full text index with linq2db?

Is it possible to make for any DB Engine, not DB specific code?

Upvotes: 1

Views: 1300

Answers (2)

Mnemo
Mnemo

Reputation: 123

It can be done like this:

using (var linqdb = new DatabaseModel())
{
    string searchFT = "wordToSearch*";
    List<TableClass> items = linqdb.GetTable<TableClass>().Where(wo => Sql.Ext.MySql().Match(MySqlExtensions.MatchModifier.Boolean, searchFT, wo.FistName, wo.LastName)).ToList();
}

Here is a link to the documentation: https://linq2db.github.io/api/LinqToDB.DataProvider.MySql.MySqlExtensions.html

An example can be found here: https://github.com/linq2db/linq2db/blob/master/Tests/Linq/Linq/FullTextTests.MySql.cs

Upvotes: 1

Michael Ceber
Michael Ceber

Reputation: 2452

As long as the database supports a text field which can support large amounts of text then you simply add an index to this field by doing somthing like:

class MyContext : DbContext { public DbSet Blogs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .HasIndex(b => b.Url);
}

}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

In EF core if you use things like StartsWith, Contains or EF.Functions.Like in your Where functions then I believe this gets translated to a Like in SQL. So in EF core this would use your full-text search for fields in such databases.

I think it should be the same for linq2db...

So yep this would be generic for any database.

However, as I once found out with SQLite I could not add an index to a such a text field with lots of data as the database does not support it... (from memory it was limited to around 1000 bytes roughly).

Upvotes: 0

Related Questions