Skip to content

Commit

Permalink
advanced-search-highlight integration
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkwinkelmann committed May 8, 2023
1 parent f0b6cac commit 152e0b7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"require-dev": {
"teamtnt/laravel-scout-tntsearch-driver": "^11.6"
},
"suggest": {
"clarkwinkelmann/flarum-ext-advanced-search-highlight": "*"
},
"authors": [
{
"name": "Clark Winkelmann",
Expand Down Expand Up @@ -43,7 +46,10 @@
"name": "fas fa-search",
"backgroundColor": "#f47f68",
"color": "#fff"
}
},
"optional-dependencies": [
"clarkwinkelmann/flarum-ext-advanced-search-highlight"
]
}
}
}
50 changes: 49 additions & 1 deletion src/ScoutStatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace ClarkWinkelmann\Scout;

use ClarkWinkelmann\AdvancedSearchHighlight\Highlighter;
use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Arr;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\MeiliSearchEngine;

Expand All @@ -12,6 +16,17 @@
*/
class ScoutStatic
{
// This is not really the attributes to highlight, rather it's the attributes highlights will be extracted for
// The actual list of attributes to highlight is configured in the advanced-search-highlight extension
public static $attributesToHighlight = [
Discussion::class => [
'title',
],
Post::class => [
'content',
],
];

protected static function useQueue(): bool
{
$settings = resolve(SettingsRepositoryInterface::class);
Expand Down Expand Up @@ -59,6 +74,39 @@ public static function makeBuilder(string $class, string $query, $callback = nul
{
$wrapped = new ScoutModelWrapper(new $class);

$isMeilisearch = $wrapped->searchableUsing() instanceof MeiliSearchEngine;

if ($isMeilisearch && is_null($callback) && class_exists(Highlighter::class)) {
$callback = function ($meilisearch, $query, $searchParams) use ($class) {
$attributes = Arr::get(self::$attributesToHighlight, $class) ?? [];

$results = $meilisearch->rawSearch($query, $searchParams + [
'attributesToHighlight' => $attributes,
'showMatchesPosition' => true,
]);

foreach ($results['hits'] as $hit) {
foreach ($attributes as $attribute) {
$positions = Arr::get($hit, '_matchesPosition.' . $attribute);

if (is_array($positions)) {
foreach ($positions as $position) {
$after = substr($hit[$attribute], $position['start'] + $position['length'], 1);

Highlighter::addHighlightRule(
substr($hit[$attribute], $position['start'], $position['length']),
$position['start'] === 0 ? null : substr($hit[$attribute], $position['start'] - 1, 1),
$after === '' ? null : $after
);
}
}
}
}

return $results;
};
}

$builder = resolve(Builder::class, [
'model' => $wrapped,
'query' => $query,
Expand All @@ -72,7 +120,7 @@ public static function makeBuilder(string $class, string $query, $callback = nul
// Developers can still customize it after getting the instance
if ($limit > 0) {
$builder->take($limit);
} else if ($wrapped->searchableUsing() instanceof MeiliSearchEngine) {
} else if ($isMeilisearch) {
// Meilisearch default limit of 20 is extremely low
// If you have a large number of models that aren't visible to guests
// you might end up with not a single result after the visibility scope is applied
Expand Down

0 comments on commit 152e0b7

Please sign in to comment.