From 961e66fcfc2dc0d0c5c907d8fcf9bd6ab368b412 Mon Sep 17 00:00:00 2001 From: uleus Date: Thu, 4 Feb 2021 10:26:25 +0100 Subject: [PATCH] Serach - autocomplate --- .../Indexing/IndexBaseItem.cs | 8 ++--- .../Mapping/SearchableBaseItem.cs | 3 +- .../Mapping/SearchablePropertyItem.cs | 8 ++--- .../Search/SearchService.cs | 9 +++--- .../Search/SimpleSearchRequest.cs | 14 +++------ .../Search.IndexData.Exe/DataProvider.cs | 30 +++++++++---------- Elasticsearch/Search.IndexData.Exe/Program.cs | 7 ----- .../Search.IndexData.Exe.csproj | 5 ---- .../Controllers/SearchController.cs | 14 ++------- 9 files changed, 32 insertions(+), 66 deletions(-) diff --git a/Elasticsearch/Search.Elasticsearch/Indexing/IndexBaseItem.cs b/Elasticsearch/Search.Elasticsearch/Indexing/IndexBaseItem.cs index 0ea8637..49fc1d0 100644 --- a/Elasticsearch/Search.Elasticsearch/Indexing/IndexBaseItem.cs +++ b/Elasticsearch/Search.Elasticsearch/Indexing/IndexBaseItem.cs @@ -48,12 +48,12 @@ protected static IAnalysis InitCommonAnalyzers(AnalysisDescriptor analysis) { return analysis.Analyzers(a => a .Custom("autocomplete", ca => ca - .Filters("eng_stopwords", "trim", "lowercase") .Tokenizer("autocomplete") + .Filters("stopwords_eng", "trim", "lowercase") ) .Custom("autocomplete_search", ca => ca - .Filters("eng_stopwords", "trim", "lowercase") - .Tokenizer("lowercase") + .Tokenizer("standard") + .Filters("stopwords_eng", "trim", "lowercase") ) ) .Tokenizers(tdesc => tdesc @@ -64,7 +64,7 @@ protected static IAnalysis InitCommonAnalyzers(AnalysisDescriptor analysis) ) ) .TokenFilters(f => f - .Stop("eng_stopwords", lang => lang + .Stop("stopwords_eng", lang => lang .StopWords("_english_") ) ); diff --git a/Elasticsearch/Search.Elasticsearch/Mapping/SearchableBaseItem.cs b/Elasticsearch/Search.Elasticsearch/Mapping/SearchableBaseItem.cs index 73e392e..0d36e53 100644 --- a/Elasticsearch/Search.Elasticsearch/Mapping/SearchableBaseItem.cs +++ b/Elasticsearch/Search.Elasticsearch/Mapping/SearchableBaseItem.cs @@ -10,10 +10,9 @@ public class SearchableBaseItem [Text(Analyzer = "autocomplete", SearchAnalyzer = "autocomplete_search", Name = nameof(Name))] public string Name { get; set; } - [Text(/*Analyzer = "autocomplete",*/ Name = nameof(Market))] + [Text(Analyzer = "autocomplete", SearchAnalyzer = "autocomplete_search", Name = nameof(Market))] public string Market { get; set; } - [Text(/*Analyzer = "autocomplete",*/ Name = nameof(State))] public string State { get; set; } public SearchableBaseItem() diff --git a/Elasticsearch/Search.Elasticsearch/Mapping/SearchablePropertyItem.cs b/Elasticsearch/Search.Elasticsearch/Mapping/SearchablePropertyItem.cs index cd0881c..6f121d8 100644 --- a/Elasticsearch/Search.Elasticsearch/Mapping/SearchablePropertyItem.cs +++ b/Elasticsearch/Search.Elasticsearch/Mapping/SearchablePropertyItem.cs @@ -7,19 +7,17 @@ public class SearchablePropertyItem : SearchableBaseItem { public const string TypeName = "searchablepropertyitem"; - [Text(/*Analyzer = "autocomplete", */Name = nameof(FormerName))] + [Text(Analyzer = "autocomplete", SearchAnalyzer = "autocomplete_search", Name = nameof(FormerName))] public string FormerName { get; set; } - [Text(/*Analyzer = "autocomplete", */Name = nameof(StreetAddres))] + [Text(Analyzer = "autocomplete", SearchAnalyzer = "autocomplete_search", Name = nameof(StreetAddres))] public string StreetAddres { get; set; } - [Text(/*Analyzer = "autocomplete", */Name = nameof(City))] + [Text(Analyzer = "autocomplete", SearchAnalyzer = "autocomplete_search", Name = nameof(City))] public string City { get; set; } - //[Text(Name=nameof(Lat))] public float Lat { get; set; } - //[Text(Name = nameof(Lng))] public float Lng { get; set; } } } diff --git a/Elasticsearch/Search.Elasticsearch/Search/SearchService.cs b/Elasticsearch/Search.Elasticsearch/Search/SearchService.cs index e628814..81c08e0 100644 --- a/Elasticsearch/Search.Elasticsearch/Search/SearchService.cs +++ b/Elasticsearch/Search.Elasticsearch/Search/SearchService.cs @@ -23,14 +23,14 @@ public SearchService(IElasticClientFactoryService aElasticClientFactoryService) public async Task Search(SimpleSearchRequest aSearchRequest) { BoolQuery filterQuery = new BoolQuery(); - if (!string.IsNullOrEmpty(aSearchRequest.Filter)) + if (!string.IsNullOrEmpty(aSearchRequest.MarketFilterQuery)) { var filterQueryParts = new List(); filterQueryParts.Add( new MatchQuery() { Field = $"{nameof(SearchableBaseItem.Market)}", - Query = aSearchRequest.Filter.ToLower(), + Query = aSearchRequest.MarketFilterQuery.ToLower(), Fuzziness = Fuzziness.Auto } ); @@ -41,11 +41,10 @@ public async Task Search(SimpleSearchRequest aSearchReques var results = await _client.SearchAsync(s => s .Size(aSearchRequest.PageSize) .Skip(aSearchRequest.PageStartIndex) - .Index(Indices.Index(aSearchRequest.Indices)) - + .Index(Indices.Index(aSearchRequest.Indices)) .Query(q => q .MultiMatch(m => m - .Query(aSearchRequest.Query.ToLower()) + .Query(aSearchRequest.AllStringFiledsQuery.ToLower()) .Fields(ff => ff .Field($"{nameof(SearchableBaseItem.Name)}") .Field($"{nameof(SearchableBaseItem.Market)}") diff --git a/Elasticsearch/Search.Elasticsearch/Search/SimpleSearchRequest.cs b/Elasticsearch/Search.Elasticsearch/Search/SimpleSearchRequest.cs index 7b0ab9a..9fa75b1 100644 --- a/Elasticsearch/Search.Elasticsearch/Search/SimpleSearchRequest.cs +++ b/Elasticsearch/Search.Elasticsearch/Search/SimpleSearchRequest.cs @@ -1,21 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace Search.Elasticsearch.Search { - + public class SimpleSearchRequest { public List Indices { get; set; } - - public string Query { get; set; } - public List QueryFields { get; set; } + public string AllStringFiledsQuery { get; set; } - - public string Filter { get; set; } - public List FilterFields { get; set; } + public string MarketFilterQuery { get; set; } public int PageSize { get; set; } public int PageStartIndex { get; set; } diff --git a/Elasticsearch/Search.IndexData.Exe/DataProvider.cs b/Elasticsearch/Search.IndexData.Exe/DataProvider.cs index be7a021..69ede2d 100644 --- a/Elasticsearch/Search.IndexData.Exe/DataProvider.cs +++ b/Elasticsearch/Search.IndexData.Exe/DataProvider.cs @@ -1,10 +1,5 @@ -using Nancy.Json; -using Newtonsoft.Json; -using RestSharp; -using Search.Elasticsearch.Indexing; +using Search.Elasticsearch.Indexing; using Search.Elasticsearch.Mapping; -using Search.WebAPI.Exe.Dto; -using System; using System.Collections.Generic; using System.IO; @@ -24,26 +19,29 @@ public DataProvider() new SearchablePropertyItem() { Id= 1, - Name ="My granny has a wooden super chair", - StreetAddres = "test", + Name ="Forest at Columbia", Market="Austin", - State ="GS" + State ="GS", + StreetAddres = "3549 Curry Lane", + City = "Marietta AAA" }, new SearchablePropertyItem() { Id = 2, - Name ="prop2", - StreetAddres = "nowa", + Name ="Forest at Columbia AAAA", + StreetAddres = "1000 Merrick Ferry Road", Market="Austin", - State ="GS" + State ="GS", + City = "Marietta ZZZ" }, new SearchablePropertyItem() { Id = 3, - Name ="prop3", + Name ="Forest AAA Columbia", StreetAddres = "nowa", - Market="San Francisco", - State ="CA" + Market="1000 Bells Ferry Road", + State ="TX", + City = "Marietta" }, }; @@ -52,7 +50,7 @@ public DataProvider() new SearchableManagementItem() { Id = 1, - Name = "Company A", + Name = "Holland Residential", Market="San Paulo", State ="TX" } diff --git a/Elasticsearch/Search.IndexData.Exe/Program.cs b/Elasticsearch/Search.IndexData.Exe/Program.cs index 16f524b..059540d 100644 --- a/Elasticsearch/Search.IndexData.Exe/Program.cs +++ b/Elasticsearch/Search.IndexData.Exe/Program.cs @@ -1,13 +1,6 @@ using Nest; -using Newtonsoft.Json; using Search.Elasticsearch.Indexing; -using Search.Elasticsearch.Mapping; -using Search.WebAPI.Exe.Dto; using System; -using System.Collections.Generic; -using System.IO; -using System.Text.Json; -using static Search.IndexData.Exe.DataProvider; namespace Search.IndexData.Exe { diff --git a/Elasticsearch/Search.IndexData.Exe/Search.IndexData.Exe.csproj b/Elasticsearch/Search.IndexData.Exe/Search.IndexData.Exe.csproj index edab8d0..a6fe81d 100644 --- a/Elasticsearch/Search.IndexData.Exe/Search.IndexData.Exe.csproj +++ b/Elasticsearch/Search.IndexData.Exe/Search.IndexData.Exe.csproj @@ -5,11 +5,6 @@ netcoreapp3.1 - - - - - diff --git a/Elasticsearch/Search.WebAPI.Exe/Controllers/SearchController.cs b/Elasticsearch/Search.WebAPI.Exe/Controllers/SearchController.cs index 7c8a1e0..6fbf3f8 100644 --- a/Elasticsearch/Search.WebAPI.Exe/Controllers/SearchController.cs +++ b/Elasticsearch/Search.WebAPI.Exe/Controllers/SearchController.cs @@ -36,18 +36,8 @@ public async Task Search(SearchCriteriaDto aRequest) { PageSize = aRequest.PageSize, PageStartIndex = aRequest.PageStartIndex, - Query = aRequest.Phase, - QueryFields = new List() - { - nameof(SearchableBaseItem.Name), - nameof(SearchableBaseItem.Market), - nameof(SearchableBaseItem.State), - nameof(SearchablePropertyItem.FormerName), - nameof(SearchablePropertyItem.StreetAddres), - nameof(SearchablePropertyItem.City) - }, - Filter = aRequest.Market, - FilterFields = new List() { nameof(SearchableBaseItem.Market) }, + AllStringFiledsQuery = aRequest.Phase, + MarketFilterQuery = aRequest.Market, Indices = new List() { Config.IndexPropertyItemName, Config.IndexManagementItemName } } );