From 089f488c15a25bbe60412a945b0f224622e2b865 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 2 Nov 2017 13:55:19 +0000 Subject: [PATCH] Adds overloads so you can do paging with the TypedSearch methods. --- .../ITypedPublishedContentQuery.cs | 20 +++++ src/Umbraco.Web/PublishedContentQuery.cs | 85 +++++++++++++++---- src/Umbraco.Web/UmbracoHelper.cs | 73 +++++++++++----- 3 files changed, 140 insertions(+), 38 deletions(-) diff --git a/src/Umbraco.Web/ITypedPublishedContentQuery.cs b/src/Umbraco.Web/ITypedPublishedContentQuery.cs index 893c036958..0ad7302ce2 100644 --- a/src/Umbraco.Web/ITypedPublishedContentQuery.cs +++ b/src/Umbraco.Web/ITypedPublishedContentQuery.cs @@ -50,6 +50,18 @@ namespace Umbraco.Web /// IEnumerable TypedSearch(string term, bool useWildCards = true, string searchProvider = null); + /// + /// Searches content + /// + /// + /// + /// + /// + /// + /// + /// + IEnumerable TypedSearch(int skip, int take, out int totalRecords, string term, bool useWildCards = true, string searchProvider = null); + /// /// Searhes content /// @@ -57,5 +69,13 @@ namespace Umbraco.Web /// /// IEnumerable TypedSearch(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null); + + /// + /// Searhes content + /// + /// + /// + /// + IEnumerable TypedSearch(int skip, int take, out int totalrecords, Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null); } } \ No newline at end of file diff --git a/src/Umbraco.Web/PublishedContentQuery.cs b/src/Umbraco.Web/PublishedContentQuery.cs index f0f2461ad8..49d38e17b2 100644 --- a/src/Umbraco.Web/PublishedContentQuery.cs +++ b/src/Umbraco.Web/PublishedContentQuery.cs @@ -383,6 +383,38 @@ namespace Umbraco.Web : _dynamicContentQuery.Search(criteria, searchProvider); } + /// + /// Searches content + /// + /// + /// + /// + /// + /// + /// + /// + public IEnumerable TypedSearch(int skip, int take, out int totalRecords, string term, bool useWildCards = true, string searchProvider = null) + { + if (_typedContentQuery != null) return _typedContentQuery.TypedSearch(skip, take, out totalRecords, term, useWildCards, searchProvider); + + var searcher = Examine.ExamineManager.Instance.DefaultSearchProvider; + if (string.IsNullOrEmpty(searchProvider) == false) + searcher = Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider]; + + var results = searcher.Search(term, useWildCards); + + totalRecords = results.TotalItemCount; + + var records = results.Skip(skip); + + if (take > 0) + { + records = records.Take(take); + } + + return records.ConvertSearchResultToPublishedContent(_contentCache); + } + /// /// Searches content /// @@ -391,16 +423,42 @@ namespace Umbraco.Web /// /// public IEnumerable TypedSearch(string term, bool useWildCards = true, string searchProvider = null) - { - if (_typedContentQuery != null) return _typedContentQuery.TypedSearch(term, useWildCards, searchProvider); + { + var total = 0; - var searcher = Examine.ExamineManager.Instance.DefaultSearchProvider; - if (string.IsNullOrEmpty(searchProvider) == false) - searcher = Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider]; - - var results = searcher.Search(term, useWildCards); - return results.ConvertSearchResultToPublishedContent(_contentCache); + return TypedSearch(0, 0, out total, term, useWildCards, searchProvider); } + + /// + /// Searhes content + /// + /// + /// + /// + /// + /// + /// + public IEnumerable TypedSearch(int skip, int take, out int totalRecords, Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null) + { + if (_typedContentQuery != null) return _typedContentQuery.TypedSearch(skip, take, out totalRecords, criteria, searchProvider); + + var s = Examine.ExamineManager.Instance.DefaultSearchProvider; + if (searchProvider != null) + s = searchProvider; + + var results = s.Search(criteria); + + totalRecords = results.TotalItemCount; + + var records = results.Skip(skip); + + if (take > 0) + { + records = records.Take(take); + } + + return records.ConvertSearchResultToPublishedContent(_contentCache); + } /// /// Searhes content @@ -410,14 +468,9 @@ namespace Umbraco.Web /// public IEnumerable TypedSearch(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null) { - if (_typedContentQuery != null) return _typedContentQuery.TypedSearch(criteria, searchProvider); - - var s = Examine.ExamineManager.Instance.DefaultSearchProvider; - if (searchProvider != null) - s = searchProvider; - - var results = s.Search(criteria); - return results.ConvertSearchResultToPublishedContent(_contentCache); + var total = 0; + + return TypedSearch(0, 0, out total, criteria, searchProvider); } #endregion diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index d94476b31c..90bc0a436b 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -1251,24 +1251,53 @@ namespace Umbraco.Web public IEnumerable TypedSearch(string term, bool useWildCards = true, string searchProvider = null) { return ContentQuery.TypedSearch(term, useWildCards, searchProvider); - } - - /// - /// Searhes content - /// - /// + } + + /// + /// Searches content + /// + /// + /// + /// + /// + /// /// /// - public IEnumerable TypedSearch(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null) + public IEnumerable TypedSearch(int skip, int take, out int totalRecords, string term, bool useWildCards = true, string searchProvider = null) + { + return ContentQuery.TypedSearch(skip, take, out totalRecords, term, useWildCards, searchProvider); + } + + /// + /// Searhes content + /// + /// + /// + /// + /// + /// + /// + public IEnumerable TypedSearch(int skip, int take, out int totalRecords, Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null) { - return ContentQuery.TypedSearch(criteria, searchProvider); - } - - #endregion - - #region Xml - - public dynamic ToDynamicXml(string xml) + return ContentQuery.TypedSearch(skip, take, out totalRecords, criteria, searchProvider); + } + + /// + /// Searhes content + /// + /// + /// + /// + public IEnumerable TypedSearch(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null) + { + return ContentQuery.TypedSearch(criteria, searchProvider); + } + + #endregion + + #region Xml + + public dynamic ToDynamicXml(string xml) { if (string.IsNullOrWhiteSpace(xml)) return null; var xElement = XElement.Parse(xml); @@ -1439,8 +1468,8 @@ namespace Umbraco.Web /// public IHtmlString Truncate(string html, int length, bool addElipsis, bool treatTagsAsContent) { - return _stringUtilities.Truncate(html, length, addElipsis, treatTagsAsContent); - } + return _stringUtilities.Truncate(html, length, addElipsis, treatTagsAsContent); + } #region Truncate by Words /// @@ -1451,7 +1480,7 @@ namespace Umbraco.Web int length = _stringUtilities.WordsToLength(html, words); return Truncate(html, length, true, false); - } + } /// /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them @@ -1481,12 +1510,12 @@ namespace Umbraco.Web int length = _stringUtilities.WordsToLength(html.ToHtmlString(), words); return Truncate(html, length, addElipsis, false); - } - #endregion + } #endregion - + #endregion + #region If - + /// /// If the test is true, the string valueIfTrue will be returned, otherwise the valueIfFalse will be returned. ///