You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Umbraco 13 自定义字典搜索:扩展至字典值检索方法咨询

扩展Umbraco字典搜索以支持检索键值内容

针对Umbraco 9+(.NET Core版本)

Umbraco后台字典树的搜索逻辑由ITreeSearchProvider接口驱动,默认实现仅检索字典键名。我们可以通过自定义搜索提供者扩展功能,加入对字典值的检索:

  1. 创建自定义字典树搜索提供者
    继承默认的TreeSearchProviderBase<DictionaryTree>,重写SearchAsync方法补充字典值的搜索逻辑:
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Trees;
using Umbraco.Cms.Infrastructure.Trees.Search;

public class CustomDictionaryTreeSearchProvider : TreeSearchProviderBase<DictionaryTree>
{
    private readonly ILocalizationService _localizationService;

    public CustomDictionaryTreeSearchProvider(ILocalizationService localizationService)
    {
        _localizationService = localizationService;
    }

    public override async Task<IEnumerable<SearchResultItem>> SearchAsync(string searchTerm, int pageSize, long pageIndex, string? searchFrom = null, CancellationToken cancellationToken = default)
    {
        // 获取默认的键名搜索结果
        var baseResults = await base.SearchAsync(searchTerm, pageSize, pageIndex, searchFrom, cancellationToken);
        
        // 检索包含关键词的字典值对应的字典项
        var valueMatches = _localizationService.GetDictionaryItemsByValue(searchTerm)
            .Select(item => new SearchResultItem
            {
                Id = item.Id.ToString(),
                Name = item.ItemKey,
                Path = $"-1,{item.Id}",
                Icon = "icon-umbraco",
                ParentId = "-1",
                TreeAlias = DictionaryTree.Alias
            });
        
        // 合并结果并按ID去重,避免重复项
        return baseResults.UnionBy(valueMatches, x => x.Id);
    }
}
  1. 注册自定义服务
    在Program.cs(Umbraco 10+)或Startup.cs中替换默认的字典树搜索提供者:
builder.Services.AddUnique<ITreeSearchProvider, CustomDictionaryTreeSearchProvider>(typeof(DictionaryTree));

针对Umbraco 7/8(.NET Framework版本)

旧版Umbraco依赖Examine索引实现后台搜索,需要修改字典项的索引规则,将值加入索引字段:

  1. 创建索引事件处理器
    监听字典项保存事件,将所有语言的字典值写入Examine索引:
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Examine;

public class DictionaryIndexingEventHandler : IComponent
{
    private readonly IExamineManager _examineManager;

    public DictionaryIndexingEventHandler(IExamineManager examineManager)
    {
        _examineManager = examineManager;
    }

    public void Initialize()
    {
        DictionaryService.Saving += DictionaryService_Saving;
    }

    private void DictionaryService_Saving(IDictionaryService sender, SaveEventArgs<IDictionaryItem> e)
    {
        if (!_examineManager.TryGetIndex(Constants.UmbracoIndexes.InternalIndexName, out var index)) return;
        
        foreach (var item in e.SavedEntities)
        {
            // 合并所有语言的字典值,用空格分隔以便搜索
            var combinedValues = string.Join(" ", item.Values.Select(v => v.Value));
            var indexValues = new Dictionary<string, object>
            {
                ["dictionaryValue"] = combinedValues
            };
            
            // 更新索引项
            index.IndexItem(new ValueSet(item.Id.ToString(), "dictionaryItem", indexValues));
        }
    }

    public void Terminate()
    {
        DictionaryService.Saving -= DictionaryService_Saving;
    }
}
  1. 注册组件
    创建自定义Composer来注册事件处理器:
using Umbraco.Core.Composing;

public class CustomComposer : IUserComposer
{
    public void Compose(Composition composition)
    {
        composition.Components().Append<DictionaryIndexingEventHandler>();
    }
}
  1. 调整搜索逻辑
    修改字典树的搜索查询,让它同时检索nodeName(键名)和dictionaryValue字段,可通过自定义TreeSearcher或直接调整Examine搜索参数实现。

额外注意事项

  • 部署后需重新生成Examine索引(Umbraco 7/8)或同步字典项数据(Umbraco 9+),确保已有字典值被纳入搜索范围
  • 多语言场景下,上述代码已自动合并所有语言的值,无需额外处理
  • Umbraco Cloud环境中,需确保自定义代码正确打包部署,避免被平台默认配置覆盖

内容的提问来源于stack exchange,提问作者SwissDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 23:25:27