Skip to content

Commit

Permalink
Fix search errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarincev committed Feb 8, 2016
1 parent 629f741 commit 15bef34
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Text;
using System.Diagnostics;
using VirtoCommerce.Platform.Core.Common;

namespace VirtoCommerce.CatalogModule.Data.Repositories
{
Expand Down Expand Up @@ -99,7 +100,7 @@ protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelB

#region Association
modelBuilder.Entity<dataModel.Association>().ToTable("Association").HasKey(x => x.Id).Property(x => x.Id);
modelBuilder.Entity<dataModel.Association>().HasRequired(m => m.CatalogItem).WithMany(x => x.Assosiations).HasForeignKey(x=>x.ItemId).WillCascadeOnDelete(false);
modelBuilder.Entity<dataModel.Association>().HasRequired(m => m.CatalogItem).WithMany(x => x.Assosiations).HasForeignKey(x => x.ItemId).WillCascadeOnDelete(false);
modelBuilder.Entity<dataModel.Association>().HasRequired(m => m.AssociationGroup).WithMany(x => x.Associations).HasForeignKey(x => x.AssociationGroupId).WillCascadeOnDelete(true);
#endregion

Expand Down Expand Up @@ -228,15 +229,19 @@ public dataModel.Catalog GetCatalogById(string catalogId)

public string[] GetAllChildrenCategoriesIds(string[] categoryIds)
{
const string queryPattern =
@"WITH cte AS ( SELECT a.Id FROM Category a WHERE Id IN ({0})
var retVal = new List<string>();
if (!categoryIds.IsNullOrEmpty())
{
const string queryPattern =
@"WITH cte AS ( SELECT a.Id FROM Category a WHERE Id IN ({0})
UNION ALL
SELECT a.Id FROM Category a JOIN cte c ON a.ParentCategoryId = c.Id)
SELECT Id FROM cte WHERE Id NOT IN ({0})";

var query = String.Format(queryPattern, String.Join(", ", categoryIds.Select(x => String.Format("'{0}'", x))));
var retVal = ObjectContext.ExecuteStoreQuery<string>(query).ToArray();
return retVal;
var query = String.Format(queryPattern, String.Join(", ", categoryIds.Select(x => String.Format("'{0}'", x))));
retVal = ObjectContext.ExecuteStoreQuery<string>(query).ToList();
}
return retVal.ToArray();
}

