Elastic Nest中通过Automapper全局剥离字段HTML的可行性咨询
Absolutely, this is completely feasible! You can set up AutoMapper to automatically strip HTML content from every string field when mapping your entities to Elasticsearch documents. Here's how to implement this cleanly:
Step 1: Create an HTML-Stripping Value Resolver
First, build a custom value resolver that handles removing HTML tags. For basic cases, a regex works, but for more robust handling (like nested tags, comments, or malformed HTML), using a library like HtmlAgilityPack is better.
Option 1: Simple Regex-Based Resolver
public class StripHtmlResolver : IValueResolver<object, object, string> { private static readonly Regex HtmlRegex = new Regex("<.*?>", RegexOptions.Compiled); public string Resolve(object source, object destination, string destMember, ResolutionContext context) { if (source == null) return null; var rawString = source.ToString(); return HtmlRegex.Replace(rawString, string.Empty).Trim(); } }
Option 2: Robust Resolver with HtmlAgilityPack
First, install the NuGet package: Install-Package HtmlAgilityPack
Then create the resolver:
using HtmlAgilityPack; public class StripHtmlResolver : IValueResolver<object, object, string> { public string Resolve(object source, object destination, string destMember, ResolutionContext context) { if (source == null) return null; var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(source.ToString()); return htmlDoc.DocumentNode.InnerText.Trim(); } }
Step 2: Configure AutoMapper to Apply Globally
Next, update your AutoMapper profile to apply this resolver to all string properties. You can target only string fields using ForAllPropertyMaps:
public class ElasticsearchMappingProfile : Profile { public ElasticsearchMappingProfile() { // Apply HTML stripping to all string properties ForAllPropertyMaps(map => map.DestinationMember.GetMemberType() == typeof(string), (map, opts) => opts.ResolveUsing<StripHtmlResolver>()); // Add your specific entity mappings here (if any) CreateMap<YourSourceEntity, YourElasticsearchDocument>(); } }
Step 3: Register the Profile
Make sure to register your profile when initializing AutoMapper:
var configuration = new MapperConfiguration(cfg => { cfg.AddProfile<ElasticsearchMappingProfile>(); }); var mapper = configuration.CreateMapper();
Notes to Consider
- Non-String Fields: The above configuration only targets string fields, so non-string types (like numbers, dates) won't be affected—perfect for Elasticsearch indexing.
- Exceptions for Specific Fields: If some fields should retain HTML (e.g., a field explicitly meant to store raw HTML), you can override the global rule for those fields:
CreateMap<YourSourceEntity, YourElasticsearchDocument>() .ForMember(dest => dest.RawHtmlField, opt => opt.MapFrom(src => src.RawHtmlField)); // Bypasses the global resolver - Performance: If processing large volumes of data, prioritize reliability and speed. HtmlAgilityPack is generally more efficient and accurate than regex for complex HTML structures.
This setup ensures every string field going into your Elasticsearch index is automatically stripped of HTML content, no need to configure each field individually!
内容的提问来源于stack exchange,提问作者rs82uk

