You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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
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.
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.
Activity
roji commentedon Apr 1, 2022
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.
payoff commentedon Feb 24, 2023
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?
roji commentedon Feb 24, 2023
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?
The EF provider exposes EF.Functions.ToTsVector which accepts a config.
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.
payoff commentedon Feb 24, 2023
ok
This works with queries, but not with updates :
entity.FullText = NpgsqlTsVector.Parse(text)worksentity.FullText =EF.Functions.ToTsVector(..)throwsroji commentedon Feb 24, 2023
@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.