public dataModel.Category[] GetCategoriesByIds(string[] categoriesIds, coreModel.CategoryResponseGroup respGroup)
Expand Down Expand Up @@ -287,11 +292,11 @@ public dataModel.Category[] GetCategoriesByIds(string[] categoriesIds, coreModel
//Need load inherited from parents categories and catalogs
var allParents = result.SelectMany(x => x.AllParents).ToArray();
var allCategoriesTreeIds = allParents.Select(x => x.Id).Concat(categoriesIds).Distinct().ToArray();
var allCatalogsIds = result.Select(x=>x.CatalogId).Concat(allParents.Select(x => x.CatalogId)).Distinct().ToArray();
var allCatalogsIds = result.Select(x => x.CatalogId).Concat(allParents.Select(x => x.CatalogId)).Distinct().ToArray();

var categoriesProperties = Properties.Include(x => x.PropertyAttributes)
.Include(x => x.DictionaryValues)
.Where(x=> allCategoriesTreeIds.Contains(x.CategoryId)).ToArray();
.Where(x => allCategoriesTreeIds.Contains(x.CategoryId)).ToArray();

var catalogProperties = Properties.Include(x => x.PropertyAttributes)
.Include(x => x.DictionaryValues)
Expand Down Expand Up @@ -319,7 +324,7 @@ public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGro
var catalogs = Catalogs.Include(x => x.CatalogLanguages).Where(x => catalogIds.Contains(x.Id)).ToArray();

//Load product categories separately
var categoryIds = retVal.Select(x => x.CategoryId).Where(x=>!String.IsNullOrEmpty(x)).Distinct().ToArray();
var categoryIds = retVal.Select(x => x.CategoryId).Where(x => !String.IsNullOrEmpty(x)).Distinct().ToArray();
var categories = GetCategoriesByIds(categoryIds, coreModel.CategoryResponseGroup.WithParents);

if ((respGroup & coreModel.ItemResponseGroup.Links) == coreModel.ItemResponseGroup.Links)
Expand Down Expand Up @@ -349,7 +354,7 @@ public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGro
{
var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray();
//For variations loads only info and images
var variations = Items.Include(x => x.Images).Include(x=>x.Assets).Where(x => variationIds.Contains(x.Id)).ToArray();
var variations = Items.Include(x => x.Images).Include(x => x.Assets).Where(x => variationIds.Contains(x.Id)).ToArray();
//load variations property values separately
var variationPropertyValues = PropertyValues.Where(x => variationIds.Contains(x.ItemId)).ToArray();
}
Expand Down Expand Up @@ -401,7 +406,7 @@ public dataModel.Property[] GetAllCatalogProperties(string catalogId)
{
//get all category relations
var linkedCategoryIds = CategoryLinks.Where(x => x.TargetCatalogId == catalogId)
.Select(x=>x.SourceCategoryId)
.Select(x => x.SourceCategoryId)
.Distinct()
.ToArray();
//linked product categories links
Expand All @@ -412,7 +417,7 @@ public dataModel.Property[] GetAllCatalogProperties(string catalogId)
.ToArray();
linkedCategoryIds = linkedCategoryIds.Concat(linkedProductCategoryIds).Distinct().ToArray();
var expandedFlatLinkedCategoryIds = linkedCategoryIds.Concat(GetAllChildrenCategoriesIds(linkedCategoryIds)).Distinct().ToArray();

propertyIds = propertyIds.Concat(Properties.Where(x => expandedFlatLinkedCategoryIds.Contains(x.CategoryId)).Select(x => x.Id)).Distinct().ToArray();
var linkedCatalogIds = Categories.Where(x => expandedFlatLinkedCategoryIds.Contains(x.Id)).Select(x => x.CatalogId).Distinct().ToArray();
propertyIds = propertyIds.Concat(Properties.Where(x => linkedCatalogIds.Contains(x.CatalogId) && x.CategoryId == null).Select(x => x.Id)).Distinct().ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace VirtoCommerce.CatalogModule.Data.Services
{
public class CatalogSearchServiceImpl : ICatalogSearchService
{
private readonly Func<ICatalogRepository> _catalogRepositoryFactory;
private readonly Func<ICatalogRepository> _catalogRepositoryFactory;
private readonly IItemService _itemService;
private readonly ICatalogService _catalogService;
private readonly ICategoryService _categoryService;
Expand All @@ -28,40 +28,40 @@ public CatalogSearchServiceImpl(Func<ICatalogRepository> catalogRepositoryFactor
_catalogService = catalogService;
_categoryService = categoryService;

_productSortingAliases["sku"] = ReflectionUtility.GetPropertyName<CatalogProduct>(x => x.Code);
_productSortingAliases["sku"] = ReflectionUtility.GetPropertyName<CatalogProduct>(x => x.Code);
_categorySortingAliases["sku"] = ReflectionUtility.GetPropertyName<Category>(x => x.Code);

}

public SearchResult Search(SearchCriteria criteria)
{
{
var retVal = new SearchResult();
var taskList = new List<Task>();
var taskList = new List<Task>();

if ((criteria.ResponseGroup & SearchResponseGroup.WithProducts) == SearchResponseGroup.WithProducts)
{
taskList.Add(Task.Factory.StartNew(() => SearchItems(criteria, retVal)));
}
{
taskList.Add(Task.Factory.StartNew(() => SearchItems(criteria, retVal)));
}

if ((criteria.ResponseGroup & SearchResponseGroup.WithCatalogs) == SearchResponseGroup.WithCatalogs)
{
taskList.Add(Task.Factory.StartNew(() => SearchCatalogs(criteria, retVal)));
}
{
taskList.Add(Task.Factory.StartNew(() => SearchCatalogs(criteria, retVal)));
}

if ((criteria.ResponseGroup & SearchResponseGroup.WithCategories) == SearchResponseGroup.WithCategories)
{
taskList.Add(Task.Factory.StartNew(() => SearchCategories(criteria, retVal)));
}
{
taskList.Add(Task.Factory.StartNew(() => SearchCategories(criteria, retVal)));
}

Task.WaitAll(taskList.ToArray());
Task.WaitAll(taskList.ToArray());

return retVal;
}
return retVal;
}

private void SearchCategories(SearchCriteria criteria, SearchResult result)
{
using (var repository = _catalogRepositoryFactory())
{
{
using (var repository = _catalogRepositoryFactory())
{
var query = repository.Categories.Where(x => criteria.WithHidden ? true : x.IsActive);

//Get list of search in categories
Expand Down Expand Up @@ -98,8 +98,11 @@ private void SearchCategories(SearchCriteria criteria, SearchResult result)
searchCategoryIds = searchCategoryIds.Concat(allCatalogLinkedCategories).Distinct().ToArray();
//Then exapand all categories, get all childrens
searchCategoryIds = searchCategoryIds.Concat(repository.GetAllChildrenCategoriesIds(searchCategoryIds)).ToArray();
//find all categories belong searched catalogs and all categories direct or implicitly linked to catalogs
query = query.Where(x => searchCategoryIds.Contains(x.Id));
if (!searchCategoryIds.IsNullOrEmpty())
{
//find all categories belong searched catalogs and all categories direct or implicitly linked to catalogs
query = query.Where(x => searchCategoryIds.Contains(x.Id));
}
}
else
{
Expand Down Expand Up @@ -137,9 +140,9 @@ private void SearchCategories(SearchCriteria criteria, SearchResult result)
.AsQueryable()
.OrderBySortInfos(sortInfos)
.ToList();
}
}

}
}

private void SearchCatalogs(SearchCriteria criteria, SearchResult result)
{
Expand Down Expand Up @@ -193,29 +196,29 @@ private void SearchItems(SearchCriteria criteria, SearchResult result)

var query = repository.Items.Where(x => criteria.WithHidden ? true : x.IsActive);

if(!criteria.SearchInVariations)
if (!criteria.SearchInVariations)
{
query = query.Where(x => x.ParentId == null);
}

if (!searchCategoryIds.IsNullOrEmpty())
{
query = query.Where(x => searchCategoryIds.Contains(x.CategoryId) || x.CategoryLinks.Any(c => searchCategoryIds.Contains(c.CategoryId)));
query = query.Where(x => searchCategoryIds.Contains(x.CategoryId) || x.CategoryLinks.Any(c => searchCategoryIds.Contains(c.CategoryId)));
}
else if (!criteria.CatalogIds.IsNullOrEmpty())
{
query = query.Where(x => criteria.CatalogIds.Contains(x.CatalogId) && (criteria.SearchInChildren || x.CategoryId == null));
}
else if (!criteria.CatalogIds.IsNullOrEmpty())
{
query = query.Where(x => criteria.CatalogIds.Contains(x.CatalogId) && (criteria.SearchInChildren || x.CategoryId == null));
}

if (!string.IsNullOrEmpty(criteria.Code))
{
query = query.Where(x => x.Code == criteria.Code);
}
{
query = query.Where(x => x.Code == criteria.Code);
}
else if (!string.IsNullOrEmpty(criteria.Keyword))
{
{
query = query.Where(x => x.Name.Contains(criteria.Keyword) || x.Code.Contains(criteria.Keyword) || x.ItemPropertyValues.Any(y => y.ShortTextValue == criteria.Keyword));
}


//Filter by property dictionary values
if (!criteria.PropertyValues.IsNullOrEmpty())
Expand All @@ -224,12 +227,12 @@ private void SearchItems(SearchCriteria criteria, SearchResult result)
query = query.Where(x => x.ItemPropertyValues.Any(y => propValueIds.Contains(y.KeyValue)));
}

if(!criteria.ProductTypes.IsNullOrEmpty())
if (!criteria.ProductTypes.IsNullOrEmpty())
{
query = query.Where(x => criteria.ProductTypes.Contains(x.ProductType));
}

if(criteria.OnlyBuyable != null)
if (criteria.OnlyBuyable != null)
{
query = query.Where(x => x.IsBuyable == criteria.OnlyBuyable);
}
Expand All @@ -240,7 +243,7 @@ private void SearchItems(SearchCriteria criteria, SearchResult result)
}

result.ProductsTotalCount = query.Count();

var sortInfos = criteria.SortInfos;
if (sortInfos.IsNullOrEmpty())
{
Expand All @@ -249,7 +252,7 @@ private void SearchItems(SearchCriteria criteria, SearchResult result)
//Try to replace sorting columns names
TryTransformSortingInfoColumnNames(_productSortingAliases, sortInfos);

query = query.OrderBySortInfos(sortInfos);
query = query.OrderBySortInfos(sortInfos);

var itemIds = query.Skip(criteria.Skip)
.Take(criteria.Take)
Expand All @@ -259,16 +262,16 @@ private void SearchItems(SearchCriteria criteria, SearchResult result)
var productResponseGroup = ItemResponseGroup.ItemInfo | ItemResponseGroup.ItemAssets | ItemResponseGroup.Links | ItemResponseGroup.Seo;

if ((criteria.ResponseGroup & SearchResponseGroup.WithProperties) == SearchResponseGroup.WithProperties)
{
{
productResponseGroup |= ItemResponseGroup.ItemProperties;
}
}

if ((criteria.ResponseGroup & SearchResponseGroup.WithVariations) == SearchResponseGroup.WithVariations)
{
{
productResponseGroup |= ItemResponseGroup.Variations;
}
}

var products = _itemService.GetByIds(itemIds, productResponseGroup);
var products = _itemService.GetByIds(itemIds, productResponseGroup);
result.Products = products.AsQueryable().OrderBySortInfos(sortInfos).ToList();
}

Expand Down

0 comments on commit 15bef34

Please sign in to comment.