0

I need to do full text search with searching by part of word using entity framework core.

I found this solution Match a phrase ending in a prefix with full text search

I try to write it with Npgsql.EntityFrameworkCore.PostgreSQL. I can parse input with EF.Functions.PlainToTsQuery but can't understand who to cast it to text.

Can I write it with ef core?

VoidName
  • 340
  • 2
  • 16
  • Have you tried following [the documentation](https://www.npgsql.org/efcore/mapping/full-text-search.html?tabs=pg12%2Cv5)? Something like `.Where(p => p.SearchVector.Matches("Npgsql"))`? – Shay Rojansky May 17 '21 at 20:53
  • @ShayRojansky I did not find solutions there, I need something like .Where( x => x.Search.Matches(NpgsqlTsQuery.Parse($@"to_tsquery('simple', regexp_replace(cast(plainto_tsquery('simple', 'Zend Fram') as text), E'(\'\\w+\')', E'\\1:*', 'g'))")) but it doesn't work – VoidName May 18 '21 at 07:13
  • You can't embed an arbitrary PG SQL expression in a string and pass it to `NpgsqlTsQuery.Parse` - that isn't a valid ts_query. You can do something like: `.Where(x => x.Search.Matches(EF.Functions.ToTsQuery("simple", "Zend Fram")))`, but there currently isn't a way to translate to regexp_replace (you can do the regex client-side if you want though, in .NET). – Shay Rojansky May 18 '21 at 22:33

1 Answers1

0

You can try something like:

.Where(p => p.SearchVector.Matches(EF.Functions.ToTsQuery("Npgsql:*"))

It should work similarly to the raw query provided in This question.

You can also use dollar sign interpolation with that to create some dynamic queries.

Mateusz
  • 1
  • 1