laravel-scout-tntsearch-driver icon indicating copy to clipboard operation
laravel-scout-tntsearch-driver copied to clipboard

How to get highlighted match result ?

Open gary101 opened this issue 4 years ago • 2 comments

I want to apply this to my website api but can not figure out how to get highlighted match result ,

How should I do to do result highlight?

Thanks a lot!

gary101 avatar Jun 15 '20 06:06 gary101

Here's an example on how to use the highlighter

https://github.com/teamtnt/tntsearch-demo/blob/51e6ccc11168dc06f8cd837be1b5cda3e0518a1b/app/Http/Controllers/SearchController.php

lines 89 and 93

nticaric avatar Jun 15 '20 11:06 nticaric

Hi @nticaric, thanks a lot for your reply, I make it after refer to the link you gave me and the source code of tntsearch.

I think is good to give the code below as reference to others(actually from source code tntsearch\tests\support\HighlighterTest.php):

    use TeamTNT\TNTSearch\Support\Highlighter;
    
    $hl     = new Highlighter;
    $text   = "This is some text";
    $output = $hl->highlight($text, "is text", 'em', ['wholeWord' => false]);
    $this->assertEquals("Th<em>is</em> <em>is</em> some <em>text</em>", $output);

    $output = $hl->highlight($text, "is text", 'em', ['wholeWord' => true]);
    $this->assertEquals("This <em>is</em> some <em>text</em>", $output);

    $output = $hl->highlight($text, "this text", 'em', ['caseSensitive' => true]);
    $this->assertEquals("This is some <em>text</em>", $output);

    $output = $hl->highlight($text, "this text", 'em', ['caseSensitive' => false]);
    $this->assertEquals("<em>This</em> is some <em>text</em>", $output);

    $output = $hl->highlight($text, "text", 'em');
    $this->assertEquals("This is some <em>text</em>", $output);

    $output = $hl->highlight($text, "text", 'b');
    $this->assertEquals("This is some <b>text</b>", $output);

Thanks again!

gary101 avatar Jun 16 '20 00:06 gary101