次の方法で共有


フルテキスト検索

Azure Cosmos DB では、 フルテキスト検索のサポートが提供されるようになりました。 ステミングなどの高度な手法を使用して効率的で効果的なテキスト検索を可能にし、特定の検索クエリに対するドキュメントの関連性を評価できます。 一部の AI シナリオで応答の精度を向上させるために、ベクター検索 (ハイブリッド検索) と組み合わせて使用できます。 EF Core では、フルテキスト検索が有効なプロパティを使用してデータベースをモデル化し、Azure Cosmos DB を対象とするクエリ内でフルテキスト検索機能を使用できます。

モデルの構成

プロパティを有効にし、フルテキスト インデックスを定義することで、フルテキスト検索を使用するように OnModelCreating 内でプロパティを構成できます。

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();
        });
    }
}

インデックスの構成は必須ではありませんが、フルテキスト検索クエリのパフォーマンスが大幅に向上します。

フルテキスト検索操作は言語固有であり、既定ではアメリカ英語 (en-US) を使用します。 EnableFullTextSearch呼び出しの一部として、個々のプロパティの言語をカスタマイズできます。

    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();
        });
    }

コンテナーの既定の言語を設定することもできます。 EnableFullTextSearch メソッドでオーバーライドされない限り、コンテナー内のすべてのフルテキスト プロパティでその言語が使用されます。

    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();
        });
    }

クエリ実行

フルテキスト検索機能の一環として、Azure Cosmos DB では、フルテキスト検索が有効なプロパティ内のコンテンツの効率的なクエリを可能にする、いくつかの組み込み関数が導入されました。 これらの関数は次のとおりです。 FullTextContainsFullTextContainsAllFullTextContainsAny。特定のキーワードまたはキーワードを検索し、 FullTextScore、指定されたキーワードに基づいて BM25 スコア を返します。

FullTextScore は、スコアに基づいてドキュメントをランク付けするために OrderBy 内でのみ使用できます。

EF Core は、クエリで使用できるように、 EF.Functions の一部としてこれらの関数を公開します。

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();

フルテキスト検索は、 FullTextScore 関数と VectorDistance 関数の結果を組み合わせることで、同じクエリ内のベクター検索 (ハイブリッド検索) で使用できます。 RRF (逆ランク Fusion) 関数を使用して実行できます。この関数は、EF Core でもEF.Functions内で提供されます。

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();

ヒント

Rrf呼び出し内で複数のスコアリング関数を組み合わせることができ、FullTextScoreのみを使用することも、VectorDistanceのみを使用することもできます。