private Task <DocumentSearchResult> SearchIndex(string indexName, SearchFormModel model) { SearchIndexClient indexClient = AzureSearchHelper.CreateSearchIndexClient(indexName: indexName); SearchParameters searchParameter = CreateSearchParameters(); return(indexClient.Documents.SearchAsync( searchText: model.SearchText, searchParameters: searchParameter, searchRequestOptions: null)); }
private async Task <EkPartNumberBrand[]> FindIndexedPartNumberBrandsAsync( string cleanedPartNumber, CancellationToken cancellationToken) { using (var searchIndexClient = AzureSearchHelper.CreateSearchIndexClient(_ekSearchSettings.ServiceName, _ekSearchSettings.QueryKey)) { searchIndexClient.IndexName = _ekSearchSettings.ProductsIndexName; var searchParameters = new SearchParameters() { Top = 100, SearchFields = new[] { "cleanedPartNumber" }, }; var searchResult = await searchIndexClient.Documents.SearchAsync <IndexProduct>( cleanedPartNumber, searchParameters, cancellationToken : cancellationToken); var matchingProducts = searchResult.Results .Select(x => x.Document) .Select(x => new { PartNumberBrand = new EkPartNumberBrand() { ProductKey = x.Key, BrandName = x.BrandName, PartNumber = x.PartNumber, Name = new MultiLanguageString() { [Languages.RussianCode] = x.Name_ru, }, }, BrandKey = GetBrandKey(x.BrandName), Price = (decimal)(x.Price ?? 0), } ) .Where(x => !string.IsNullOrEmpty(x.BrandKey) && x.Price > 0) .ToArray(); // return the cheapest product for each brand return(matchingProducts .GroupBy(x => x.BrandKey) .Select(x => x .OrderBy(p => p.Price) .Select(p => p.PartNumberBrand) .First()) .ToArray()); } }
public override async Task <EkKioskAutocompleteOptionsGetResponse> ExecuteAsync(EkKioskAutocompleteOptionsGetRequest request) { if (string.IsNullOrEmpty(request.Term) || request.SearchType != EkSearchTypeEnum.Name) { return(new EkKioskAutocompleteOptionsGetResponse() { AutocompleteOptions = new string[0], }); } // cancellation token var cancellationToken = _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None; using (var searchIndexClient = AzureSearchHelper.CreateSearchIndexClient(_ekSearchSettings.ServiceName, _ekSearchSettings.QueryKey)) { searchIndexClient.IndexName = _ekSearchSettings.ProductsIndexName; var autocompleteParameters = new AutocompleteParameters() { // todo: request by OneTermWithContext first, only then by OneTerm (if not enough results) AutocompleteMode = AutocompleteMode.OneTerm, UseFuzzyMatching = true, Top = 10, SearchFields = EkKioskProductSearchByNameGet.GetLanguageSpecificTextFields(request.LanguageCode), }; var autocompleteResult = await searchIndexClient.Documents.AutocompleteAsync( request.Term, SearchConstants.SuggesterName, autocompleteParameters, cancellationToken : cancellationToken); var autocompleteOptions = autocompleteResult.Results .Select(x => x.Text) .ToArray(); return(new EkKioskAutocompleteOptionsGetResponse() { AutocompleteOptions = autocompleteOptions, }); } }
public override async Task <EkKioskProductSearchByNameGetResponse> ExecuteAsync(EkKioskProductSearchByNameGetRequest request) { if (string.IsNullOrEmpty(request.Term) || request.Term.Length < 3) { return(new EkKioskProductSearchByNameGetResponse() { Products = new EkProduct[0], }); } // cancellation token var cancellationToken = _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None; using (var searchIndexClient = AzureSearchHelper.CreateSearchIndexClient(_ekSearchSettings.ServiceName, _ekSearchSettings.QueryKey)) { searchIndexClient.IndexName = _ekSearchSettings.ProductsIndexName; // paging var indexSearchParameters = new SearchParameters() { Skip = request.From, Top = request.Count, IncludeTotalResultCount = request.IncludeTotal, SearchFields = GetLanguageSpecificTextFields(request.LanguageCode), ScoringProfile = SearchConstants.BoostNameScoringProfileName, }; // sorting switch (request.Sorting) { case EkProductSearchSortingEnum.PriceAscending: indexSearchParameters.OrderBy = new[] { "price asc" }; break; case EkProductSearchSortingEnum.PriceDescending: indexSearchParameters.OrderBy = new[] { "price desc" }; break; case EkProductSearchSortingEnum.Default: default: // no sorting break; } // term var term = request.Term; var searchResult = await searchIndexClient.Documents.SearchAsync <IndexProduct>( term, indexSearchParameters, cancellationToken : cancellationToken); var products = searchResult.Results .Select(x => EkConvertHelper.EkNewIndexProductToProduct(x.Document)) .ToArray(); var total = searchResult.Count ?? 0; return(new EkKioskProductSearchByNameGetResponse() { Products = products, Total = total, }); } }
public override async Task <EkKioskProductAndReplacementsByPartNumberGetResponse> ExecuteAsync(EkKioskProductAndReplacementsByPartNumberGetRequest request) { Assure.ArgumentNotNull(request.PartNumberBrand, nameof(request.PartNumberBrand)); Assure.ArgumentNotNull(request.PartNumberBrand.ProductKey, nameof(request.PartNumberBrand.ProductKey)); Assure.ArgumentNotNull(request.PartNumberBrand.PartNumber, nameof(request.PartNumberBrand.PartNumber)); var response = new EkKioskProductAndReplacementsByPartNumberGetResponse(); // cancellation token var cancellationToken = _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None; using (var searchIndexClient = AzureSearchHelper.CreateSearchIndexClient(_ekSearchSettings.ServiceName, _ekSearchSettings.QueryKey)) { searchIndexClient.IndexName = _ekSearchSettings.ProductsIndexName; // FIND PRODUCT try { var indexProduct = await searchIndexClient.Documents.GetAsync <IndexProduct>( request.PartNumberBrand.ProductKey, cancellationToken : cancellationToken); response.Product = EkConvertHelper.EkNewIndexProductToProduct(indexProduct); // todo: add search by Brand/PartNumber to find all direct matches since many products sources are supported // it's not done since new product search model is planned anyway } catch (CloudException) { response.Product = EkConvertHelper.EkOmegaPartNumberBrandToProduct(request.PartNumberBrand); } // FIND REPLACEMENTS var cleanedPartNumber = PartNumberCleaner.GetCleanedPartNumber(request.PartNumberBrand.PartNumber); // TecDoc replacements // todo: add cancellationToken support to proxy based clients var tecDocReplacements = await _tecDocWsClient.SearchByArticleNumberAsync(cleanedPartNumber); tecDocReplacements = tecDocReplacements // except direct match .Where(x => x.NumberType != ArticleNumberTypeEnum.ArticleNumber) .ToArray(); var replacementCleanedBrandPartNumbers = tecDocReplacements .Select(x => PartNumberCleaner.GetCleanedBrandPartNumber(x.BrandName, x.ArticleNo)) .Take(100) .ToArray(); if (replacementCleanedBrandPartNumbers.Length > 0) { var replacementsIndexSearchParameters = new SearchParameters() { Top = 100, SearchFields = new[] { "cleanedBrandPartNumber" }, }; var searchTerm = string.Join("|", replacementCleanedBrandPartNumbers); var searchResult = await searchIndexClient.Documents.SearchAsync <IndexProduct>( searchTerm, replacementsIndexSearchParameters, cancellationToken : cancellationToken); response.Replacements = searchResult.Results .Select(x => EkConvertHelper.EkNewIndexProductToProduct(x.Document)) .ToArray(); } else { response.Replacements = new EkProduct[0]; } return(response); } }
public override async Task <EkKioskProductSearchByCategoryGetResponse> ExecuteAsync(EkKioskProductSearchByCategoryGetRequest request) { var response = new EkKioskProductSearchByCategoryGetResponse(); // cancellation token var cancellationToken = _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None; // todo: add cancellationToken support to proxy based clients // determine TecDoc car type of modification first CarTypeEnum carType; // request categories for cars first var categories = await _tecDocWsClient.GetCategoriesAsync(CarTypeEnum.Car, request.ModificationId, null, childNodes : false); if (categories?.Length > 0) { carType = CarTypeEnum.Car; } else { // then request for trucks categories = await _tecDocWsClient.GetCategoriesAsync(CarTypeEnum.Truck, request.ModificationId, null, childNodes : false); if (categories?.Length > 0) { carType = CarTypeEnum.Truck; } else { return(response); } } const int MaxProductCount = 200; // TecDoc articles var tecDocProducts = await _tecDocWsClient.GetArticlesCompactInfoAsync(carType, request.ModificationId, request.CategoryId); var productCleanedBrandPartNumbers = tecDocProducts .Select(x => PartNumberCleaner.GetCleanedBrandPartNumber(x.BrandName, x.ArticleNo)) .Take(MaxProductCount) .ToArray(); if (productCleanedBrandPartNumbers.Length == 0) { return(response); } // FIND PRODUCTS IN STOCK using (var searchIndexClient = AzureSearchHelper.CreateSearchIndexClient(_ekSearchSettings.ServiceName, _ekSearchSettings.QueryKey)) { searchIndexClient.IndexName = _ekSearchSettings.ProductsIndexName; var replacementsIndexSearchParameters = new SearchParameters() { Top = MaxProductCount, SearchFields = new[] { "cleanedBrandPartNumber" }, }; var searchTerm = string.Join("|", productCleanedBrandPartNumbers); var searchResult = await searchIndexClient.Documents.SearchAsync <IndexProduct>( searchTerm, replacementsIndexSearchParameters, cancellationToken : cancellationToken); response.Products = searchResult.Results .Select(x => EkConvertHelper.EkNewIndexProductToProduct(x.Document)) .ToArray(); return(response); } }