Search code examples
postgresqlentity-framework-corenpgsqltsvector

EF Core npgsql order by Rank on full text search


Trying to work out the EF Core / npgsql combo for a ranking search order...

var models = await _dbContext.ThreadDocuments
    .Where(td => td.SearchVector.Matches(filters.SearchTerm))
    .OrderByDescending(td => td.SearchVector.Rank(new NpgsqlTsQuery(filters.SearchTerm)))
    .Select(td => td.Document)
    .Take(100)
    .ToListAsync();

NpgsqlTsQuery is abstract and the sub classes don't suit.

I can find no examples and the document for Rank is sparse

https://www.npgsql.org/efcore/mapping/full-text-search.html?tabs=pg12%2Cv5


Solution

  • How to form a TsQuery with npgsql (such a great port)

    .OrderByDescending(td => td.SearchVector.Rank(EF.Functions.ToTsQuery(filters.SearchTerm)))