mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Pulling search result page selection back into the service and cleaning up a few other bits
--HG-- branch : dev
This commit is contained in:
@@ -24,18 +24,19 @@ namespace Orchard.Search.Controllers {
|
||||
PageSize = take
|
||||
};
|
||||
|
||||
var results = _searchService.Query(q);
|
||||
var results = _searchService.Query(q, skip, take);
|
||||
|
||||
searchViewModel.Count = results.Count();
|
||||
if (results == null)
|
||||
return View(searchViewModel);
|
||||
|
||||
searchViewModel.Count = results.TotalCount;
|
||||
searchViewModel.TotalPageCount = (int)Math.Ceiling((decimal)searchViewModel.Count/searchViewModel.PageSize);
|
||||
//todo: deal with page requests beyond result count
|
||||
searchViewModel.ResultsPage = results
|
||||
searchViewModel.ResultsPage = results.Page
|
||||
.Select(result => new SearchResultViewModel {
|
||||
Content = _contentManager.BuildDisplayModel(_contentManager.Get(result.Id), "SummaryForSearch"),
|
||||
SearchHit = result
|
||||
})
|
||||
.Skip(skip)
|
||||
.Take(take)
|
||||
.ToList();
|
||||
|
||||
return View(searchViewModel);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Indexing;
|
||||
|
||||
namespace Orchard.Search.Models {
|
||||
public interface ISearchResult {
|
||||
IEnumerable<ISearchHit> Page { get; set; }
|
||||
int TotalCount { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Indexing;
|
||||
|
||||
namespace Orchard.Search.Models {
|
||||
public class SearchResult : ISearchResult {
|
||||
public IEnumerable<ISearchHit> Page { get; set; }
|
||||
public int TotalCount { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,9 @@
|
||||
<Compile Include="Filters\SearchFilter.cs" />
|
||||
<Compile Include="Permissions.cs" />
|
||||
<Compile Include="Routes.cs" />
|
||||
<Compile Include="Models\ISearchResult.cs" />
|
||||
<Compile Include="Services\ISearchService.cs" />
|
||||
<Compile Include="Models\SearchResult.cs" />
|
||||
<Compile Include="Services\SearchService.cs" />
|
||||
<Compile Include="ViewModels\SearchIndexViewModel.cs" />
|
||||
<Compile Include="ViewModels\SearchResultViewModel.cs" />
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Search.Models;
|
||||
|
||||
namespace Orchard.Search.Services {
|
||||
public interface ISearchService : IDependency {
|
||||
bool HasIndexToManage { get; }
|
||||
IEnumerable<ISearchHit> Query(string term);
|
||||
ISearchResult Query(string query, int skip, int? take);
|
||||
void RebuildIndex();
|
||||
void UpdateIndex();
|
||||
DateTime GetIndexUpdatedUtc();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Search.Models;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Search.Services
|
||||
@@ -27,17 +27,26 @@ namespace Orchard.Search.Services
|
||||
get { return _indexManager.HasIndexProvider(); }
|
||||
}
|
||||
|
||||
public IEnumerable<ISearchHit> Query(string term) {
|
||||
if (string.IsNullOrWhiteSpace(term) || !_indexManager.HasIndexProvider())
|
||||
return Enumerable.Empty<ISearchHit>();
|
||||
ISearchResult ISearchService.Query(string query, int skip, int? take) {
|
||||
if (string.IsNullOrWhiteSpace(query) || !_indexManager.HasIndexProvider())
|
||||
return null;
|
||||
|
||||
return _indexManager.GetSearchIndexProvider().CreateSearchBuilder(SearchIndexName)
|
||||
.WithField("title", term)
|
||||
.WithField("body", term)
|
||||
.Search();
|
||||
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(SearchIndexName)
|
||||
.WithField("title", query)
|
||||
.WithField("body", query);
|
||||
|
||||
var totalCount = searchBuilder.Count();
|
||||
if (take != null)
|
||||
searchBuilder = searchBuilder
|
||||
.Slice(skip, (int)take);
|
||||
|
||||
return new SearchResult {
|
||||
Page = searchBuilder.Search(),
|
||||
TotalCount = totalCount
|
||||
};
|
||||
}
|
||||
|
||||
public void RebuildIndex() {
|
||||
void ISearchService.RebuildIndex() {
|
||||
if (!_indexManager.HasIndexProvider()) {
|
||||
Services.Notifier.Warning(T("There is no search index to rebuild."));
|
||||
return;
|
||||
@@ -51,7 +60,7 @@ namespace Orchard.Search.Services
|
||||
Services.Notifier.Information(T("The search index has been rebuilt."));
|
||||
}
|
||||
|
||||
public void UpdateIndex() {
|
||||
void ISearchService.UpdateIndex() {
|
||||
|
||||
foreach(var handler in _indexNotifierHandlers) {
|
||||
handler.UpdateIndex(SearchIndexName);
|
||||
@@ -60,12 +69,10 @@ namespace Orchard.Search.Services
|
||||
Services.Notifier.Information(T("The search index has been updated."));
|
||||
}
|
||||
|
||||
public DateTime GetIndexUpdatedUtc() {
|
||||
if(!HasIndexToManage) {
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
|
||||
return _indexManager.GetSearchIndexProvider().GetLastIndexUtc(SearchIndexName);
|
||||
DateTime ISearchService.GetIndexUpdatedUtc() {
|
||||
return !HasIndexToManage
|
||||
? DateTime.MinValue
|
||||
: _indexManager.GetSearchIndexProvider().GetLastIndexUtc(SearchIndexName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,15 @@
|
||||
Html.RegisterStyle("search.css"); %>
|
||||
<h1><%=Html.TitleForPage(T("Search").Text)%></h1><%
|
||||
Html.Zone("search");
|
||||
if (Model.Count == 0) { %>
|
||||
<p class="search-summary"><%=T("<em>zero</em> results")%></p><%
|
||||
if (!string.IsNullOrWhiteSpace(Model.Query)) {
|
||||
if (Model.Count == 0) { %>
|
||||
<p class="search-summary"><%=T("<em>zero</em> results") %></p><%
|
||||
}
|
||||
else { %>
|
||||
<p class="search-summary"><%=T("<em>{0} - {1}</em> of <em>{2}</em> results", (Model.Page - 1) * Model.PageSize + 1, Model.Page * Model.PageSize > Model.Count ? Model.Count : Model.Page * Model.PageSize, Model.Count)%></p><%
|
||||
}
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(Model.Query)) { %>
|
||||
<p class="search-summary"><%=T("<em>{0} - {1}</em> of <em>{2}</em> results", (Model.Page - 1) * Model.PageSize + 1, Model.Page * Model.PageSize > Model.Count ? Model.Count : Model.Page * Model.PageSize, Model.Count)%></p><%
|
||||
}
|
||||
if (Model.ResultsPage.Count() > 0) { %>
|
||||
if (Model.ResultsPage != null && Model.ResultsPage.Count() > 0) { %>
|
||||
<%=Html.UnorderedList(Model.ResultsPage, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString(), "search-results contentItems") %>
|
||||
<%=Html.Pager(Model.TotalPageCount, Model.Page, new {q = Model.Query}, "<", ">", false) %><%
|
||||
<%=Html.Pager(Model.TotalPageCount, Model.Page, new {q = Model.Query}) %><%
|
||||
} %>
|
||||
@@ -42,7 +42,7 @@ namespace Orchard.Mvc.Html {
|
||||
}
|
||||
#region Pager
|
||||
|
||||
public static string Pager(this HtmlHelper html, int pageCount, int currentPage, object values, string previousText, string nextText, bool alwaysShowPreviousAndNext) {
|
||||
public static string Pager(this HtmlHelper html, int pageCount, int currentPage, object values = null, string previousText = "<", string nextText = ">", bool alwaysShowPreviousAndNext = false) {
|
||||
if (pageCount < 2)
|
||||
return "";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user