protected virtual GetSearchQueryViewModelParams BuildParameters(string queryName, int page, string sortBy, string sortDirection, int maxItemsPerPage)
        {
            var searchCriteria = new SearchCriteria
            {
                NumberOfItemsPerPage = maxItemsPerPage,
                IncludeFacets        = true,
                StartingIndex        = (page - 1) * maxItemsPerPage,
                SortBy               = sortBy,
                SortDirection        = sortDirection,
                Page                 = page,
                BaseUrl              = RequestUtils.GetBaseUrl(Request).ToString(),
                CultureInfo          = ComposerContext.CultureInfo,
                Scope                = ComposerContext.Scope,
                InventoryLocationIds = GetInventoryLocationIds()
            };

            searchCriteria.SelectedFacets.AddRange(SearchUrlProvider.BuildSelectedFacets(Request.QueryString));

            var param = new GetSearchQueryViewModelParams
            {
                Criteria             = searchCriteria,
                QueryName            = queryName,
                QueryType            = QueryType,
                InventoryLocationIds = GetInventoryLocationIds(),
            };

            return(param);
        }
        public virtual async Task <IHttpActionResult> GetSearchResults(GetQueryFacetsRequest request)
        {
            var queryString    = HttpUtility.ParseQueryString(request.QueryString ?? "");
            var SelectedFacets = SearchUrlProvider.BuildSelectedFacets(queryString).ToList();
            var CurrentPage    = int.TryParse(queryString[SearchRequestParams.Page], out int page) && page > 0 ? page : 1;
            var SortDirection  = queryString[SearchRequestParams.SortDirection] ?? SearchRequestParams.DefaultSortDirection;
            var SortBy         = queryString[SearchRequestParams.SortBy] ?? SearchRequestParams.DefaultSortBy;
            var BaseUrl        = RequestUtils.GetBaseUrl(Request).ToString();

            var searchCriteria = await BaseSearchCriteriaProvider.GetSearchCriteriaAsync(null, BaseUrl, true, CurrentPage).ConfigureAwait(false);

            searchCriteria.SortBy        = SortBy;
            searchCriteria.SortDirection = SortDirection;
            searchCriteria.SelectedFacets.AddRange(SelectedFacets);

            var param = new GetSearchQueryViewModelParams
            {
                QueryName            = request.QueryName,
                QueryType            = request.QueryType,
                Scope                = ComposerContext.Scope,
                CultureInfo          = ComposerContext.CultureInfo,
                Criteria             = searchCriteria,
                InventoryLocationIds = searchCriteria.InventoryLocationIds
            };

            var viewModel = await SearchQueryViewService.GetSearchQueryViewModelAsync(param).ConfigureAwait(false);

            viewModel.ProductSearchResults.Facets = viewModel.ProductSearchResults.Facets.Where(f => !f.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix)).ToList();
            return(Ok(viewModel));
        }
        public virtual async Task <IHttpActionResult> GetFacets(GetQueryFacetsRequest request)
        {
            var queryString = HttpUtility.ParseQueryString(request.QueryString);

            var criteria = await BaseSearchCriteriaProvider.GetSearchCriteriaAsync(null, RequestUtils.GetBaseUrl(Request).ToString(), true).ConfigureAwait(false);

            criteria.NumberOfItemsPerPage = 0;

            criteria.SelectedFacets.AddRange(SearchUrlProvider.BuildSelectedFacets(queryString));

            var param = new GetSearchQueryViewModelParams
            {
                QueryName            = request.QueryName,
                QueryType            = request.QueryType,
                Scope                = ComposerContext.Scope,
                CultureInfo          = ComposerContext.CultureInfo,
                Criteria             = criteria,
                InventoryLocationIds = criteria.InventoryLocationIds
            };

            var viewModel = await SearchQueryViewService.GetSearchQueryViewModelAsync(param).ConfigureAwait(false);

            viewModel.ProductSearchResults.Facets = viewModel.ProductSearchResults.Facets.Where(f => !f.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix)).ToList();

            return(Ok(viewModel));
        }
