Skip to content

Commit 02cb3b7

Browse files
committed
Document SQL Server full-text catalog/index creation (#5268)
See dotnet/efcore#11488
1 parent 3e87f84 commit 02cb3b7

2 files changed

Lines changed: 101 additions & 6 deletions

File tree

entity-framework/core/providers/sql-server/full-text-search.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,82 @@ EF Core's SQL Server provider supports both full-text search *predicates* (for f
1313

1414
## Setting up full-text search
1515

16-
Before using full-text search, you must:
16+
Before using full-text search, you must create a [full-text catalog](/sql/t-sql/statements/create-fulltext-catalog-transact-sql) on your database, and a [full-text index](/sql/t-sql/statements/create-fulltext-index-transact-sql) on the columns you want to search.
1717

18-
1. **Create a full-text catalog** on your database
19-
2. **Create a full-text index** on the columns you want to search
18+
### [EF Core 11+](#tab/ef-core-11)
2019

21-
This setup is done at the SQL Server level and is outside the scope of EF Core. For more information, see the [SQL Server full-text search documentation](/sql/relational-databases/search/get-started-with-full-text-search).
20+
> [!NOTE]
21+
> Full-text catalog and index management in migrations was introduced in EF Core 11.
22+
23+
You can configure full-text catalogs and indexes directly in your EF model. When you add a [migration](xref:core/managing-schemas/migrations/index), EF will generate the appropriate SQL to create (or alter) the catalog and index for you.
24+
25+
First, define a full-text catalog on the model, then configure a full-text index on your entity type:
26+
27+
```csharp
28+
protected override void OnModelCreating(ModelBuilder modelBuilder)
29+
{
30+
modelBuilder.HasFullTextCatalog("ftCatalog");
31+
32+
modelBuilder.Entity<Article>()
33+
.HasFullTextIndex(a => a.Contents)
34+
.HasKeyIndex("PK_Articles")
35+
.OnCatalog("ftCatalog");
36+
}
37+
```
38+
39+
The `HasKeyIndex()` method specifies the unique, non-nullable, single-column index used as the full-text key for the table (typically the primary key index). `OnCatalog()` assigns the full-text index to a specific catalog.
40+
41+
You can also configure multiple columns and additional options such as per-column languages and change tracking:
42+
43+
```csharp
44+
modelBuilder.Entity<Article>()
45+
.HasFullTextIndex(a => new { a.Title, a.Contents })
46+
.HasKeyIndex("PK_Articles")
47+
.OnCatalog("ftCatalog")
48+
.WithChangeTracking(FullTextChangeTracking.Manual)
49+
.HasLanguage("Title", "English")
50+
.HasLanguage("Contents", "French");
51+
```
52+
53+
The full-text catalog can also be configured as the default catalog, and with accent sensitivity:
54+
55+
```csharp
56+
modelBuilder.HasFullTextCatalog("ftCatalog")
57+
.IsDefault()
58+
.IsAccentSensitive(false);
59+
```
60+
61+
### [Older versions](#tab/older-versions)
62+
63+
On older versions of EF Core, you can set up full-text search by adding raw SQL to a migration. Add an empty migration and then edit it to include the full-text catalog and index creation SQL:
64+
65+
```csharp
66+
protected override void Up(MigrationBuilder migrationBuilder)
67+
{
68+
migrationBuilder.Sql(
69+
sql: "CREATE FULLTEXT CATALOG ftCatalog AS DEFAULT;",
70+
suppressTransaction: true);
71+
72+
migrationBuilder.Sql(
73+
sql: "CREATE FULLTEXT INDEX ON Articles(Contents) KEY INDEX PK_Articles;",
74+
suppressTransaction: true);
75+
}
76+
77+
protected override void Down(MigrationBuilder migrationBuilder)
78+
{
79+
migrationBuilder.Sql(
80+
sql: "DROP FULLTEXT INDEX ON Articles;",
81+
suppressTransaction: true);
82+
83+
migrationBuilder.Sql(
84+
sql: "DROP FULLTEXT CATALOG ftCatalog;",
85+
suppressTransaction: true);
86+
}
87+
```
88+
89+
---
90+
91+
For more information, see the [SQL Server full-text search documentation](/sql/relational-databases/search/get-started-with-full-text-search).
2292

2393
## Full-text predicates
2494

entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,34 @@ This translates to the SQL Server [`VECTOR_SEARCH()`](/sql/t-sql/functions/vecto
260260

261261
For more information, see the [full documentation on vector search](xref:core/providers/sql-server/vector-search).
262262

263-
<a name="sqlserver-full-text-tvf"></a>
263+
<a name="sqlserver-full-text"></a>
264264

265-
### Full-text search table-valued functions
265+
### Full-text search improvements
266+
267+
#### Full-text search catalog and index creation
268+
269+
SQL Server's [full-text search](/sql/relational-databases/search/full-text-search) requires a full-text catalog and index to be set up on your database before you can use it. EF 11 now allows configuring full-text catalogs and indexes in your model, so that [EF migrations](xref:core/managing-schemas/migrations/index) can automatically create and manage them for you:
270+
271+
```csharp
272+
// In your OnModelCreating:
273+
modelBuilder.HasFullTextCatalog("ftCatalog");
274+
275+
modelBuilder.Entity<Blog>()
276+
.HasFullTextIndex(b => b.FullName)
277+
.HasKeyIndex("PK_Blogs")
278+
.OnCatalog("ftCatalog");
279+
```
280+
281+
This generates the following SQL in a migration:
282+
283+
```sql
284+
CREATE FULLTEXT CATALOG [ftCatalog];
285+
CREATE FULLTEXT INDEX ON [Blogs]([FullName]) KEY INDEX [PK_Blogs] ON [ftCatalog];
286+
```
287+
288+
Previously, full-text catalog and index creation had to be managed manually by adding SQL to migrations. For full details on setting up full-text catalogs and indexes, see the [full-text search documentation](xref:core/providers/sql-server/full-text-search#setting-up-full-text-search).
289+
290+
#### Full-text search table-valued functions
266291

267292
EF Core has long provided support for SQL Server's full-text search predicates `FREETEXT()` and `CONTAINS()`, via `EF.Functions.FreeText()` and `EF.Functions.Contains()`. These predicates can be used in LINQ `Where()` clauses to filter results based on search criteria.
268293

0 commit comments

Comments
 (0)