From 26eb848c36688e459e5dfc46cdc68d87ed2bda0c Mon Sep 17 00:00:00 2001 From: Gina Haeussge Date: Sat, 2 Oct 2010 18:04:09 +0200 Subject: FS#1353: Only highlight isolated occurences of search term, not those where it's part of another term. Word boundaries are now respected. --- inc/fulltext.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'inc/fulltext.php') diff --git a/inc/fulltext.php b/inc/fulltext.php index e90205e9c..7dae183c8 100644 --- a/inc/fulltext.php +++ b/inc/fulltext.php @@ -313,7 +313,7 @@ function ft_snippet($id,$highlight){ $len = utf8_strlen($text); // build a regexp from the phrases to highlight - $re1 = '('.join('|',array_map('preg_quote_cb',array_filter((array) $highlight))).')'; + $re1 = '('.join('|',array_map('_ft_snippet_re_preprocess', array_map('preg_quote_cb',array_filter((array) $highlight)))).')'; $re2 = "$re1.{0,75}(?!\\1)$re1"; $re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1"; @@ -386,6 +386,13 @@ function ft_snippet($id,$highlight){ return $evdata['snippet']; } +/** + * Wraps a search term in regex boundary checks. + */ +function _ft_snippet_re_preprocess($term) { + return '\b'.$term.'\b'; +} + /** * Combine found documents and sum up their scores * -- cgit v1.2.3 From 2237b4fa4c84beb06a92308a19e45027e2bdc131 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 3 Oct 2010 12:53:41 +0200 Subject: correctly handle highlighting of partial searches FS#1353 --- inc/fulltext.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'inc/fulltext.php') diff --git a/inc/fulltext.php b/inc/fulltext.php index 7dae183c8..943a5d401 100644 --- a/inc/fulltext.php +++ b/inc/fulltext.php @@ -313,7 +313,7 @@ function ft_snippet($id,$highlight){ $len = utf8_strlen($text); // build a regexp from the phrases to highlight - $re1 = '('.join('|',array_map('_ft_snippet_re_preprocess', array_map('preg_quote_cb',array_filter((array) $highlight)))).')'; + $re1 = '('.join('|',array_map('ft_snippet_re_preprocess', array_map('preg_quote_cb',array_filter((array) $highlight)))).')'; $re2 = "$re1.{0,75}(?!\\1)$re1"; $re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1"; @@ -389,8 +389,19 @@ function ft_snippet($id,$highlight){ /** * Wraps a search term in regex boundary checks. */ -function _ft_snippet_re_preprocess($term) { - return '\b'.$term.'\b'; +function ft_snippet_re_preprocess($term) { + if(substr($term,0,2) == '\\*'){ + $term = substr($term,2); + }else{ + $term = '\b'.$term; + } + + if(substr($term,-2,2) == '\\*'){ + $term = substr($term,0,-2); + }else{ + $term = $term.'\b'; + } + return $term; } /** @@ -685,7 +696,7 @@ function ft_queryParser($query){ break; case 'W+:': $q['words'][] = $body; - $q['highlight'][] = str_replace('*', '', $body); + $q['highlight'][] = $body; $q['and'][] = $body; // for backward compatibility break; case 'P-:': @@ -693,7 +704,7 @@ function ft_queryParser($query){ break; case 'P+:': $q['phrases'][] = $body; - $q['highlight'][] = str_replace('*', '', $body); + $q['highlight'][] = $body; break; } } -- cgit v1.2.3