diff --git a/src/EFCore.PG/Extensions/DbFunctionsExtensions/NpgsqlFullTextSearchLinqExtensions.cs b/src/EFCore.PG/Extensions/DbFunctionsExtensions/NpgsqlFullTextSearchLinqExtensions.cs index 9707c61a9..ae49ee137 100644 --- a/src/EFCore.PG/Extensions/DbFunctionsExtensions/NpgsqlFullTextSearchLinqExtensions.cs +++ b/src/EFCore.PG/Extensions/DbFunctionsExtensions/NpgsqlFullTextSearchLinqExtensions.cs @@ -90,6 +90,15 @@ public static string GetResultHeadline(this NpgsqlTsQuery query, string config, public static NpgsqlTsQuery Rewrite(this NpgsqlTsQuery query, NpgsqlTsQuery target, NpgsqlTsQuery substitute) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(NpgsqlTsQuery) + "." + nameof(Rewrite))); + /// + /// For each row of the SQL result, occurrences of the first column value (the target) + /// are replaced by the second column value (the substitute) within the current value. + /// The must yield two columns of tsquery type. + /// http://www.postgresql.org/docs/current/static/textsearch-features.html#TEXTSEARCH-MANIPULATE-TSQUERY + /// + public static NpgsqlTsQuery Rewrite(this NpgsqlTsQuery query, string select) + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(NpgsqlTsQuery) + "." + nameof(Rewrite))); + /// /// Returns a tsquery that searches for a match to followed by a match /// to . diff --git a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlFullTextSearchMethodTranslator.cs b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlFullTextSearchMethodTranslator.cs index dd33be268..eec423f94 100644 --- a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlFullTextSearchMethodTranslator.cs +++ b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlFullTextSearchMethodTranslator.cs @@ -229,7 +229,16 @@ arguments[1] is SqlConstantExpression constant argumentsPropagateNullability: TrueArrays[4], method.ReturnType), - nameof(NpgsqlFullTextSearchLinqExtensions.Rewrite) + nameof(NpgsqlFullTextSearchLinqExtensions.Rewrite) when arguments.Count == 2 + => _sqlExpressionFactory.Function( + "ts_rewrite", + arguments, + nullable: true, + argumentsPropagateNullability: TrueArrays[2], + typeof(NpgsqlTsQuery), + _tsQueryMapping), + + nameof(NpgsqlFullTextSearchLinqExtensions.Rewrite) when arguments.Count == 3 => _sqlExpressionFactory.Function( "ts_rewrite", arguments, diff --git a/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs index 276b32312..419a36425 100644 --- a/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs @@ -531,6 +531,25 @@ LIMIT 1 """); } + [Fact] + public void Rewrite_with_select() + { + using var context = CreateContext(); + var rewritten = context.Customers + .Select( + c => EF.Functions.ToTsQuery("a & b").Rewrite( + """SELECT 'a'::tsquery, 'c'::tsquery""")) + .First(); + + Assert.NotNull(rewritten); + AssertSql( +""" +SELECT ts_rewrite(to_tsquery('a & b'), 'SELECT ''a''::tsquery, ''c''::tsquery') +FROM "Customers" AS c +LIMIT 1 +"""); + } + [Fact] public void ToPhrase() {