如何解决扩展 Umbraco 后台搜索以搜索自定义属性,而不仅仅是标题
我想做的是能够在后台搜索中键入自定义属性。例如将 ISBN 放入搜索字段并显示当前结果,它始终返回“未找到项目”,因为搜索将仅显示标题节点的结果。
数据在内部索引中,我已经检查过索引是否正常工作,如果我通过自定义数据搜索,可以通过“检查管理”看到结果。
解决方法
解决方案是我用来扩展搜索的 https://dev.to/skttl/how-to-customize-searching-in-umbraco-list-views-1knk
在 App_Code (SearchExtender) 中添加一个新文件
using System.Linq;
using Examine;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web;
using Umbraco.Web.Editors;
using Umbraco.Web.Models.ContentEditing;
namespace SearchExtender
{
public class CustomListViewSearchController : ContentController
{
public CustomListViewSearchController(PropertyEditorCollection propertyEditors,IGlobalSettings globalSettings,IUmbracoContextAccessor umbracoContextAccessor,ISqlContext sqlContext,ServiceContext services,AppCaches appCaches,IProfilingLogger logger,IRuntimeState runtimeState,UmbracoHelper umbracoHelper)
: base(propertyEditors,globalSettings,umbracoContextAccessor,sqlContext,services,appCaches,logger,runtimeState,umbracoHelper)
{
}
public PagedResult<ContentItemBasic<ContentPropertyBasic>> GetChildrenCustom(int id,string includeProperties,int pageNumber = 0,int pageSize = 0,string orderBy = "SortOrder",Direction orderDirection = Direction.Ascending,bool orderBySystemField = true,string filter = "",string cultureName = "")
{
// get the parent node,and its doctype alias from the content cache
var parentNode = Services.ContentService.GetById(id);
var parentNodeDocTypeAlias = parentNode != null ? parentNode.ContentType.Alias : null;
// if the parent node is not "books",redirect to the core GetChildren() method
if (parentNode?.ContentType.Alias != "books")
{
return GetChildren(id,includeProperties,pageNumber,pageSize,orderBy,orderDirection,orderBySystemField,filter);
}
// if we can't get the InternalIndex,redirect to the core GetChildren() method,but log an error
if (!ExamineManager.Instance.TryGetIndex("InternalIndex",out IIndex index))
{
Logger.Error<CustomListViewSearchController>("Couldn't get InternalIndex for searching products in list view");
return GetChildren(id,filter);
}
// find children using Examine
// create search criteria
var searcher = index.GetSearcher();
var searchCriteria = searcher.CreateQuery();
var searchQuery = searchCriteria.Field("parentID",id);
if (!filter.IsNullOrWhiteSpace())
{
searchQuery = searchQuery.And().GroupedOr(new [] { "nodeName","isbn" },filter);
}
// do the search,but limit the results to the current page ? https://shazwazza.com/post/paging-with-examine/
// pageNumber is not zero indexed in this,so just multiply pageSize by pageNumber
var searchResults = searchQuery.Execute(pageSize * pageNumber);
// get the results on the current page
// pageNumber is not zero indexed in this,so subtract 1 from the pageNumber
var totalChildren = searchResults.TotalItemCount;
var pagedResultIds = searchResults.Skip((pageNumber > 0 ? pageNumber - 1 : 0) * pageSize).Select(x => x.Id).Select(x => int.Parse(x)).ToList();
var children = Services.ContentService.GetByIds(pagedResultIds).ToList();
if (totalChildren == 0)
{
return new PagedResult<ContentItemBasic<ContentPropertyBasic>>(0,0);
}
var pagedResult = new PagedResult<ContentItemBasic<ContentPropertyBasic>>(totalChildren,pageSize);
pagedResult.Items = children.Select(content =>
Mapper.Map<IContent,ContentItemBasic<ContentPropertyBasic>>(content))
.ToList(); // evaluate now
return pagedResult;
}
}
}
更改 /umbraco/backoffice/UmbracoApi/Content/GetChildren(子节点的默认端点)的请求,并将其更改为我新创建的一个,位于 /umbraco/backoffice/api/CustomListViewSearch/GetChildrenCustom。
这很容易通过添加一个包含这样的拦截器的 js 文件来完成。 将文件添加到 /App_Plugins/CustomListViewSearch/CustomListViewSearch.js
angular.module('umbraco.services').config([
'$httpProvider',function ($httpProvider) {
$httpProvider.interceptors.push(function ($q) {
return {
'request': function (request) {
// Redirect any requests for the listview to our custom list view UI
if (request.url.indexOf("backoffice/UmbracoApi/Content/GetChildren?id=") > -1)
request.url = request.url.replace("backoffice/UmbracoApi/Content/GetChildren","backoffice/api/CustomListViewSearch/GetChildrenCustom");
return request || $q.when(request);
}
};
});
}]);
我的 App_Plugins 文件夹中的 package.manifest 文件。
{
"javascript": [
"/App_Plugins/CustomListViewSearch/CustomListViewSearch.js"
]
}
如果节点 Alais 不工作,请确保其设置在文档类型中(文档类型名称的最右侧)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。