最近组里同事开发项目时遇到一个Sphinx的问题,表面上看起来没有问题的代码,实际运行时却总是无法的到正常的结果。
我看了一下,主要的代码片段如下:
//高亮显示的配置参数的数组 $opts = array( "before_match" => "<span class=\"highlight\">", "after_match" => "</span>", "chunk_separator" => "...", "limit" => 60, "around" => 25, "single_passage" => true, "exact_phrase" => false ); ...... //高亮显示 $highlight_name_array = search_highlight( $name_docs, $index, $words, $opts ); $highlight_description_array = search_highlight( $description_docs, $index, $words, $opts ); ...... function search_highlight( $docs, $index, $words, $opts ) { $cl = new SphinxClient (); $arr_words = $cl->BuildExcerpts ( $docs, $index, $words, $opts ); return $arr_words; }
遇到的情况却是标题里的高亮显示没有问题,但是介绍里的高亮却总是没有办法出来。

经过反复调试,发现和参数传递,过程处理,字符编码都没有关系,得到的结果在第二句search_highlight处理时总是固执的没有任何变化。
后来又把问题放在了参数设置上,将single_passage、exact_phrase两个参数去掉,替换before_match、after_match,都没有得到任何的成效。
一筹莫展时,突然看到了around这个参数,一般我们都设置为3,而这里设置为30,把这个参数改一改试试,发现It works!

经过调试,发现around值为25的时候能满足功能和产品的需求。
这个问题可能是Sphinx的一个bug,在某些特定条件下高亮的截取会失败,在有时间的时候我会阅读源码来调查这个问题的产生根源。
在使用Sphinx进行文本高亮显示时遇到特定条件下的bug导致介绍部分高亮显示失败,通过调整参数around的值从25改为30找到了解决方案。
528

被折叠的 条评论
为什么被折叠?



