Skip to content

Commit

Permalink
Fixes Search an element from its GUID with the backoffice #6185 (#6530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza authored and nul800sebastiaan committed Oct 14, 2019
1 parent fa7a463 commit 63ae00b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 31 deletions.
40 changes: 36 additions & 4 deletions src/Umbraco.Web/Search/UmbracoTreeSearcher.cs
Expand Up @@ -7,13 +7,16 @@
using Umbraco.Core;
using Umbraco.Core.Mapping;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Examine;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Trees;

namespace Umbraco.Web.Search
{

/// <summary>
/// Used for internal Umbraco implementations of <see cref="ISearchableTree"/>
/// </summary>
Expand All @@ -24,22 +27,25 @@ public class UmbracoTreeSearcher
private readonly ILocalizationService _languageService;
private readonly IEntityService _entityService;
private readonly UmbracoMapper _mapper;
private readonly ISqlContext _sqlContext;

public UmbracoTreeSearcher(IExamineManager examineManager,
UmbracoContext umbracoContext,
ILocalizationService languageService,
IEntityService entityService,
UmbracoMapper mapper)
UmbracoMapper mapper,
ISqlContext sqlContext)
{
_examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
_umbracoContext = umbracoContext;
_languageService = languageService;
_entityService = entityService;
_mapper = mapper;
_sqlContext = sqlContext;
}

/// <summary>
/// Searches for results based on the entity type
/// Searches Examine for results based on the entity type
/// </summary>
/// <param name="query"></param>
/// <param name="entityType"></param>
Expand All @@ -61,7 +67,7 @@ public class UmbracoTreeSearcher

string type;
var indexName = Constants.UmbracoIndexes.InternalIndexName;
var fields = new[] { "id", "__NodeId" };
var fields = new[] { "id", "__NodeId", "__Key" };

// TODO: WE should try to allow passing in a lucene raw query, however we will still need to do some manual string
// manipulation for things like start paths, member types, etc...
Expand All @@ -70,12 +76,18 @@ public class UmbracoTreeSearcher

//}

//special GUID check since if a user searches on one specifically we need to escape it
if (Guid.TryParse(query, out var g))
{
query = "\"" + g.ToString() + "\"";
}

switch (entityType)
{
case UmbracoEntityTypes.Member:
indexName = Constants.UmbracoIndexes.MembersIndexName;
type = "member";
fields = new[] { "id", "__NodeId", "email", "loginName" };
fields = new[] { "id", "__NodeId", "__Key", "email", "loginName" };
if (searchFrom != null && searchFrom != Constants.Conventions.MemberTypes.AllMembersListId && searchFrom.Trim() != "-1")
{
sb.Append("+__NodeTypeAlias:");
Expand Down Expand Up @@ -129,6 +141,26 @@ public class UmbracoTreeSearcher
}
}

/// <summary>
/// Searches with the <see cref="IEntityService"/> for results based on the entity type
/// </summary>
/// <param name="objectType"></param>
/// <param name="query"></param>
/// <param name="pageSize"></param>
/// <param name="pageIndex"></param>
/// <param name="totalFound"></param>
/// <param name="searchFrom"></param>
/// <returns></returns>
public IEnumerable<SearchResultEntity> EntitySearch(UmbracoObjectTypes objectType, string query, int pageSize, long pageIndex, out long totalFound, string searchFrom = null)
{
//if it's a GUID, match it
Guid.TryParse(query, out var g);

var results = _entityService.GetPagedDescendants(objectType, pageIndex, pageSize, out totalFound,
filter: _sqlContext.Query<IUmbracoEntity>().Where(x => x.Name.Contains(query) || x.Key == g));
return _mapper.MapEnumerable<IEntitySlim, SearchResultEntity>(results);
}

