2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Xml.XPath;
|
|
|
|
|
|
using Examine;
|
2018-12-17 12:17:03 +11:00
|
|
|
|
using Examine.Search;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Core.Xml;
|
|
|
|
|
|
using Umbraco.Web.PublishedCache;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
|
|
|
|
|
using Examine = global::Examine;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A class used to query for published content, media items
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PublishedContentQuery : IPublishedContentQuery
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPublishedContentCache _contentCache;
|
|
|
|
|
|
private readonly IPublishedMediaCache _mediaCache;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor used to return results from the caches
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="contentCache"></param>
|
|
|
|
|
|
/// <param name="mediaCache"></param>
|
|
|
|
|
|
public PublishedContentQuery(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache)
|
|
|
|
|
|
{
|
|
|
|
|
|
_contentCache = contentCache ?? throw new ArgumentNullException(nameof(contentCache));
|
|
|
|
|
|
_mediaCache = mediaCache ?? throw new ArgumentNullException(nameof(mediaCache));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Content
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(int id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(Guid id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(udi.Guid, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemByXPath(xpath, vars, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<int> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_contentCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_contentCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(string xpath, params XPathVariable[] vars)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByXPath(xpath, vars, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByXPath(xpath, vars, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtRoot()
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsAtRoot(_contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Media
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Media(int id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _mediaCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Media(Guid id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _mediaCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Media(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(udi.Guid, _mediaCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<int> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_mediaCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_mediaCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> MediaAtRoot()
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsAtRoot(_mediaCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Used by Content/Media
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent ItemById(int id, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetById(id);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent ItemById(Guid id, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetById(id);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent ItemByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//NOTE: Not used?
|
|
|
|
|
|
//private IPublishedContent ItemByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
// return doc;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<int> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsAtRoot(IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
return cache.GetAtRoot();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Search
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 12:17:03 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, string indexName = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-12-17 12:17:03 +11:00
|
|
|
|
return Search(term, 0, 0, out _, indexName);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 12:17:03 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, int skip, int take, out long totalRecords, string indexName = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-12-03 22:10:56 +11:00
|
|
|
|
indexName = string.IsNullOrEmpty(indexName)
|
2018-12-11 15:42:32 +11:00
|
|
|
|
? Constants.UmbracoIndexes.ExternalIndexName
|
2018-12-03 22:10:56 +11:00
|
|
|
|
: indexName;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-12-03 22:10:56 +11:00
|
|
|
|
if (!ExamineManager.Instance.TryGetIndex(indexName, out var index))
|
|
|
|
|
|
throw new InvalidOperationException($"No index found by name {indexName}");
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-12-03 22:10:56 +11:00
|
|
|
|
var searcher = index.GetSearcher();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-12-05 16:53:25 +11:00
|
|
|
|
var results = skip == 0 && take == 0
|
2018-12-13 15:04:18 +11:00
|
|
|
|
? searcher.Search(term)
|
|
|
|
|
|
: searcher.Search(term, maxResults: skip + take);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-12-05 16:53:25 +11:00
|
|
|
|
totalRecords = results.TotalItemCount;
|
|
|
|
|
|
return results.ToPublishedSearchResults(_contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 12:17:03 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-12-17 12:17:03 +11:00
|
|
|
|
return Search(query, 0, 0, out _);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 12:17:03 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query, int skip, int take, out long totalRecords)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var results = skip == 0 && take == 0
|
2018-12-17 12:17:03 +11:00
|
|
|
|
? query.Execute()
|
|
|
|
|
|
: query.Execute(maxResults: skip + take);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
totalRecords = results.TotalItemCount;
|
|
|
|
|
|
return results.ToPublishedSearchResults(_contentCache);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|