From 469b99abf84e20e0ccf473773b7f9598b4c76359 Mon Sep 17 00:00:00 2001 From: IgorAlymov Date: Wed, 31 Jul 2024 10:10:06 +0500 Subject: [PATCH] feat: update searching by numbers Refs: SITKO-CORE-T-21 --- .../OpenSearchSearcher.cs | 8 ++++-- .../OpenSearchTests.cs | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Sitko.Core.Search.OpenSearch/OpenSearchSearcher.cs b/src/Sitko.Core.Search.OpenSearch/OpenSearchSearcher.cs index 2a9dc5231..b69585945 100644 --- a/src/Sitko.Core.Search.OpenSearch/OpenSearchSearcher.cs +++ b/src/Sitko.Core.Search.OpenSearch/OpenSearchSearcher.cs @@ -246,11 +246,15 @@ private static SearchDescriptor GetSearchRequest(SearchDescriptor< switch (searchType) { case SearchType.Morphology: - descriptor.Query(q => q.QueryString(qs => qs.Query(names))); + descriptor.Query(q => q.QueryString(qs => + qs.Fields(fieldsDescriptor => fieldsDescriptor.Field(searchModel => searchModel.Title) + .Field(searchModel => searchModel.Content)).Query(names))); break; case SearchType.Wildcard: descriptor.Query(q => - q.QueryString(qs => qs.Query($"*{names}*").AnalyzeWildcard())); + q.QueryString(qs => qs.Fields(fieldsDescriptor => fieldsDescriptor + .Field(searchModel => searchModel.Title) + .Field(searchModel => searchModel.Content)).Query($"*{names}*").AnalyzeWildcard())); break; } diff --git a/tests/Sitko.Core.Search.OpenSearch.Tests/OpenSearchTests.cs b/tests/Sitko.Core.Search.OpenSearch.Tests/OpenSearchTests.cs index 534dbcd37..0401da72e 100644 --- a/tests/Sitko.Core.Search.OpenSearch.Tests/OpenSearchTests.cs +++ b/tests/Sitko.Core.Search.OpenSearch.Tests/OpenSearchTests.cs @@ -149,6 +149,32 @@ public async Task PartialSearchRusTestAsync(int foundDocs, string searchText) var result = await searchProvider.SearchAsync(searchText, 10, SearchType.Wildcard); result.Length.Should().Be(foundDocs); } + + [Theory(DisplayName = "SearchByNumbersTest")] + [InlineData(2, "74", SearchType.Morphology)] + [InlineData(2, "74", SearchType.Wildcard)] + public async Task SearchByNumbersTestAsync(int foundDocs, string searchText, SearchType searchType) + { + var scope = await GetScopeAsync(); + var provider = scope.GetService(); + var searchProvider = scope.GetService>(); + await searchProvider.DeleteIndexAsync(); + await searchProvider.InitAsync(); + var firstGuid = Guid.Parse("dd134352-da92-4cd2-9c" + searchText + "-440be713aba5"); + var secondGuid = Guid.Parse("dd134352-da92-4cd2-9c88-440be713aba5"); + var thirdGuid = Guid.Parse("dd134352-da92-4cd3-9c88-440be713aba5"); + + var firstModel = new TestModel { Id = firstGuid, Title = "MMI", Description = "MMI", Url = $"/page/{firstGuid.ToString()}" }; + var secondModel = new TestModel { Id = secondGuid, Title = "MMI", Description = searchText, Url = "mmicentre" }; + var thirdModel = new TestModel { Id = thirdGuid, Title = searchText, Description = "MMI", Url = "mmicentre" }; + provider.AddModel(firstModel).AddModel(secondModel).AddModel(thirdModel); + + await searchProvider.AddOrUpdateEntitiesAsync(provider.Models.ToArray()); + await Task.Delay(TimeSpan.FromSeconds(5)); + + var result2 = await searchProvider.SearchAsync(searchText, 10, searchType); + result2.Length.Should().Be(foundDocs); + } } public class OpenSearchTestScope : BaseTestScope