Skip to content

nandor23/EFCore.ParadeDB.PgSearch

Repository files navigation

Logo
EFCore.ParadeDB.PgSearch

Build License

EFCore.ParadeDB.PgSearch adds ParadeDB's pg_search extension support to Npgsql.EntityFrameworkCore.PostgreSQL. It exposes ParadeDB search functions through EF.Functions API for LINQ-based full-text search queries. BM25 index creation must be done via raw SQL. See the EF Core documentation for more details on adding raw SQL to migrations.

Configuration

Install the Npgsql.EntityFrameworkCore.PostgreSQL NuGet package and configure your DbContext by calling UsePgSearch() on the NpgsqlDbContextOptionsBuilder to enable pg_search function mappings.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContextPool<AppDbContext>(opt =>
{
    opt.UseNpgsql(
        builder.Configuration.GetConnectionString("AppDatabase"),
        o => o.UsePgSearch()
    );
});

Register the pg_search extension in the DbContext.

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.HasPostgresExtension("pg_search");
    }
}

A complete example is also available in the samples directory demonstrating PgSearch configuration and BM25 index creation.

Function Mappings

The following ParadeDB operations are available through the EF.Functions API:

ParadeDB Operation LINQ Methods
Match MatchDisjunction(), MatchConjunction()
Phrase Phrase()
Term Term()
BM25 scoring Score()
Highlighting Snippet()
Proximity Proximity(), ProximityRegex()

Usage Example

var products = await dbContext
    .Products.Where(p =>
        EF.Functions.MatchDisjunction(
            p.Description,
            "with shoes and",
            Fuzzy.With(1),
            Boost.With(2.3f)
        )
    )
    .Select(p => new
    {
        p.Id,
        p.Description,
        Score = EF.Functions.Score(p.Id),
    })
    .ToListAsync();

Translates to:

SELECT p.id AS "Id", p.description AS "Description", COALESCE(paradedb.score(p.id), 0) AS "Score"
FROM products AS p
WHERE p.description ||| 'with shoes and'::fuzzy(1)::boost(2.3)

About

Entity Framework Core PostgreSQL extension for ParadeDB pg_search full-text search

Resources

License

Stars

Watchers

Forks

Languages