EF Core 2.1 中的全文搜索?
Full Text Search in EF Core 2.1?
我正在考虑使用全文搜索,但不是 100% 清楚如何使用 EF Core 2.1。
似乎 EF Core 2.1 可能已经实现了对全文搜索的部分支持,但我没有找到任何关于如何实际使用它的教程。
我的理解是我必须在我的其中一个专栏中添加全文索引。
所以如果我有这个 table
public class Company {
public string Name {get; set;}
}
public class CompanyConfig : IEntityTypeConfiguration<Company>
{
public void Configure(EntityTypeBuilder<Company> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Name).HasMaxLength(100).IsRequired();
}
}
如何为我的姓名 属性 添加全文索引?
您现在需要使用迁移中的 SQL 功能手动添加它们。
从 EF Core 2.1 开始,尚未构建全文索引的创建。有关详细信息,请参阅此问题跟踪器 https://github.com/aspnet/EntityFrameworkCore/issues/11488。
总结一下;
In EF Core 2.1 we have initial support for for full-text search via the FreeText predicate in LINQ, but this only works with databases that have already been indexed. EF Core and the SQL Server provider don't provide any way to configure the model so that migrations or EnsureCreated can generate the right SQL for defining the indexes.
FreeText 的 C# Linq 查询示例,摘自 https://github.com/aspnet/EntityFrameworkCore/commit/2a6ccad8821f9360ae753bce41d63811185b8912;
上的测试
using (var context = CreateContext())
{
var result = await context
.Employees
.Where(c => EF.Functions.FreeText(c.Title, "Representative"))
.ToListAsync();
Assert.Equal(result.First().EmployeeID, 1u);
Assert.Equal(
@"SELECT [c].[EmployeeID], [c].[City], [c].[Country], [c].[FirstName], [c].[ReportsTo], [c].[Title] FROM [Employees] AS [c] WHERE FREETEXT([c].[Title], N'Representative')",
Sql,
ignoreLineEndingDifferences: true,
ignoreWhiteSpaceDifferences: true);
}
我正在考虑使用全文搜索,但不是 100% 清楚如何使用 EF Core 2.1。
似乎 EF Core 2.1 可能已经实现了对全文搜索的部分支持,但我没有找到任何关于如何实际使用它的教程。
我的理解是我必须在我的其中一个专栏中添加全文索引。
所以如果我有这个 table
public class Company {
public string Name {get; set;}
}
public class CompanyConfig : IEntityTypeConfiguration<Company>
{
public void Configure(EntityTypeBuilder<Company> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Name).HasMaxLength(100).IsRequired();
}
}
如何为我的姓名 属性 添加全文索引?
您现在需要使用迁移中的 SQL 功能手动添加它们。
从 EF Core 2.1 开始,尚未构建全文索引的创建。有关详细信息,请参阅此问题跟踪器 https://github.com/aspnet/EntityFrameworkCore/issues/11488。
总结一下;
In EF Core 2.1 we have initial support for for full-text search via the FreeText predicate in LINQ, but this only works with databases that have already been indexed. EF Core and the SQL Server provider don't provide any way to configure the model so that migrations or EnsureCreated can generate the right SQL for defining the indexes.
FreeText 的 C# Linq 查询示例,摘自 https://github.com/aspnet/EntityFrameworkCore/commit/2a6ccad8821f9360ae753bce41d63811185b8912;
上的测试using (var context = CreateContext())
{
var result = await context
.Employees
.Where(c => EF.Functions.FreeText(c.Title, "Representative"))
.ToListAsync();
Assert.Equal(result.First().EmployeeID, 1u);
Assert.Equal(
@"SELECT [c].[EmployeeID], [c].[City], [c].[Country], [c].[FirstName], [c].[ReportsTo], [c].[Title] FROM [Employees] AS [c] WHERE FREETEXT([c].[Title], N'Representative')",
Sql,
ignoreLineEndingDifferences: true,
ignoreWhiteSpaceDifferences: true);
}