How to get matching string by performing search on Lucene?
I would like to know how can I get the matching string results by performing search on a Lucene ind开发者_如何学运维ex ?
I have tried to install the Perl module Lucene::Search::Highlight
and failed over and over again. Is there another I can get the relevant fragments of text that Lucene "thinks" they relevant for my search phrase?
Two possible ways to do what you ask for, dependent on what you want:
- See what strings matched for debugging purposes: Java Lucene has explain() for this purpose. As Plucene should be similar, I suggest you look for explain() in the source code, as I failed to find it in the documentation.
- Display matched strings to the user: Use the highlighter, as you tried to do. I suggest you post code with specific problems to SO and probably someone will be able to help.
You can get the words from query, Then
function highlightWords($text, $words)
{
/*** loop of the array of words ***/
foreach ($words as $word)
{
/*** highlight the words ***/
$text = preg_replace("/\b($word)\b/i",
'<span style="color:red">\1</span>', $text);
}
/*** return the text ***/
return $text;
}
精彩评论