示例#4
0
        public async Task <SearchQueryViewModel> GetTopSearchQueryViewModelAsync(SearchQueryType queryType, string queryName, int pageSize)
        {
            if (_topResultsViewModel != null && _topResultsViewModel.QueryName == queryName && _topResultsViewModel.QueryType == queryType)
            {
                return(_topResultsViewModel);
            }

            var criteria = BuildProductsSearchCriteria();

            criteria.NumberOfItemsPerPage = pageSize;
            var param = new GetSearchQueryViewModelParams
            {
                QueryName            = queryName,
                QueryType            = queryType,
                Scope                = ComposerContext.Scope,
                CultureInfo          = ComposerContext.CultureInfo,
                Criteria             = criteria,
                InventoryLocationIds = criteria.InventoryLocationIds
            };

            _topResultsViewModel = await SearchQueryViewService.GetSearchQueryViewModelAsync(param).ConfigureAwait(false);

            if (_topResultsViewModel.ProductSearchResults.Pagination != null)
            {
                _topResultsViewModel.ProductSearchResults.Pagination.TotalNumberOfPages = 1;
                _topResultsViewModel.ProductSearchResults.Pagination.NextPage           = null;
            }

            return(_topResultsViewModel);
        }
        public async Task <SearchQueryViewModel> GetSearchQueryViewModelAsync(GetSearchQueryViewModelParams param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            if (ViewModel != null)
            {
                return(ViewModel);
            }

            param.Scope       = ComposerContext.Scope;
            param.CultureInfo = ComposerContext.CultureInfo;

            ViewModel = await SearchQueryViewService.GetSearchQueryViewModelAsync(param).ConfigureAwait(false);

            return(ViewModel);
        }
