Skip to content

How to manually update a tsvector column? #2317

Closed as not planned
Closed as not planned

Description

@sep2

Hi @roji, I have a search vector column that can only be generated in client side (because I use a custom word segmenter in client code to get the tokens and weights, the postgres built-in tokenizer cannot meet the requirements). I have see your commnent in #1899 said that

you need to instantiate a TsVector in .NET and assign that as you would with any other column type.

However, I'm not sure how to do it correctly. Specifically I want to do the following update statement:

UPDATE table1
SET search_vector = setweight(to_tsvector('simple', @aTokens), 'A') ||
                    setweight(to_tsvector('simple', @bTokens), 'B')
WHERE id = @id

Where @aTokens and @bTokens are space-saparated tokens in a string type.

Do you have any suggestion on this?

Activity

  1. roji commented on Apr 1, 2022

    @roji
    Member

    Inserting/updating a column based on the output of a server function is something that isn't currently supported in EF Core. If you can't manage to get a TsVector in .NET which corresponds to what you want, you'll currently have to drop to raw SQL to execute the SQL above.

    Note that dotnet/efcore#795 (planned for 7.0) would allow expressing the above in LINQ, but without passing through change tracking.

  2. payoff commented on Feb 24, 2023

    @payoff

    I'm using a NpgsqlTsVector property with TPT in EFCore 6. Data to be inserted into the tsvector comes from different tables with different logics, so was not possible to use generated columns.

    To update the data, we initially used triggers, but this caused issues with EFCore (xmin).

    When we switched to direct update, we attempted to use NpgsqlTsVector.Parse, but we found that it doesn't handle the config (we use a not english configuration).

    Therefore, the solution was to add ExecuteSqlRaw in SaveChangesAsync with
    update TABLE set full-text = to_tsvector(config, text)
    on entity insert or update

    Is this the only option?

  3. roji commented on Feb 24, 2023

    @roji
    Member

    To update the data, we initially used triggers, but this caused issues with EFCore (xmin).

    It sounds if you have to have multiple tables, then triggers are probably the way to go. I'd be interested in knowing which exact problems you had with xmin - can you please open a new issue with a minimal code sample showing the problems you had?

    When we switched to direct update, we attempted to use NpgsqlTsVector.Parse, but we found that it doesn't handle the config (we use a not english configuration).

    The EF provider exposes EF.Functions.ToTsVector which accepts a config.

    Therefore, the solution was to add ExecuteSqlRaw in SaveChangesAsync with

    Note that EF Core 7.0 added support for ExecuteUpdate, so you should no longer need to use SQL for this. However, I still thing the trigger solution is the better (and more efficient) one.

  4. payoff commented on Feb 24, 2023

    @payoff

    can you please open a new issue with a minimal code sample showing the problems you had?

    ok

    The EF provider exposes EF.Functions.ToTsVector which accepts a config.

    This works with queries, but not with updates :
    entity.FullText = NpgsqlTsVector.Parse(text) works
    entity.FullText =EF.Functions.ToTsVector(..) throws

  5. roji commented on Feb 24, 2023

    @roji
    Member

    @payoff that's true.

    First, NpgsqlTsVector.Parse is an attempt at a client-side .NET implementation matching the to_tsvector; this was done a long time ago and most probably should not exist (see npgsql/npgsql#4946). Even without the problem of accepting a configuration, it cannot possibly fully perform the tasks it needs to, like normalizing plural words to singular. We may obsolete/remove this in a future release.

    Unfortunately, EF currently doesn't allow calling a function when inserting (i.e. to_tsvector); that would be covered by dotnet/efcore#10861.

    But in any case, the right solution here isn't to reinsert the tsvector column each time, but rather to have it computed for you in the database, e.g. via triggers.

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