//TODO パフォーマンス改善 public static List<SearchResultArticle> getUserArticles(ApplicationDbContext context, int order, int skip, string userId, out int count, int takeCount = 10,string exceptId="") { ArticleThumbnailManager thumbnailManager=new ArticleThumbnailManager(new BlobStorageConnection()); IQueryable<ArticleModel> query = String.IsNullOrWhiteSpace(exceptId) ? context.Articles.Where(f => f.AuthorID.Equals(userId)) : context.Articles.Where(f => f.AuthorID.Equals(userId) && !f.ArticleModelId.Equals(exceptId)); count = query.Count(); query = ChangeOrder(order, query); query = query.Skip(skip); List<SearchResultArticle> articles = new List<SearchResultArticle>(); foreach (var source in query.Take(takeCount)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag=thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } return articles; }
public static List<SearchResultArticle> getRelatedArticles(ApplicationDbContext context, int order, int skip, ArticleModel article, int count) { ArticleThumbnailManager thumbnailManager = new ArticleThumbnailManager(new BlobStorageConnection()); //直接の関係の記事を探す IQueryable<ArticleModel> query = context.Articles.Where(f => f.RelatedArticleId.Equals(article.ArticleModelId)); query = ChangeOrder(order, query); query = query.Skip(skip); List<SearchResultArticle> articles = new List<SearchResultArticle>(); foreach (var source in query.Take(count)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag = thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } int remain = count - article.LabelCount; if(article.LabelCount<count) {//取り出すカウントに満たない場合は間接的関連記事でうめる context.Articles.Where(f => f.ThemeId.Equals(article.ThemeId)&&!f.RelatedArticleId.Equals(article.ArticleModelId)); query = ChangeOrder(order, query); query = query.Skip(skip); foreach (var source in query.Take(remain)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag = thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } } return articles; }
public async Task<ActionResult> Search(string searchText,int skip=0,int order=0) { if(searchText==null)return View(new SearchResultViewModel() {Articles = new SearchResultArticle[0],Order = order,Skip = skip}); string[] queries=searchText.Split(' '); var context = Request.GetOwinContext().Get<ApplicationDbContext>(); string first = queries[0]; var result=context.Articles.Where((f) => f.Title.Contains(first)&&!f.IsDraft); for (int index = 1; index < Math.Min(4,queries.Length); index++) { var query = queries[index]; result=result.Where(f => f.Title.Contains(query)); } int count = await result.CountAsync(); result = ChangeOrder(order, result); result = result.Skip(skip); SearchResultViewModel vm=new SearchResultViewModel(); ArticleThumbnailManager thumbnailManager=new ArticleThumbnailManager(new BlobStorageConnection()); List<SearchResultArticle> articles=new List<SearchResultArticle>(); foreach (var source in result.Take(20)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag = thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } vm.Articles = articles.ToArray(); if (vm.Articles.Length == 0) { vm.SearchResultText = string.Format("「{0}」に関する検索結果は見つかりませんでした。", searchText); } else { vm.SearchResultText = string.Format("「{0}」に関する検索結果:{1}件中{2}~{3}件", searchText,count,skip+1,Math.Min(skip+21,count)); } vm.SearchText = searchText; vm.Order = order; vm.Skip = skip; vm.Count = count; return View(vm); }