0
0

Add index to DB

This commit is contained in:
Serghei Cebotari 2024-01-18 22:32:40 +03:00
parent b0b1008ade
commit 709d58aa8b
5 changed files with 30 additions and 9 deletions

View File

@ -23,9 +23,9 @@ namespace RhSolutions.Api.Controllers
public IAsyncEnumerable<Product> SearchProducts([FromQuery] string query) public IAsyncEnumerable<Product> SearchProducts([FromQuery] string query)
{ {
return context.Products return context.Products
.Where(p => EF.Functions.ToTsVector( .Where(p => EF.Functions.ToTsVector("russian", p.Name)
"russian", string.Join(' ', new[] { p.Name, string.Join(' ', p.ProductLines)}))
.Matches(EF.Functions.WebSearchToTsQuery("russian", query))) .Matches(EF.Functions.WebSearchToTsQuery("russian", query)))
.Where(p => p.ProductLines.Contains("RAUTITAN"))
.OrderByDescending(p => p.IsOnWarehouse) .OrderByDescending(p => p.IsOnWarehouse)
.AsAsyncEnumerable(); .AsAsyncEnumerable();
} }

View File

@ -13,7 +13,7 @@ using RhSolutions.Models;
namespace RhSolutions.Api.Migrations namespace RhSolutions.Api.Migrations
{ {
[DbContext(typeof(RhSolutionsContext))] [DbContext(typeof(RhSolutionsContext))]
[Migration("20230511043408_Init")] [Migration("20240117210635_Init")]
partial class Init partial class Init
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -21,7 +21,7 @@ namespace RhSolutions.Api.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "7.0.5") .HasAnnotation("ProductVersion", "8.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63); .HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@ -34,7 +34,7 @@ namespace RhSolutions.Api.Migrations
b.Property<double?>("DeliveryMakeUp") b.Property<double?>("DeliveryMakeUp")
.HasColumnType("double precision"); .HasColumnType("double precision");
b.Property<List<string>>("DeprecatedSkus") b.Property<string[]>("DeprecatedSkus")
.IsRequired() .IsRequired()
.HasColumnType("text[]"); .HasColumnType("text[]");
@ -61,6 +61,11 @@ namespace RhSolutions.Api.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Name")
.HasAnnotation("Npgsql:TsVectorConfig", "russian");
NpgsqlIndexBuilderExtensions.HasMethod(b.HasIndex("Name"), "GIN");
b.ToTable("Products"); b.ToTable("Products");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618

View File

@ -18,7 +18,7 @@ namespace RhSolutions.Api.Migrations
Id = table.Column<string>(type: "text", nullable: false), Id = table.Column<string>(type: "text", nullable: false),
Name = table.Column<string>(type: "text", nullable: false), Name = table.Column<string>(type: "text", nullable: false),
ProductSku = table.Column<string>(type: "text", nullable: false), ProductSku = table.Column<string>(type: "text", nullable: false),
DeprecatedSkus = table.Column<List<string>>(type: "text[]", nullable: false), DeprecatedSkus = table.Column<string[]>(type: "text[]", nullable: false),
ProductLines = table.Column<List<string>>(type: "text[]", nullable: false), ProductLines = table.Column<List<string>>(type: "text[]", nullable: false),
IsOnWarehouse = table.Column<bool>(type: "boolean", nullable: false), IsOnWarehouse = table.Column<bool>(type: "boolean", nullable: false),
ProductMeasure = table.Column<int>(type: "integer", nullable: false), ProductMeasure = table.Column<int>(type: "integer", nullable: false),
@ -29,6 +29,13 @@ namespace RhSolutions.Api.Migrations
{ {
table.PrimaryKey("PK_Products", x => x.Id); 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");
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -18,7 +18,7 @@ namespace RhSolutions.Api.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "7.0.5") .HasAnnotation("ProductVersion", "8.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63); .HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@ -31,7 +31,7 @@ namespace RhSolutions.Api.Migrations
b.Property<double?>("DeliveryMakeUp") b.Property<double?>("DeliveryMakeUp")
.HasColumnType("double precision"); .HasColumnType("double precision");
b.Property<List<string>>("DeprecatedSkus") b.Property<string[]>("DeprecatedSkus")
.IsRequired() .IsRequired()
.HasColumnType("text[]"); .HasColumnType("text[]");
@ -58,6 +58,11 @@ namespace RhSolutions.Api.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Name")
.HasAnnotation("Npgsql:TsVectorConfig", "russian");
NpgsqlIndexBuilderExtensions.HasMethod(b.HasIndex("Name"), "GIN");
b.ToTable("Products"); b.ToTable("Products");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618

View File

@ -18,5 +18,9 @@ public class RhSolutionsContext : DbContext
.PrimitiveCollection(e => e.DeprecatedSkus) .PrimitiveCollection(e => e.DeprecatedSkus)
.ElementType() .ElementType()
.HasConversion(typeof(SkuConverter)); .HasConversion(typeof(SkuConverter));
builder.Entity<Product>()
.HasIndex(b => new { b.Name })
.HasMethod("GIN")
.IsTsVectorExpressionIndex("russian");
} }
} }