private bool BuildQuery(StringBuilder sb, string query, string searchFrom, string[] fields, string type)
{
//build a lucene query:
Expand Down
17 changes: 11 additions & 6 deletions src/Umbraco.Web/Trees/ContentTreeController.cs
Expand Up @@ -15,6 +15,10 @@
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Search;
using Constants = Umbraco.Core.Constants;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;

namespace Umbraco.Web.Trees
{
Expand All @@ -36,20 +40,21 @@ public class ContentTreeController : ContentTreeControllerBase, ISearchableTree
private readonly UmbracoTreeSearcher _treeSearcher;
private readonly ActionCollection _actions;

public ContentTreeController(UmbracoTreeSearcher treeSearcher, ActionCollection actions)
{
_treeSearcher = treeSearcher;
_actions = actions;
}

protected override int RecycleBinId => Constants.System.RecycleBinContent;

protected override bool RecycleBinSmells => Services.ContentService.RecycleBinSmells();

private int[] _userStartNodes;

protected override int[] UserStartNodes
=> _userStartNodes ?? (_userStartNodes = Security.CurrentUser.CalculateContentStartNodeIds(Services.EntityService));

public ContentTreeController(UmbracoTreeSearcher treeSearcher, ActionCollection actions, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
_treeSearcher = treeSearcher;
_actions = actions;
}

/// <inheritdoc />
protected override TreeNode GetSingleTreeNode(IEntitySlim entity, string parentId, FormDataCollection queryStrings)
{
Expand Down
14 changes: 14 additions & 0 deletions src/Umbraco.Web/Trees/ContentTreeControllerBase.cs
Expand Up @@ -18,11 +18,23 @@
using Umbraco.Web.Actions;
using Umbraco.Web.Composing;
using Umbraco.Core.Security;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Persistence;

namespace Umbraco.Web.Trees
{
public abstract class ContentTreeControllerBase : TreeController
{

protected ContentTreeControllerBase(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
}

protected ContentTreeControllerBase()
{
}

#region Actions

/// <summary>
Expand Down Expand Up @@ -529,6 +541,8 @@ internal IEntitySlim GetEntityFromId(string id)

private bool? _ignoreUserStartNodes;



/// <summary>
/// If the request should allows a user to choose nodes that they normally don't have access to
/// </summary>
Expand Down
20 changes: 15 additions & 5 deletions src/Umbraco.Web/Trees/ContentTypeTreeController.cs
Expand Up @@ -3,11 +3,17 @@
using System.Linq;
using System.Net.Http.Formatting;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Actions;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Search;
using Umbraco.Web.WebApi.Filters;

namespace Umbraco.Web.Trees
Expand All @@ -18,6 +24,13 @@ namespace Umbraco.Web.Trees
[CoreTree]
public class ContentTypeTreeController : TreeController, ISearchableTree
{
private readonly UmbracoTreeSearcher _treeSearcher;

public ContentTypeTreeController(UmbracoTreeSearcher treeSearcher, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
_treeSearcher = treeSearcher;
}

protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
{
var root = base.CreateRootNode(queryStrings);
Expand Down Expand Up @@ -135,10 +148,7 @@ protected override MenuItemCollection GetMenuForNode(string id, FormDataCollecti
}

public IEnumerable<SearchResultEntity> Search(string query, int pageSize, long pageIndex, out long totalFound, string searchFrom = null)
{
var results = Services.EntityService.GetPagedDescendants(UmbracoObjectTypes.DocumentType, pageIndex, pageSize, out totalFound,
filter: SqlContext.Query<IUmbracoEntity>().Where(x => x.Name.Contains(query)));
return Mapper.MapEnumerable<IEntitySlim, SearchResultEntity>(results);
}
=> _treeSearcher.EntitySearch(UmbracoObjectTypes.DocumentType, query, pageSize, pageIndex, out totalFound, searchFrom);

}
}
18 changes: 13 additions & 5 deletions src/Umbraco.Web/Trees/DataTypeTreeController.cs
Expand Up @@ -12,6 +12,11 @@
using Umbraco.Web.Actions;
using Umbraco.Web.Models.ContentEditing;
using Constants = Umbraco.Core.Constants;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Web.Search;

namespace Umbraco.Web.Trees
{
Expand All @@ -21,6 +26,13 @@ namespace Umbraco.Web.Trees
[CoreTree]
public class DataTypeTreeController : TreeController, ISearchableTree
{
private readonly UmbracoTreeSearcher _treeSearcher;

public DataTypeTreeController(UmbracoTreeSearcher treeSearcher, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
_treeSearcher = treeSearcher;
}

protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
{
var intId = id.TryConvertTo<int>();
Expand Down Expand Up @@ -148,10 +160,6 @@ protected override MenuItemCollection GetMenuForNode(string id, FormDataCollecti
}

public IEnumerable<SearchResultEntity> Search(string query, int pageSize, long pageIndex, out long totalFound, string searchFrom = null)
{
var results = Services.EntityService.GetPagedDescendants(UmbracoObjectTypes.DataType, pageIndex, pageSize, out totalFound,
filter: SqlContext.Query<IUmbracoEntity>().Where(x => x.Name.Contains(query)));
return Mapper.MapEnumerable<IEntitySlim, SearchResultEntity>(results);
}
=> _treeSearcher.EntitySearch(UmbracoObjectTypes.DataType, query, pageSize, pageIndex, out totalFound, searchFrom);
}
}
6 changes: 5 additions & 1 deletion src/Umbraco.Web/Trees/MediaTreeController.cs
Expand Up @@ -16,6 +16,10 @@
using Umbraco.Web.Search;
using Constants = Umbraco.Core.Constants;
using Umbraco.Core.Services.Implement;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;

namespace Umbraco.Web.Trees
{
Expand All @@ -35,7 +39,7 @@ public class MediaTreeController : ContentTreeControllerBase, ISearchableTree
{
private readonly UmbracoTreeSearcher _treeSearcher;

public MediaTreeController(UmbracoTreeSearcher treeSearcher)
public MediaTreeController(UmbracoTreeSearcher treeSearcher, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
_treeSearcher = treeSearcher;
}
Expand Down
19 changes: 14 additions & 5 deletions src/Umbraco.Web/Trees/MediaTypeTreeController.cs
Expand Up @@ -10,6 +10,11 @@
using Umbraco.Core.Services;
using Umbraco.Web.Actions;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Web.Search;

namespace Umbraco.Web.Trees
{
Expand All @@ -19,6 +24,13 @@ namespace Umbraco.Web.Trees
[CoreTree]
public class MediaTypeTreeController : TreeController, ISearchableTree
{
private readonly UmbracoTreeSearcher _treeSearcher;

public MediaTypeTreeController(UmbracoTreeSearcher treeSearcher, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
_treeSearcher = treeSearcher;
}

protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
{
var intId = id.TryConvertTo<int>();
Expand Down Expand Up @@ -117,10 +129,7 @@ protected override MenuItemCollection GetMenuForNode(string id, FormDataCollecti
}

public IEnumerable<SearchResultEntity> Search(string query, int pageSize, long pageIndex, out long totalFound, string searchFrom = null)
{
var results = Services.EntityService.GetPagedDescendants(UmbracoObjectTypes.MediaType, pageIndex, pageSize, out totalFound,
filter: SqlContext.Query<IUmbracoEntity>().Where(x => x.Name.Contains(query)));
return Mapper.MapEnumerable<IEntitySlim, SearchResultEntity>(results);
}
=> _treeSearcher.EntitySearch(UmbracoObjectTypes.MediaType, query, pageSize, pageIndex, out totalFound, searchFrom);

}
}
19 changes: 14 additions & 5 deletions src/Umbraco.Web/Trees/TemplatesTreeController.cs
Expand Up @@ -3,12 +3,18 @@
using System.Linq;
using System.Net.Http.Formatting;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Actions;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Mvc;
using Umbraco.Web.Search;
using Umbraco.Web.WebApi.Filters;
using Constants = Umbraco.Core.Constants;

Expand All @@ -20,6 +26,13 @@ namespace Umbraco.Web.Trees
[CoreTree]
public class TemplatesTreeController : TreeController, ISearchableTree
{
private readonly UmbracoTreeSearcher _treeSearcher;

public TemplatesTreeController(UmbracoTreeSearcher treeSearcher, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
_treeSearcher = treeSearcher;
}

protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
{
var root = base.CreateRootNode(queryStrings);
Expand Down Expand Up @@ -119,10 +132,6 @@ private EntitySlim FromTemplate(ITemplate template)
}

public IEnumerable<SearchResultEntity> Search(string query, int pageSize, long pageIndex, out long totalFound, string searchFrom = null)
{
var results = Services.EntityService.GetPagedDescendants(UmbracoObjectTypes.Template, pageIndex, pageSize, out totalFound,
filter: SqlContext.Query<IUmbracoEntity>().Where(x => x.Name.Contains(query)));
return Mapper.MapEnumerable<IEntitySlim, SearchResultEntity>(results);
}
=> _treeSearcher.EntitySearch(UmbracoObjectTypes.Template, query, pageSize, pageIndex, out totalFound, searchFrom);
}
}

0 comments on commit 63ae00b

Please sign in to comment.