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)
{
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();
}

View File

@ -13,7 +13,7 @@ using RhSolutions.Models;
namespace RhSolutions.Api.Migrations
{
[DbContext(typeof(RhSolutionsContext))]
[Migration("20230511043408_Init")]
[Migration("20240117210635_Init")]
partial class Init
{
/// <inheritdoc />
@ -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<double?>("DeliveryMakeUp")
.HasColumnType("double precision");
b.Property<List<string>>("DeprecatedSkus")
b.Property<string[]>("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

View File

@ -18,7 +18,7 @@ namespace RhSolutions.Api.Migrations
Id = table.Column<string>(type: "text", nullable: false),
Name = 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),
IsOnWarehouse = table.Column<bool>(type: "boolean", 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);
});
migrationBuilder.CreateIndex(
name: "IX_Products_Name",
table: "Products",
column: "Name")
.Annotation("Npgsql:IndexMethod", "GIN")
.Annotation("Npgsql:TsVectorConfig", "russian");
}
/// <inheritdoc />

View File

@ -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<double?>("DeliveryMakeUp")
.HasColumnType("double precision");
b.Property<List<string>>("DeprecatedSkus")
b.Property<string[]>("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

View File

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