search - 扩展 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, includeProperties, pageNumber, pageSize, orderBy, orderDirection, orderBySystemField, 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, 0);
}
var pagedResult = new PagedResult<ContentItemBasic<ContentPropertyBasic>>(totalChildren, pageNumber, 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 不工作,请确保它在文档类型中设置(文档类型名称的最右侧)
推荐阅读
- digital-ocean - 如何通过 ssh 进入 Digital Ocean 的 App Platform 应用程序?
- execution - 没有得到关于我的模型调整的任何执行细节
- swift - 以编程方式添加的导航项不显示
- sql - MS Access:具有多个条件的双循环查询以显示在表单上
- javascript - 在不同的 div 上一次折叠一个
- css - 通过检查元素永久更改,让浏览器记住我的外观偏好的方法?
- django - 如何将简单的序列化器容纳到模型序列化器中或在 Django rest API 视图中组合 2 个单独的应用程序(模型序列化器)
- java - 执行派生类中的 Test 时,如何执行基类中的 TestNG 注释?
- sql-server - 如何在不包含所有参数的情况下调用存储过程?
- django - 如何在 Django 的一个 HTML 模板中显示在 ListView 中声明的两个不同模型的两个单独列表?