2015-02-18 17:14:55 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using System.Linq;
|
2018-03-27 10:04:07 +02:00
|
|
|
|
using System.Reflection;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using System.Xml.XPath;
|
2018-03-27 10:04:07 +02:00
|
|
|
|
using Examine.LuceneEngine.Providers;
|
|
|
|
|
|
using Examine.LuceneEngine.SearchCriteria;
|
|
|
|
|
|
using Examine.SearchCriteria;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using Umbraco.Core;
|
2016-06-10 16:37:28 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2018-03-06 16:11:27 +01:00
|
|
|
|
using Umbraco.Core.Services;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using Umbraco.Core.Xml;
|
|
|
|
|
|
using Umbraco.Web.PublishedCache;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
2017-07-27 12:01:38 +02:00
|
|
|
|
using Examine = global::Examine;
|
|
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A class used to query for published content, media items
|
|
|
|
|
|
/// </summary>
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public class PublishedContentQuery : IPublishedContentQuery
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private readonly IPublishedContentQuery _query;
|
2016-05-26 17:12:04 +02:00
|
|
|
|
private readonly IPublishedContentCache _contentCache;
|
|
|
|
|
|
private readonly IPublishedMediaCache _mediaCache;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2015-02-18 17:14:55 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor used to return results from the caches
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="contentCache"></param>
|
|
|
|
|
|
/// <param name="mediaCache"></param>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
public PublishedContentQuery(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2018-03-27 10:04:07 +02:00
|
|
|
|
_contentCache = contentCache ?? throw new ArgumentNullException(nameof(contentCache));
|
|
|
|
|
|
_mediaCache = mediaCache ?? throw new ArgumentNullException(nameof(mediaCache));
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 17:14:55 +01:00
|
|
|
|
/// <summary>
|
2016-06-30 16:39:05 +02:00
|
|
|
|
/// Constructor used to wrap the ITypedPublishedContentQuery object passed in
|
2015-02-18 17:14:55 +01:00
|
|
|
|
/// </summary>
|
2016-06-30 18:35:43 +02:00
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
|
public PublishedContentQuery(IPublishedContentQuery query)
|
2015-02-18 17:14:55 +01:00
|
|
|
|
{
|
2018-03-27 10:04:07 +02:00
|
|
|
|
_query = query ?? throw new ArgumentNullException(nameof(query));
|
2015-02-18 17:14:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
#region Content
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IPublishedContent Content(int id)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _contentCache)
|
|
|
|
|
|
: _query.Content(id);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
public IPublishedContent Content(Guid id)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _contentCache)
|
|
|
|
|
|
: _query.Content(id);
|
2016-07-19 13:00:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-08 19:39:13 +02:00
|
|
|
|
public IPublishedContent Content(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(udi.Guid, _contentCache)
|
|
|
|
|
|
: _query.Content(udi.Guid);
|
|
|
|
|
|
}
|
2017-09-23 10:08:18 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IPublishedContent ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemByXPath(xpath, vars, _contentCache)
|
|
|
|
|
|
: _query.ContentSingleAtXPath(xpath, vars);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<int> ids)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_contentCache, ids)
|
|
|
|
|
|
: _query.Content(ids);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<Guid> ids)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_contentCache, ids)
|
|
|
|
|
|
: _query.Content(ids);
|
2016-07-19 13:00:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(string xpath, params XPathVariable[] vars)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByXPath(xpath, vars, _contentCache)
|
|
|
|
|
|
: _query.ContentAtXPath(xpath, vars);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByXPath(xpath, vars, _contentCache)
|
|
|
|
|
|
: _query.ContentAtXPath(xpath, vars);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtRoot()
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsAtRoot(_contentCache)
|
|
|
|
|
|
: _query.ContentAtRoot();
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Media
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IPublishedContent Media(int id)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _mediaCache)
|
|
|
|
|
|
: _query.Media(id);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
2016-07-19 13:00:43 +02:00
|
|
|
|
|
2016-11-05 12:22:57 +01:00
|
|
|
|
public IPublishedContent Media(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _mediaCache)
|
|
|
|
|
|
: _query.Media(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-08 19:39:13 +02:00
|
|
|
|
public IPublishedContent Media(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(udi.Guid, _mediaCache)
|
|
|
|
|
|
: _query.Media(udi.Guid);
|
|
|
|
|
|
}
|
2017-09-23 10:08:18 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<int> ids)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_mediaCache, ids)
|
|
|
|
|
|
: _query.Media(ids);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-05 12:22:57 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_mediaCache, ids)
|
|
|
|
|
|
: _query.Media(ids);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> MediaAtRoot()
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsAtRoot(_mediaCache)
|
|
|
|
|
|
: _query.MediaAtRoot();
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Used by Content/Media
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IPublishedContent ItemById(int id, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetById(id);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
private static IPublishedContent ItemById(Guid id, IPublishedCache cache)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
var doc = cache.GetById(id);
|
2016-07-19 13:00:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IPublishedContent ItemByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//NOTE: Not used?
|
2016-06-30 18:35:43 +02:00
|
|
|
|
//private IPublishedContent ItemByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
//{
|
|
|
|
|
|
// var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
// return doc;
|
2017-07-20 11:21:28 +02:00
|
|
|
|
//}
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<int> ids)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
private IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<Guid> ids)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
2016-07-19 13:00:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsAtRoot(IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
return cache.GetAtRoot();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Search
|
|
|
|
|
|
|
2018-03-27 10:04:07 +02:00
|
|
|
|
/// <inheritdoc />
|
2017-07-21 17:04:14 +02:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, bool useWildCards = true, string searchProvider = null)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2018-03-27 10:04:07 +02:00
|
|
|
|
return Search(0, 0, out _, term, useWildCards, searchProvider);
|
|
|
|
|
|
}
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2018-03-27 10:04:07 +02:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(int skip, int take, out int totalRecords, string term, bool useWildCards = true, string searchProvider = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_query != null) return _query.Search(skip, take, out totalRecords, term, useWildCards, searchProvider);
|
|
|
|
|
|
|
|
|
|
|
|
var searcher = string.IsNullOrWhiteSpace(searchProvider)
|
|
|
|
|
|
? Examine.ExamineManager.Instance.DefaultSearchProvider
|
|
|
|
|
|
: Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider];
|
|
|
|
|
|
|
|
|
|
|
|
if (skip == 0 && take == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = searcher.Search(term, useWildCards);
|
|
|
|
|
|
totalRecords = results.TotalItemCount;
|
|
|
|
|
|
return results.ToPublishedSearchResults(_contentCache);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!(searcher is BaseLuceneSearcher luceneSearcher))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = searcher.Search(term, useWildCards);
|
2017-12-15 17:24:54 +11:00
|
|
|
|
totalRecords = results.TotalItemCount;
|
2018-03-27 10:04:07 +02:00
|
|
|
|
// Examine skip, Linq take
|
|
|
|
|
|
return results.Skip(skip).ToPublishedSearchResults(_contentCache).Take(take);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var criteria = SearchAllFields(term, useWildCards, luceneSearcher);
|
2018-03-29 20:01:14 +11:00
|
|
|
|
return Search(skip, take, out totalRecords, criteria, searcher);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-27 10:04:07 +02:00
|
|
|
|
/// <inheritdoc />
|
2017-07-21 17:04:14 +02:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2018-03-27 10:04:07 +02:00
|
|
|
|
return Search(0, 0, out _, criteria, searchProvider);
|
|
|
|
|
|
}
|
2015-02-18 17:14:55 +01:00
|
|
|
|
|
2018-03-27 10:04:07 +02:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(int skip, int take, out int totalRecords, Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_query != null) return _query.Search(skip, take, out totalRecords, criteria, searchProvider);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2018-03-27 10:04:07 +02:00
|
|
|
|
var searcher = searchProvider ?? Examine.ExamineManager.Instance.DefaultSearchProvider;
|
|
|
|
|
|
|
|
|
|
|
|
var results = skip == 0 && take == 0
|
|
|
|
|
|
? searcher.Search(criteria)
|
|
|
|
|
|
: searcher.Search(criteria, maxResults: skip + take);
|
|
|
|
|
|
|
|
|
|
|
|
totalRecords = results.TotalItemCount;
|
2017-07-21 17:04:14 +02:00
|
|
|
|
return results.ToPublishedSearchResults(_contentCache);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-27 10:04:07 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates an ISearchCriteria for searching all fields in a <see cref="BaseLuceneSearcher"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This is here because some of this stuff is internal in Examine.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
private ISearchCriteria SearchAllFields(string searchText, bool useWildcards, BaseLuceneSearcher searcher)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sc = searcher.CreateSearchCriteria();
|
|
|
|
|
|
|
|
|
|
|
|
if (_examineGetSearchFields == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//get the GetSearchFields method from BaseLuceneSearcher
|
|
|
|
|
|
_examineGetSearchFields = typeof(BaseLuceneSearcher).GetMethod("GetSearchFields", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
|
2017-12-15 17:24:54 +11:00
|
|
|
|
}
|
2018-03-27 10:04:07 +02:00
|
|
|
|
|
|
|
|
|
|
//get the results of searcher.BaseLuceneSearcher() using ugly reflection since it's not public
|
|
|
|
|
|
var searchFields = (IEnumerable<string>) _examineGetSearchFields.Invoke(searcher, null);
|
|
|
|
|
|
|
|
|
|
|
|
//this is what Examine does internally to create ISearchCriteria for searching all fields
|
|
|
|
|
|
var strArray = searchText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
sc = useWildcards == false
|
|
|
|
|
|
? sc.GroupedOr(searchFields, strArray).Compile()
|
|
|
|
|
|
: sc.GroupedOr(searchFields, strArray.Select(x => new CustomExamineValue(Examineness.ComplexWildcard, x.MultipleCharacterWildcard().Value)).ToArray<IExamineValue>()).Compile();
|
|
|
|
|
|
return sc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static MethodInfo _examineGetSearchFields;
|
|
|
|
|
|
|
|
|
|
|
|
//support class since Examine doesn't expose it's own ExamineValue class publicly
|
|
|
|
|
|
private class CustomExamineValue : IExamineValue
|
|
|
|
|
|
{
|
|
|
|
|
|
public CustomExamineValue(Examineness vagueness, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Examineness = vagueness;
|
|
|
|
|
|
this.Value = value;
|
|
|
|
|
|
this.Level = 1f;
|
|
|
|
|
|
}
|
|
|
|
|
|
public Examineness Examineness { get; private set; }
|
|
|
|
|
|
public string Value { get; private set; }
|
|
|
|
|
|
public float Level { get; private set; }
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|