Beispiel #1
0
        public JObject GetProducts(ProductSearchData productSearchData)
        {
            // If we do not get any facets as part of query, we use the default facets from the registry
            var facetRegistry = ServiceLocator.Current.GetInstance <FacetRegistry>();

            if (productSearchData.ProductData.Facets == null || productSearchData.ProductData.Facets.Any() == false)
            {
                productSearchData.ProductData.Facets = new List <FacetValues>();
                foreach (FacetDefinition definition in facetRegistry.FacetDefinitions)
                {
                    var valuesForFacet = new FacetValues()
                    {
                        Definition = definition
                    };
                    productSearchData.ProductData.Facets.Add(valuesForFacet);
                }
            }

            // The language is part of the route
            string language = Language;

            // search term, used if part of freetext search
            var searchTerm = productSearchData.ProductData.SearchTerm;
            SearchResults <FindProduct> productsSearchResult = GetProductsForSearchAndFilters(productSearchData, searchTerm, language);

            string selectedFacetName   = productSearchData.ProductData.SelectedFacetName ?? string.Empty;
            var    productFacetsResult = GetFacetResult(productSearchData, language, productSearchData.ProductData.Facets, searchTerm);

            // parse facet results
            IEnumerable <TermCount> productCategoryFacetsResult = productFacetsResult.TermsFacetFor(x => x.ParentCategoryName).Terms;
            List <FacetViewModel>   allProductCategoryFacets    = CreateCategoryFacetViewModels(productCategoryFacetsResult,
                                                                                                productSearchData.ProductData.SelectedProductCategories.Select(x => x.ToString()).ToList());

            // Get all facet values based on facet registry
            var facetsAndValues = GetFacetsAndValues(productFacetsResult, productSearchData.ProductData.Facets);

            // If we're doing a filtering on a specific facet, we handle that one
            // in a special way, in order to show the count for all terms for the facet
            SearchResults <FindProduct> productSelectedFacetsResult = null;

            if (string.IsNullOrEmpty(selectedFacetName) == false)
            {
                List <FacetValues> selectedFacet = new List <FacetValues>();
                if (productSearchData.ProductData.Facets != null)
                {
                    FacetValues selectedValues = productSearchData.ProductData.Facets.Find(f => f.Definition.Name.Equals(selectedFacetName));
                    if (selectedValues != null)
                    {
                        selectedFacet.Add(selectedValues);
                    }
                }

                // Get facet values and counts for the selected facet
                productSelectedFacetsResult = GetFacetResult(
                    productSearchData,
                    language,
                    selectedFacet,
                    searchTerm, applyFacetFilters: false);

                // Treat the selected faced specially, as it might show more data if it is selected
                facetsAndValues = GetFacetsAndValues(productSelectedFacetsResult, facetsAndValues);
            }

            var result = new
            {
                products = productsSearchResult.ToList(),
                productCategoryFacets = allProductCategoryFacets,
                facets      = facetsAndValues,
                totalResult = productsSearchResult.TotalMatching
            };

            var serializer = new JsonSerializer();

            serializer.TypeNameHandling = TypeNameHandling.Auto;

            return(JObject.FromObject(result, serializer));
        }
        public JObject GetProducts(ProductSearchData productSearchData)
        {
            // If we do not get any facets as part of query, we use the default facets from the registry
            var facetRegistry = ServiceLocator.Current.GetInstance<FacetRegistry>();
            if (productSearchData.ProductData.Facets == null || productSearchData.ProductData.Facets.Any() == false)
            {
                productSearchData.ProductData.Facets = new List<FacetValues>();
                foreach (FacetDefinition definition in facetRegistry.FacetDefinitions)
                {
                    var valuesForFacet = new FacetValues()
                    {
                        Definition = definition
                    };
                    productSearchData.ProductData.Facets.Add(valuesForFacet);
                }
            }

            // The language is part of the route
            string language = Language;

            // search term, used if part of freetext search
            var searchTerm = productSearchData.ProductData.SearchTerm;
            SearchResults<FindProduct> productsSearchResult = GetProductsForSearchAndFilters(productSearchData, searchTerm, language);

            string selectedFacetName = productSearchData.ProductData.SelectedFacetName ?? string.Empty;
            var productFacetsResult = GetFacetResult(productSearchData, language, productSearchData.ProductData.Facets, searchTerm);

            // parse facet results
            IEnumerable<TermCount> productCategoryFacetsResult = productFacetsResult.TermsFacetFor(x => x.ParentCategoryName).Terms;
            List<FacetViewModel> allProductCategoryFacets = CreateCategoryFacetViewModels(productCategoryFacetsResult,
                productSearchData.ProductData.SelectedProductCategories.Select(x => x.ToString()).ToList());

            // Get all facet values based on facet registry
            var facetsAndValues = GetFacetsAndValues(productFacetsResult, productSearchData.ProductData.Facets);

            // If we're doing a filtering on a specific facet, we handle that one
            // in a special way, in order to show the count for all terms for the facet
            SearchResults<FindProduct> productSelectedFacetsResult = null;
            if (string.IsNullOrEmpty(selectedFacetName) == false)
            {
                List<FacetValues> selectedFacet = new List<FacetValues>();
                if (productSearchData.ProductData.Facets != null)
                {
                    FacetValues selectedValues = productSearchData.ProductData.Facets.Find(f => f.Definition.Name.Equals(selectedFacetName));
                    if (selectedValues != null)
                        selectedFacet.Add(selectedValues);
                }

                // Get facet values and counts for the selected facet
                productSelectedFacetsResult = GetFacetResult(
                    productSearchData,
                    language,
                    selectedFacet,
                    searchTerm, applyFacetFilters: false);

                // Treat the selected faced specially, as it might show more data if it is selected
                facetsAndValues = GetFacetsAndValues(productSelectedFacetsResult, facetsAndValues);
            }

            var result = new
            {
                products = productsSearchResult.ToList(),
                productCategoryFacets = allProductCategoryFacets,
                facets = facetsAndValues,
                totalResult = productsSearchResult.TotalMatching
            };

            var serializer = new JsonSerializer();
            serializer.TypeNameHandling = TypeNameHandling.Auto;

            return JObject.FromObject(result, serializer);
        }