-
Notifications
You must be signed in to change notification settings - Fork 2k
Cosmos full-text search "what's new" docs #5009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
entity-framework/core/providers/cosmos/full-text-search.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
--- | ||
title: Full Text Search - Azure Cosmos DB Provider - EF Core | ||
description: Full text search with the Azure Cosmos DB EF Core Provider | ||
author: maumar | ||
ms.date: 04/19/2025 | ||
uid: core/providers/cosmos/full-text-search | ||
--- | ||
# Full text search | ||
|
||
Azure Cosmos DB now offers support for [full-text search](/azure/cosmos-db/gen-ai/full-text-search). It enables efficient and effective text searches using advanced techniques like stemming, as well as evaluating the relevance of documents to a given search query. It can be used in combination with vector search (i.e. hybrid search) to improve the accuracy of responses in some AI scenarios. | ||
EF Core allows for modeling the database with full-text search enabled properties and using full-text search functions inside queries targeting Azure Cosmos DB. | ||
|
||
## Model configuration | ||
|
||
A property can be configured inside `OnModelCreating` to use full-text search by enabling it for the property and defining a full-text index: | ||
|
||
```c# | ||
public class Blog | ||
{ | ||
... | ||
|
||
public string Contents { get; set; } | ||
} | ||
|
||
public class BloggingContext | ||
{ | ||
... | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Blog>(b => | ||
{ | ||
b.Property(x => x.Contents).EnableFullTextSearch(); | ||
b.HasIndex(x => x.Contents).IsFullTextIndex(); | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
> [!NOTE] | ||
> Configuring the index is not mandatory, but it is recommended as it greatly improves performance of full-text search queries. | ||
|
||
Full-text search operations are language specific, using American English (`en-US`) by default. You can customize the language for individual properties as part of `EnableFullTextSearch` call: | ||
|
||
```c# | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Blog>(b => | ||
{ | ||
b.Property(x => x.Contents).EnableFullTextSearch(); | ||
b.HasIndex(x => x.Contents).IsFullTextIndex(); | ||
b.Property(x => x.ContentsGerman).EnableFullTextSearch("de-DE"); | ||
b.HasIndex(x => x.ContentsGerman).IsFullTextIndex(); | ||
}); | ||
} | ||
``` | ||
|
||
You can also set a default language for the container - unless overridden in the `EnableFullTextSearch` method, all full-text properties inside the container will use that language. | ||
|
||
```c# | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Blog>(b => | ||
{ | ||
b.HasDefaultFullTextLanguage("de-DE"); | ||
b.Property(x => x.ContentsEnglish).EnableFullTextSearch("en-US"); | ||
b.HasIndex(x => x.ContentsEnglish).IsFullTextIndex(); | ||
b.Property(x => x.ContentsGerman).EnableFullTextSearch(); | ||
b.HasIndex(x => x.ContentsGerman).IsFullTextIndex(); | ||
b.Property(x => x.TagsGerman).EnableFullTextSearch(); | ||
b.HasIndex(x => x.TagsGerman).IsFullTextIndex(); | ||
}); | ||
} | ||
``` | ||
|
||
## Querying | ||
|
||
As part of the full-text search feature, Azure Cosmos DB introduced several built-in functions which allow for efficient querying of content inside the full-text search enabled properties. These functions are: [`FullTextContains`](/azure/cosmos-db/nosql/query/fulltextcontains), [`FullTextContainsAll`](/azure/cosmos-db/nosql/query/fulltextcontainsall), [`FullTextContainsAny`](/azure/cosmos-db/nosql/query/fulltextcontainsany), which look for specific keyword or keywords and [`FullTextScore`](/azure/cosmos-db/nosql/query/fulltextscore), which returns [BM25 score](https://en.wikipedia.org/wiki/Okapi_BM25) based on provided keywords. | ||
|
||
> [!NOTE] | ||
> `FullTextScore` can only be used inside `OrderBy` to rank the documents based on the score. | ||
|
||
EF Core exposes these functions as part of `EF.Functions` so they can be used in queries: | ||
|
||
```c# | ||
var cosmosBlogs = await context.Blogs.Where(x => EF.Functions.FullTextContainsAll(x.Contents, "database", "cosmos")).ToListAsync(); | ||
|
||
var keywords = new string[] { "AI", "agent", "breakthrough" }; | ||
var mostInteresting = await context.Blogs.OrderBy(x => EF.Functions.FullTextScore(x.Contents, keywords)).Take(5).ToListAsync(); | ||
``` | ||
|
||
## Hybrid search | ||
|
||
Full-text search can be used with vector search in the same query (i.e. hybrid search), by combining results of `FullTextScore` and `VectorDistance` functions. It can be done using the [`RRF`](/azure/cosmos-db/nosql/query/rrf) (Reciprocal Rank Fusion) function, which EF Core also provides inside `EF.Functions`: | ||
|
||
```c# | ||
public class Blog | ||
{ | ||
... | ||
|
||
public float[] Vector { get; set; } | ||
public string Contents { get; set; } | ||
} | ||
|
||
public class BloggingContext | ||
{ | ||
... | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Blog>(b => | ||
{ | ||
b.Property(x => x.Contents).EnableFullTextSearch(); | ||
b.HasIndex(x => x.Contents).IsFullTextIndex(); | ||
|
||
b.Property(x => x.Vector).IsVectorProperty(DistanceFunction.Cosine, dimensions: 1536); | ||
b.HasIndex(x => x.Vector).IsVectorIndex(VectorIndexType.Flat); | ||
}); | ||
} | ||
} | ||
|
||
float[] myVector = /* generate vector data from text, image, etc. */ | ||
var hybrid = await context.Blogs.OrderBy(x => EF.Functions.Rrf( | ||
EF.Functions.FullTextScore(x.Contents, "database"), | ||
EF.Functions.VectorDistance(x.Vector, myVector))) | ||
.Take(10) | ||
.ToListAsync(); | ||
``` | ||
|
||
> [!TIP] | ||
> You can combine more than two scoring functions inside `Rrf` call, as well as using only `FullTextScore`, or only `VectorDistance`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.