示例#6
0
        public async Task <SearchQueryViewModel> GetSearchQueryViewModelAsync(SearchQueryType queryType, string queryName)
        {
            if (_viewModel != null && _viewModel.QueryName == queryName && _viewModel.QueryType == queryType)
            {
                return(_viewModel);
            }
            var criteria = BuildProductsSearchCriteria();
            var param    = new GetSearchQueryViewModelParams
            {
                QueryName            = queryName,
                QueryType            = queryType,
                Scope                = ComposerContext.Scope,
                CultureInfo          = ComposerContext.CultureInfo,
                Criteria             = criteria,
                InventoryLocationIds = criteria.InventoryLocationIds
            };

            _viewModel = await SearchQueryViewService.GetSearchQueryViewModelAsync(param).ConfigureAwait(false);

            return(_viewModel);
        }
        public async Task <SearchQueryViewModel> GetSearchQueryViewModelAsync(GetSearchQueryViewModelParams param)
        {
            SearchQueryViewModel viewModel;

            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.Criteria == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria)), nameof(param));
            }

            var searchQueryProducts = await SearchQueryRepository.SearchQueryProductAsync(new SearchQueryProductParams
            {
                CultureName = param.CultureInfo.Name,
                QueryName   = param.QueryName,
                QueryType   = param.QueryType,
                ScopeId     = param.Scope,
                Criteria    = param.Criteria
            }).ConfigureAwait(false);

            var documents = searchQueryProducts.Result.Documents.Select(ToProductDocument).ToList();

            var inventoryLocations = await InventoryRepository.GetInventoryLocationStatusesBySkus(
                new GetInventoryLocationStatuseParam()
            {
                Skus    = documents.Select(d => d.Sku).ToList(),
                ScopeId = param.Scope,
                InventoryLocationIds = param.InventoryLocationIds
            }).ConfigureAwait(false);

            FixInventories(documents, inventoryLocations);
            documents = await FixInventoryFilter(documents, param.Scope).ConfigureAwait(false);

            var getImageParam = new GetProductMainImagesParam
            {
                ImageSize            = SearchConfiguration.DefaultImageSize,
                ProductImageRequests = documents
                                       .Select(document => new ProductImageRequest
                {
                    ProductId = document.ProductId,
                    Variant   = document.PropertyBag.ContainsKey(VariantPropertyBagKey)
                            ? new VariantKey {
                        Id = document.PropertyBag[VariantPropertyBagKey].ToString()
                    }
                            : VariantKey.Empty,
                    ProductDefinitionName = document.PropertyBag.ContainsKey("DefinitionName")
                            ? document.PropertyBag["DefinitionName"].ToString()
                            : string.Empty,
                    PropertyBag = document.PropertyBag
                }).ToList()
            };

            var imageUrls = await DamProvider.GetProductMainImagesAsync(getImageParam).ConfigureAwait(false);



            var newCriteria = param.Criteria.Clone();

            var createSearchViewModelParam = new CreateProductSearchResultsViewModelParam <SearchParam>
            {
                SearchParam = new SearchParam()
                {
                    Criteria = newCriteria
                },
                ImageUrls    = imageUrls,
                SearchResult = new ProductSearchResult()
                {
                    Documents  = documents,
                    TotalCount = searchQueryProducts.Result.TotalCount,
                    Facets     = searchQueryProducts.Result.Facets
                }
            };

            viewModel = new SearchQueryViewModel
            {
                SelectedFacets =
                    await GetSelectedFacetsAsync(createSearchViewModelParam.SearchParam).ConfigureAwait(false),
                ProductSearchResults =
                    await CreateProductSearchResultsViewModelAsync(createSearchViewModelParam).ConfigureAwait(false),
            };

            if (searchQueryProducts.SelectedFacets != null)
            {
                foreach (var facet in searchQueryProducts.SelectedFacets)
                {
                    foreach (var value in facet.Values)
                    {
                        if (viewModel.SelectedFacets.Facets.All(f => f.Value != value))
                        {
                            viewModel.SelectedFacets.Facets.Add(new SelectedFacet()
                            {
                                Value       = value,
                                FieldName   = facet.FacetName,
                                DisplayName = value,
                                IsRemovable = false
                            });
                        }
                    }
                }

                foreach (var selectedFacet in searchQueryProducts.SelectedFacets)
                {
                    foreach (var facet in viewModel.ProductSearchResults.Facets.Where(d => d.FieldName == selectedFacet.FacetName))
                    {
                        foreach (var facetValue in selectedFacet.Values.Select(value => facet.FacetValues.FirstOrDefault(f => f.Value == value)).Where(facetValue => facetValue != null))
                        {
                            facetValue.IsSelected = true;
                        }
                    }
                }
            }

            viewModel.Context[nameof(viewModel.ProductSearchResults.SearchResults)] = viewModel.ProductSearchResults.SearchResults;
            viewModel.Context[nameof(SearchConfiguration.MaxItemsPerPage)]          = SearchConfiguration.MaxItemsPerPage;
            viewModel.Context["ListName"] = "Search Query";

            return(viewModel);
        }
        public async Task <SearchQueryViewModel> GetSearchQueryViewModelAsync(GetSearchQueryViewModelParams param)
        {
            SearchQueryViewModel viewModel;

            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.Criteria == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria)), nameof(param));
            }

            var searchQueryProducts = await SearchQueryRepository.SearchQueryProductAsync(new SearchQueryProductParams
            {
                CultureName = param.CultureInfo.Name,
                QueryName   = param.QueryName,
                QueryType   = param.QueryType,
                ScopeId     = param.Scope,
                Criteria    = param.Criteria
            }).ConfigureAwait(false);

            var documents = searchQueryProducts.Result.Documents.Select(ToProductDocument).ToList();

            var inventoryLocations = await InventoryRepository.GetInventoryLocationStatusesBySkus(
                new GetInventoryLocationStatuseParam()
            {
                Skus    = documents.Select(d => d.Sku).ToList(),
                ScopeId = param.Scope,
                InventoryLocationIds = param.InventoryLocationIds
            }).ConfigureAwait(false);

            FixInventories(documents, inventoryLocations);
            documents = await FixInventoryFilter(documents, param.Scope).ConfigureAwait(false);

            var getImageParam = new GetProductMainImagesParam
            {
                ImageSize            = SearchConfiguration.DefaultImageSize,
                ProductImageRequests = documents
                                       .Select(document => new ProductImageRequest
                {
                    ProductId = document.ProductId,
                    Variant   = document.PropertyBag.ContainsKey(VariantPropertyBagKey)
                            ? new VariantKey {
                        Id = document.PropertyBag[VariantPropertyBagKey].ToString()
                    }
                            : VariantKey.Empty,
                    ProductDefinitionName = document.PropertyBag.ContainsKey("DefinitionName")
                            ? document.PropertyBag["DefinitionName"].ToString()
                            : string.Empty,
                    PropertyBag = document.PropertyBag
                }).ToList()
            };

            var imageUrls = await DamProvider.GetProductMainImagesAsync(getImageParam).ConfigureAwait(false);

            var newCriteria = param.Criteria.Clone();

            var createSearchViewModelParam = new CreateProductSearchResultsViewModelParam <SearchParam>
            {
                SearchParam = new SearchParam()
                {
                    Criteria = newCriteria
                },
                ImageUrls    = imageUrls,
                SearchResult = new ProductSearchResult()
                {
                    Documents  = documents,
                    TotalCount = searchQueryProducts.Result.TotalCount,
                    Facets     = searchQueryProducts.Result.Facets,
                },
            };

            if (param.QueryType != Overture.ServiceModel.SearchQueries.SearchQueryType.ProductSet &&
                param.Criteria.IncludeFacets &&
                param.Criteria.SelectedFacets != null &&
                param.Criteria.SelectedFacets.Any(s => s.Name?.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix) ?? false))
            {
                createSearchViewModelParam.CategoryFacetCountsResult = await SearchQueryRepository.GetCategoryFacetCountsAsync(param.Criteria, searchQueryProducts).ConfigureAwait(false);
            }

            viewModel = new SearchQueryViewModel
            {
                QueryName     = param.QueryName,
                QueryType     = param.QueryType,
                FacetSettings = new FacetSettingsViewModel()
                {
                    SelectedFacets =
                        await GetSelectedFacetsAsync(createSearchViewModelParam.SearchParam).ConfigureAwait(false),
                },
                ProductSearchResults =
                    await CreateProductSearchResultsViewModelAsync(createSearchViewModelParam).ConfigureAwait(false),
                ListName = "Search Query"
            };

            ProcessFacets(viewModel, searchQueryProducts);

            viewModel.FacetSettings.CategoryFacetValuesTree = await BuildCategoryFacetValuesTree(viewModel.ProductSearchResults.Facets,
                                                                                                 viewModel.FacetSettings.SelectedFacets,
                                                                                                 viewModel.ProductSearchResults.CategoryFacetCounts).ConfigureAwait(false);

            if (viewModel.FacetSettings.CategoryFacetValuesTree != null)
            {
                viewModel.FacetSettings.CategoryFacetValuesTree.TotalCount = createSearchViewModelParam.CategoryFacetCountsResult != null ? createSearchViewModelParam.CategoryFacetCountsResult.TotalCount : viewModel.ProductSearchResults.TotalCount;
                viewModel.FacetSettings.Context["CategoryFacetValuesTree"] = viewModel.FacetSettings.CategoryFacetValuesTree;
            }

            // Json context for Facets
            viewModel.FacetSettings.Context["SelectedFacets"]      = viewModel.FacetSettings.SelectedFacets;
            viewModel.FacetSettings.Context["Facets"]              = viewModel.ProductSearchResults.Facets.Where(f => !f.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix));
            viewModel.FacetSettings.Context["PromotedFacetValues"] = viewModel.ProductSearchResults.PromotedFacetValues;

            viewModel.Context[nameof(viewModel.ProductSearchResults.SearchResults)] = viewModel.ProductSearchResults.SearchResults;
            viewModel.Context[nameof(viewModel.MaxItemsPerPage)] = viewModel.MaxItemsPerPage;
            viewModel.Context["ListName"] = viewModel.ListName;

            return(viewModel);
        }