From 709d58aa8bed5346c6d520f8b7ab4cfd780f4679 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Thu, 18 Jan 2024 22:32:40 +0300 Subject: [PATCH] Add index to DB --- RhSolutions.Api/Controllers/SearchController.cs | 4 ++-- ...it.Designer.cs => 20240117210635_Init.Designer.cs} | 11 ++++++++--- ...{20230511043408_Init.cs => 20240117210635_Init.cs} | 9 ++++++++- .../Migrations/RhSolutionsContextModelSnapshot.cs | 9 +++++++-- RhSolutions.Api/Models/RhsolutionsContext.cs | 6 +++++- 5 files changed, 30 insertions(+), 9 deletions(-) rename RhSolutions.Api/Migrations/{20230511043408_Init.Designer.cs => 20240117210635_Init.Designer.cs} (85%) rename RhSolutions.Api/Migrations/{20230511043408_Init.cs => 20240117210635_Init.cs} (80%) diff --git a/RhSolutions.Api/Controllers/SearchController.cs b/RhSolutions.Api/Controllers/SearchController.cs index f0402ec..162e961 100644 --- a/RhSolutions.Api/Controllers/SearchController.cs +++ b/RhSolutions.Api/Controllers/SearchController.cs @@ -23,9 +23,9 @@ namespace RhSolutions.Api.Controllers public IAsyncEnumerable SearchProducts([FromQuery] string query) { return context.Products - .Where(p => EF.Functions.ToTsVector( - "russian", string.Join(' ', new[] { p.Name, string.Join(' ', p.ProductLines)})) + .Where(p => EF.Functions.ToTsVector("russian", p.Name) .Matches(EF.Functions.WebSearchToTsQuery("russian", query))) + .Where(p => p.ProductLines.Contains("RAUTITAN")) .OrderByDescending(p => p.IsOnWarehouse) .AsAsyncEnumerable(); } diff --git a/RhSolutions.Api/Migrations/20230511043408_Init.Designer.cs b/RhSolutions.Api/Migrations/20240117210635_Init.Designer.cs similarity index 85% rename from RhSolutions.Api/Migrations/20230511043408_Init.Designer.cs rename to RhSolutions.Api/Migrations/20240117210635_Init.Designer.cs index 887bd5a..8f61c8e 100644 --- a/RhSolutions.Api/Migrations/20230511043408_Init.Designer.cs +++ b/RhSolutions.Api/Migrations/20240117210635_Init.Designer.cs @@ -13,7 +13,7 @@ using RhSolutions.Models; namespace RhSolutions.Api.Migrations { [DbContext(typeof(RhSolutionsContext))] - [Migration("20230511043408_Init")] + [Migration("20240117210635_Init")] partial class Init { /// @@ -21,7 +21,7 @@ namespace RhSolutions.Api.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("ProductVersion", "8.0.0") .HasAnnotation("Relational:MaxIdentifierLength", 63); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); @@ -34,7 +34,7 @@ namespace RhSolutions.Api.Migrations b.Property("DeliveryMakeUp") .HasColumnType("double precision"); - b.Property>("DeprecatedSkus") + b.Property("DeprecatedSkus") .IsRequired() .HasColumnType("text[]"); @@ -61,6 +61,11 @@ namespace RhSolutions.Api.Migrations b.HasKey("Id"); + b.HasIndex("Name") + .HasAnnotation("Npgsql:TsVectorConfig", "russian"); + + NpgsqlIndexBuilderExtensions.HasMethod(b.HasIndex("Name"), "GIN"); + b.ToTable("Products"); }); #pragma warning restore 612, 618 diff --git a/RhSolutions.Api/Migrations/20230511043408_Init.cs b/RhSolutions.Api/Migrations/20240117210635_Init.cs similarity index 80% rename from RhSolutions.Api/Migrations/20230511043408_Init.cs rename to RhSolutions.Api/Migrations/20240117210635_Init.cs index 7f9427f..ca7ba91 100644 --- a/RhSolutions.Api/Migrations/20230511043408_Init.cs +++ b/RhSolutions.Api/Migrations/20240117210635_Init.cs @@ -18,7 +18,7 @@ namespace RhSolutions.Api.Migrations Id = table.Column(type: "text", nullable: false), Name = table.Column(type: "text", nullable: false), ProductSku = table.Column(type: "text", nullable: false), - DeprecatedSkus = table.Column>(type: "text[]", nullable: false), + DeprecatedSkus = table.Column(type: "text[]", nullable: false), ProductLines = table.Column>(type: "text[]", nullable: false), IsOnWarehouse = table.Column(type: "boolean", nullable: false), ProductMeasure = table.Column(type: "integer", nullable: false), @@ -29,6 +29,13 @@ namespace RhSolutions.Api.Migrations { table.PrimaryKey("PK_Products", x => x.Id); }); + + migrationBuilder.CreateIndex( + name: "IX_Products_Name", + table: "Products", + column: "Name") + .Annotation("Npgsql:IndexMethod", "GIN") + .Annotation("Npgsql:TsVectorConfig", "russian"); } /// diff --git a/RhSolutions.Api/Migrations/RhSolutionsContextModelSnapshot.cs b/RhSolutions.Api/Migrations/RhSolutionsContextModelSnapshot.cs index 0052799..de714c1 100644 --- a/RhSolutions.Api/Migrations/RhSolutionsContextModelSnapshot.cs +++ b/RhSolutions.Api/Migrations/RhSolutionsContextModelSnapshot.cs @@ -18,7 +18,7 @@ namespace RhSolutions.Api.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("ProductVersion", "8.0.0") .HasAnnotation("Relational:MaxIdentifierLength", 63); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); @@ -31,7 +31,7 @@ namespace RhSolutions.Api.Migrations b.Property("DeliveryMakeUp") .HasColumnType("double precision"); - b.Property>("DeprecatedSkus") + b.Property("DeprecatedSkus") .IsRequired() .HasColumnType("text[]"); @@ -58,6 +58,11 @@ namespace RhSolutions.Api.Migrations b.HasKey("Id"); + b.HasIndex("Name") + .HasAnnotation("Npgsql:TsVectorConfig", "russian"); + + NpgsqlIndexBuilderExtensions.HasMethod(b.HasIndex("Name"), "GIN"); + b.ToTable("Products"); }); #pragma warning restore 612, 618 diff --git a/RhSolutions.Api/Models/RhsolutionsContext.cs b/RhSolutions.Api/Models/RhsolutionsContext.cs index be0fcb5..e21818a 100644 --- a/RhSolutions.Api/Models/RhsolutionsContext.cs +++ b/RhSolutions.Api/Models/RhsolutionsContext.cs @@ -18,5 +18,9 @@ public class RhSolutionsContext : DbContext .PrimitiveCollection(e => e.DeprecatedSkus) .ElementType() .HasConversion(typeof(SkuConverter)); - } + builder.Entity() + .HasIndex(b => new { b.Name }) + .HasMethod("GIN") + .IsTsVectorExpressionIndex("russian"); + } }