2019-01-07 23:49:29 +11:00
|
|
|
using System.Collections;
|
2021-09-15 13:40:08 +02:00
|
|
|
using System.Globalization;
|
2018-06-29 19:52:40 +02:00
|
|
|
using System.Xml.XPath;
|
|
|
|
|
using Examine;
|
2018-12-17 12:17:03 +11:00
|
|
|
using Examine.Search;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
|
|
|
using Umbraco.Cms.Core.PublishedCache;
|
|
|
|
|
using Umbraco.Cms.Core.Xml;
|
2021-02-12 10:57:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Examine;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
namespace Umbraco.Cms.Infrastructure;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A class used to query for published content, media items
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <seealso cref="Umbraco.Cms.Core.IPublishedContentQuery" />
|
|
|
|
|
public class PublishedContentQuery : IPublishedContentQuery
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
private readonly IExamineManager _examineManager;
|
|
|
|
|
private readonly IPublishedSnapshot _publishedSnapshot;
|
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
2022-07-13 11:14:31 +02:00
|
|
|
private static readonly HashSet<string> _returnedQueryFields =
|
2022-06-23 00:36:37 +02:00
|
|
|
new() { ExamineFieldNames.ItemIdFieldName, ExamineFieldNames.CategoryFieldName };
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
/// <summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
/// Initializes a new instance of the <see cref="PublishedContentQuery" /> class.
|
2018-06-29 19:52:40 +02:00
|
|
|
/// </summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
public PublishedContentQuery(IPublishedSnapshot publishedSnapshot,
|
|
|
|
|
IVariationContextAccessor variationContextAccessor, IExamineManager examineManager)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
_publishedSnapshot = publishedSnapshot ?? throw new ArgumentNullException(nameof(publishedSnapshot));
|
|
|
|
|
_variationContextAccessor = variationContextAccessor ??
|
|
|
|
|
throw new ArgumentNullException(nameof(variationContextAccessor));
|
|
|
|
|
_examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#region Convert Helpers
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static bool ConvertIdObjectToInt(object id, out int intId)
|
|
|
|
|
{
|
|
|
|
|
switch (id)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
case string s:
|
|
|
|
|
return int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out intId);
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
case int i:
|
|
|
|
|
intId = i;
|
|
|
|
|
return true;
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
default:
|
|
|
|
|
intId = default;
|
|
|
|
|
return false;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static bool ConvertIdObjectToGuid(object id, out Guid guidId)
|
|
|
|
|
{
|
|
|
|
|
switch (id)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
case string s:
|
|
|
|
|
return Guid.TryParse(s, out guidId);
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
case Guid g:
|
|
|
|
|
guidId = g;
|
|
|
|
|
return true;
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
default:
|
|
|
|
|
guidId = default;
|
|
|
|
|
return false;
|
2020-03-03 11:59:17 +01:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static bool ConvertIdObjectToUdi(object id, out Udi? guidId)
|
|
|
|
|
{
|
|
|
|
|
switch (id)
|
2020-03-03 11:59:17 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
case string s:
|
|
|
|
|
return UdiParser.TryParse(s, out guidId);
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
case Udi u:
|
|
|
|
|
guidId = u;
|
|
|
|
|
return true;
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
default:
|
|
|
|
|
guidId = default;
|
|
|
|
|
return false;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#endregion
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#region Content
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Content(int id)
|
|
|
|
|
=> ItemById(id, _publishedSnapshot.Content);
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Content(Guid id)
|
|
|
|
|
=> ItemById(id, _publishedSnapshot.Content);
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Content(Udi? id)
|
|
|
|
|
{
|
|
|
|
|
if (!(id is GuidUdi udi))
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ItemById(udi.Guid, _publishedSnapshot.Content);
|
|
|
|
|
}
|
2022-03-16 14:39:28 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Content(object id)
|
|
|
|
|
{
|
|
|
|
|
if (ConvertIdObjectToInt(id, out var intId))
|
|
|
|
|
{
|
|
|
|
|
return Content(intId);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (ConvertIdObjectToGuid(id, out Guid guidId))
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
return Content(guidId);
|
|
|
|
|
}
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (ConvertIdObjectToUdi(id, out Udi? udiId))
|
|
|
|
|
{
|
|
|
|
|
return Content(udiId);
|
|
|
|
|
}
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2023-07-07 11:12:04 +02:00
|
|
|
[Obsolete("The current implementation of this method is suboptimal and will be removed entirely in a future version. Scheduled for removal in v14")]
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
|
|
|
|
|
=> ItemByXPath(xpath, vars, _publishedSnapshot.Content);
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<int> ids)
|
|
|
|
|
=> ItemsByIds(_publishedSnapshot.Content, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<Guid> ids)
|
|
|
|
|
=> ItemsByIds(_publishedSnapshot.Content, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<object> ids)
|
|
|
|
|
=> ids.Select(Content).WhereNotNull();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2023-07-07 11:12:04 +02:00
|
|
|
[Obsolete("The current implementation of this method is suboptimal and will be removed entirely in a future version. Scheduled for removal in v14")]
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(string xpath, params XPathVariable[] vars)
|
|
|
|
|
=> ItemsByXPath(xpath, vars, _publishedSnapshot.Content);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2023-07-07 11:12:04 +02:00
|
|
|
[Obsolete("The current implementation of this method is suboptimal and will be removed entirely in a future version. Scheduled for removal in v14")]
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars)
|
|
|
|
|
=> ItemsByXPath(xpath, vars, _publishedSnapshot.Content);
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> ContentAtRoot()
|
|
|
|
|
=> ItemsAtRoot(_publishedSnapshot.Content);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#endregion
|
2020-03-03 11:59:17 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#region Media
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Media(int id)
|
|
|
|
|
=> ItemById(id, _publishedSnapshot.Media);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Media(Guid id)
|
|
|
|
|
=> ItemById(id, _publishedSnapshot.Media);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Media(Udi? id)
|
|
|
|
|
{
|
|
|
|
|
if (!(id is GuidUdi udi))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
return ItemById(udi.Guid, _publishedSnapshot.Media);
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IPublishedContent? Media(object id)
|
|
|
|
|
{
|
|
|
|
|
if (ConvertIdObjectToInt(id, out var intId))
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
return Media(intId);
|
|
|
|
|
}
|
2022-03-16 14:39:28 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (ConvertIdObjectToGuid(id, out Guid guidId))
|
|
|
|
|
{
|
|
|
|
|
return Media(guidId);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (ConvertIdObjectToUdi(id, out Udi? udiId))
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
return Media(udiId);
|
|
|
|
|
}
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<int> ids)
|
|
|
|
|
=> ItemsByIds(_publishedSnapshot.Media, ids);
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<object> ids)
|
|
|
|
|
=> ids.Select(Media).WhereNotNull();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<Guid> ids)
|
|
|
|
|
=> ItemsByIds(_publishedSnapshot.Media, ids);
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerable<IPublishedContent> MediaAtRoot()
|
|
|
|
|
=> ItemsAtRoot(_publishedSnapshot.Media);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#endregion
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#region Used by Content/Media
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static IPublishedContent? ItemById(int id, IPublishedCache? cache)
|
|
|
|
|
=> cache?.GetById(id);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static IPublishedContent? ItemById(Guid id, IPublishedCache? cache)
|
|
|
|
|
=> cache?.GetById(id);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static IPublishedContent? ItemByXPath(string xpath, XPathVariable[] vars, IPublishedCache? cache)
|
|
|
|
|
=> cache?.GetSingleByXPath(xpath, vars);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache? cache, IEnumerable<int> ids)
|
|
|
|
|
=> ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache? cache, IEnumerable<Guid> ids)
|
|
|
|
|
=> ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(string xpath, XPathVariable[] vars,
|
|
|
|
|
IPublishedCache? cache)
|
|
|
|
|
=> cache?.GetByXPath(xpath, vars) ?? Array.Empty<IPublishedContent>();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(XPathExpression xpath, XPathVariable[] vars,
|
|
|
|
|
IPublishedCache? cache)
|
|
|
|
|
=> cache?.GetByXPath(xpath, vars) ?? Array.Empty<IPublishedContent>();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static IEnumerable<IPublishedContent> ItemsAtRoot(IPublishedCache? cache)
|
|
|
|
|
=> cache?.GetAtRoot() ?? Array.Empty<IPublishedContent>();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#endregion
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
#region Search
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, string culture = "*",
|
|
|
|
|
string indexName = Constants.UmbracoIndexes.ExternalIndexName)
|
|
|
|
|
=> Search(term, 0, 0, out _, culture, indexName);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, int skip, int take, out long totalRecords,
|
|
|
|
|
string culture = "*", string indexName = Constants.UmbracoIndexes.ExternalIndexName,
|
|
|
|
|
ISet<string>? loadedFields = null)
|
|
|
|
|
{
|
|
|
|
|
if (skip < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(skip), skip,
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (take < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(take), take,
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (string.IsNullOrEmpty(indexName))
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
indexName = Constants.UmbracoIndexes.ExternalIndexName;
|
|
|
|
|
}
|
2020-01-05 22:55:55 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (!_examineManager.TryGetIndex(indexName, out IIndex? index) || index is not IUmbracoIndex umbIndex)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"No index found by name {indexName} or is not of type {typeof(IUmbracoIndex)}");
|
|
|
|
|
}
|
2020-01-05 22:55:55 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
IQuery? query = umbIndex.Searcher.CreateQuery(IndexTypes.Content);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
IOrdering ordering;
|
|
|
|
|
if (culture == "*")
|
|
|
|
|
{
|
|
|
|
|
// Search everything
|
|
|
|
|
ordering = query.ManagedQuery(term);
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrWhiteSpace(culture))
|
|
|
|
|
{
|
|
|
|
|
// Only search invariant
|
|
|
|
|
ordering = query
|
|
|
|
|
.Field(UmbracoExamineFieldNames.VariesByCultureFieldName, "n") // Must not vary by culture
|
|
|
|
|
.And().ManagedQuery(term);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Only search the specified culture
|
|
|
|
|
var fields =
|
|
|
|
|
umbIndex.GetCultureAndInvariantFields(culture)
|
|
|
|
|
.ToArray(); // Get all index fields suffixed with the culture name supplied
|
|
|
|
|
ordering = query.ManagedQuery(term, fields);
|
|
|
|
|
}
|
2018-12-17 13:11:51 +11:00
|
|
|
|
2022-06-23 00:36:37 +02:00
|
|
|
// Filter selected fields because results are loaded from the published snapshot based on these
|
2022-07-13 11:14:31 +02:00
|
|
|
IOrdering? queryExecutor = ordering.SelectFields(_returnedQueryFields);
|
2018-12-20 13:29:57 +01:00
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
ISearchResults? results = skip == 0 && take == 0
|
|
|
|
|
? queryExecutor.Execute()
|
|
|
|
|
: queryExecutor.Execute(QueryOptions.SkipTake(skip, take));
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
totalRecords = results.TotalItemCount;
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
return new CultureContextualSearchResults(results.ToPublishedSearchResults(_publishedSnapshot.Content),
|
|
|
|
|
_variationContextAccessor, culture);
|
|
|
|
|
}
|
2019-11-13 14:39:11 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query)
|
|
|
|
|
=> Search(query, 0, 0, out _);
|
2019-01-07 23:49:29 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query, int skip, int take, out long totalRecords)
|
|
|
|
|
{
|
|
|
|
|
if (skip < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(skip), skip,
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (take < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(take), take,
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (query is IOrdering ordering)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-23 00:36:37 +02:00
|
|
|
// Filter selected fields because results are loaded from the published snapshot based on these
|
2022-07-13 11:14:31 +02:00
|
|
|
query = ordering.SelectFields(_returnedQueryFields);
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2020-01-05 22:55:55 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
ISearchResults? results = skip == 0 && take == 0
|
|
|
|
|
? query.Execute()
|
|
|
|
|
: query.Execute(QueryOptions.SkipTake(skip, take));
|
2022-02-15 16:46:20 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
totalRecords = results.TotalItemCount;
|
|
|
|
|
|
|
|
|
|
return results.ToPublishedSearchResults(_publishedSnapshot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is used to contextualize the values in the search results when enumerating over them, so that the correct
|
|
|
|
|
/// culture values are used.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private class CultureContextualSearchResults : IEnumerable<PublishedSearchResult>
|
|
|
|
|
{
|
|
|
|
|
private readonly string _culture;
|
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
|
|
|
|
private readonly IEnumerable<PublishedSearchResult> _wrapped;
|
2020-01-05 22:55:55 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public CultureContextualSearchResults(IEnumerable<PublishedSearchResult> wrapped,
|
|
|
|
|
IVariationContextAccessor variationContextAccessor, string culture)
|
|
|
|
|
{
|
|
|
|
|
_wrapped = wrapped;
|
|
|
|
|
_variationContextAccessor = variationContextAccessor;
|
|
|
|
|
_culture = culture;
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public IEnumerator<PublishedSearchResult> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
// We need to change the current culture to what is requested and then change it back
|
|
|
|
|
VariationContext? originalContext = _variationContextAccessor.VariationContext;
|
|
|
|
|
if (!_culture.IsNullOrWhiteSpace() && !_culture.InvariantEquals(originalContext?.Culture))
|
|
|
|
|
{
|
|
|
|
|
_variationContextAccessor.VariationContext = new VariationContext(_culture);
|
|
|
|
|
}
|
2019-11-01 10:49:45 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Now the IPublishedContent returned will be contextualized to the culture specified and will be reset when the enumerator is disposed
|
|
|
|
|
return new CultureContextualSearchResultsEnumerator(_wrapped.GetEnumerator(), _variationContextAccessor,
|
|
|
|
|
originalContext);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
|
|
2019-01-07 23:49:29 +11:00
|
|
|
/// <summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
/// Resets the variation context when this is disposed.
|
2019-01-07 23:49:29 +11:00
|
|
|
/// </summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
private class CultureContextualSearchResultsEnumerator : IEnumerator<PublishedSearchResult>
|
2019-01-07 23:49:29 +11:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
private readonly VariationContext? _originalContext;
|
2020-03-03 11:59:17 +01:00
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
2022-06-02 08:18:31 +02:00
|
|
|
private readonly IEnumerator<PublishedSearchResult> _wrapped;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public CultureContextualSearchResultsEnumerator(
|
|
|
|
|
IEnumerator<PublishedSearchResult> wrapped,
|
|
|
|
|
IVariationContextAccessor variationContextAccessor,
|
|
|
|
|
VariationContext? originalContext)
|
2019-01-07 23:49:29 +11:00
|
|
|
{
|
|
|
|
|
_wrapped = wrapped;
|
|
|
|
|
_variationContextAccessor = variationContextAccessor;
|
2022-06-02 08:18:31 +02:00
|
|
|
_originalContext = originalContext;
|
2019-01-07 23:49:29 +11:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public PublishedSearchResult Current => _wrapped.Current;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
object IEnumerator.Current => Current;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Dispose()
|
2019-01-07 23:49:29 +11:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
_wrapped.Dispose();
|
2019-01-07 23:49:29 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Reset to original variation context
|
|
|
|
|
_variationContextAccessor.VariationContext = _originalContext;
|
|
|
|
|
}
|
2019-01-07 23:49:29 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public bool MoveNext() => _wrapped.MoveNext();
|
2019-01-07 23:49:29 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Reset() => _wrapped.Reset();
|
2019-01-07 23:49:29 +11:00
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
|
|
|
|
|
#endregion
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|