Skip to content

Save TsVector with custom weights gives error "the query has switched to client-evaluation" #1899

Closed

Description

@dctremblay

Hello, I am trying to save the SearchVector with different weights per properties.

// ...
var richContent = HtmlToText(entry.RichContent);
entry.SearchVector = EF.Functions.ToTsVector("english", entry.Name).SetWeight(Weight.A)
             .Concat(EF.Functions.ToTsVector("english", entry.Description).SetWeight(Weight.B))
             .Concat(EF.Functions.ToTsVector("english", richContent).SetWeight(Weight.C));
Context.Add(entry);
Context.SaveChanges();

Unfortunately I get the following error :

System.InvalidOperationException: The 'ToTsVector' method is not supported because the query has switched to client-evaluation. This usually happens when the arguments to the method cannot be translated to server. Rewrite the query to avoid client evaluation of arguments so that method can be translated to server.
         at Microsoft.EntityFrameworkCore.NpgsqlFullTextSearchDbFunctionsExtensions.ToTsVector(DbFunctions _, String config, String document)

How can I make this work ?

Thanks

Activity

  1. roji commented on Jul 16, 2021

    @roji
    Member

    @dctremblay sorry for responding so late on this...

    You can only use the EF.Functions inside queries - EF.Functions.ToTsVector translates to the PostgreSQL to_tsvector function; there is no .NET implementation that can be executed client-side.

    Typically, tsvector columns are auto-updated, e.g. by making them generated columns (see the docs). If you'd like to update tsvectors manually, you need to instantiate a TsVector in .NET and assign that as you would with any other column type.

    I'm closing this for now, but please don't hesitate to post back with further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions