mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Search bug: filter out non existent content items from index
There is latency between the content of the index and the content items in the database. We need to filter out content items returned from the indexing service that aren't published in the database. --HG-- branch : dev
This commit is contained in:
@@ -7,6 +7,8 @@ using Orchard.Settings;
|
||||
using Orchard.Search.Models;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Collections;
|
||||
|
||||
namespace Orchard.Search.Controllers {
|
||||
[ValidateInput(false)]
|
||||
@@ -24,16 +26,37 @@ namespace Orchard.Search.Controllers {
|
||||
public ActionResult Index(string q, int page = 1, int pageSize = 10) {
|
||||
var searchFields = CurrentSite.As<SearchSettingsPart>().Record.SearchedFields.Split(new[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var searchHits = _searchService.Query(q, page, pageSize,
|
||||
CurrentSite.As<SearchSettingsPart>().Record.FilterCulture,
|
||||
searchFields,
|
||||
searchHit => searchHit);
|
||||
|
||||
var searchResultViewModels = new List<SearchResultViewModel>();
|
||||
|
||||
foreach(var searchHit in searchHits) {
|
||||
var contentItem = _contentManager.Get(searchHit.ContentItemId);
|
||||
// ignore search results which content item has been removed or unpublished
|
||||
if(contentItem == null){
|
||||
searchHits.TotalItemCount--;
|
||||
continue;
|
||||
}
|
||||
|
||||
searchResultViewModels.Add(new SearchResultViewModel {
|
||||
Content = _contentManager.BuildDisplayModel(contentItem, "SummaryForSearch"),
|
||||
SearchHit = searchHit
|
||||
});
|
||||
}
|
||||
|
||||
var pageOfItems = new PageOfItems<SearchResultViewModel>(searchResultViewModels) {
|
||||
PageNumber = page,
|
||||
PageSize = searchHits.PageSize,
|
||||
TotalItemCount = searchHits.TotalItemCount
|
||||
};
|
||||
|
||||
var searchViewModel = new SearchViewModel {
|
||||
Query = q,
|
||||
DefaultPageSize = 10, // <- yeah, I know :|
|
||||
PageOfResults = _searchService.Query(q, page, pageSize,
|
||||
CurrentSite.As<SearchSettingsPart>().Record.FilterCulture,
|
||||
searchFields,
|
||||
searchHit => new SearchResultViewModel {
|
||||
Content = _contentManager.BuildDisplayModel(_contentManager.Get(searchHit.ContentItemId), "SummaryForSearch"),
|
||||
SearchHit = searchHit
|
||||
})
|
||||
PageOfResults = pageOfItems
|
||||
};
|
||||
|
||||
//todo: deal with page requests beyond result count
|
||||
|
@@ -51,8 +51,9 @@ namespace Orchard.Search.Services {
|
||||
searchBuilder = searchBuilder
|
||||
.Slice((page > 0 ? page - 1 : 0) * (int)pageSize, (int)pageSize);
|
||||
|
||||
var searchResults = searchBuilder.Search();
|
||||
|
||||
var pageOfItems = new PageOfItems<T>(searchBuilder.Search().Select(shapeResult)) {
|
||||
var pageOfItems = new PageOfItems<T>(searchResults.Select(shapeResult)) {
|
||||
PageNumber = page,
|
||||
PageSize = pageSize != null ? (int)pageSize : totalCount,
|
||||
TotalItemCount = totalCount
|
||||
|
Reference in New Issue
Block a user