Lucene.NET 搜索突出显示尊重 HTML 标签
我试图在 HTML 块中突出显示搜索词,问题是如果用户搜索“颜色”,则: <
span style='color: White'>White;
变成: 白色
显然,打乱我的风格并不是一个好主意。
这是我正在使用的代码:
Query parsedQuery = parser.Parse(luceneQuery);
StandardAnalyzer Analyzer = new StandardAnalyzer();
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b class='search'>", "</b>");
QueryScorer scorer = new QueryScorer(parsedQuery);
Highlighter highlighter = new Highlighter(formatter, scorer);
highlighter.SetTextFragmenter(new SimpleFragmenter());
Highlighter.GetBestFragment(Analyzer, propertyName, invocation.ReturnValue.ToString())
我猜问题是我需要一个不同的碎片机,但我不确定。任何帮助将不胜感激。
I am trying to highlight search terms in a block of HTML, the problem is if a user does a search for "color", this:
<span style='color: white'>White</span>
becomes:
<span style='<b>color</b>: white'><b>White</b></span>
and obviously, messing up my style is not a good idea.
Here is the code I am using:
Query parsedQuery = parser.Parse(luceneQuery);
StandardAnalyzer Analyzer = new StandardAnalyzer();
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b class='search'>", "</b>");
QueryScorer scorer = new QueryScorer(parsedQuery);
Highlighter highlighter = new Highlighter(formatter, scorer);
highlighter.SetTextFragmenter(new SimpleFragmenter());
Highlighter.GetBestFragment(Analyzer, propertyName, invocation.ReturnValue.ToString())
I'm guessing the problem is that i need a different Fragmenter, but I'm not sure. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我明白了...
我将 StandardAnalyzer 子类化并将 TokenStream 更改为:
并将 HtmlStripCharFilter 实现为:
它朝着正确的方向前进,但在完成之前仍然需要做更多的工作。如果有人有更好的解决方案(阅读“经过测试的”解决方案),我很想听听。
I think I figured it out...
I subclassed StandardAnalyzer and changed TokenStream to this:
and Implemented HtmlStripCharFilter as:
It's headed in the right direction, but still needs a lot more work before it's done. If anyone has a better solution (read "TESTED" solution) I would love to hear it.