Skip to content

Commit

Permalink
Merge branch 'vhessam-exlude_some_rows_feature'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrala committed Apr 30, 2020
2 parents f87a750 + 30237a7 commit 6c0e5da
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ Results on ``title`` or ``content`` are weighted in the results. Search result s

Enable wildcard after words. So when searching for for example ``car`` it will also match ``carbon``.

### exclude_feature_enabled

This feature excludes some rows from being returned. Enable this when you have a flag in your model which determines whether this record must be returned in search queries or not. By default this feature is disabled.

### exclude_records_column_name

The column name for that property (which acts as a flag). This must match the exact column name at the table.

#### An example of using this feature

Think about when you have a blog and then you add this search functionality to your blogging system to search through your blog posts. Sometimes you do not want some posts to be appeared in search result, for example when a post is not published yet. This feature helps you to do it.

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
6 changes: 6 additions & 0 deletions config/laravel-fulltext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
* Enable wildcard after words
*/
'enable_wildcards' => true,

/*
* Exclude some rows
*/
'exclude_feature_enabled' => false,
'exclude_records_column_name' => 'default_column',
];
14 changes: 10 additions & 4 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ public function searchQuery($search)
'('.$titleWeight.' * (MATCH (indexed_title) AGAINST (?)) +
'.$contentWeight.' * (MATCH (indexed_title, indexed_content) AGAINST (?))
) DESC',
[$termsMatch, $termsMatch]
)
->limit(config('laravel-fulltext.limit-results'))
->with('indexable');
[$termsMatch, $termsMatch])
->limit(config('laravel-fulltext.limit-results'));

if (config('laravel-fulltext.exclude_feature_enabled')) {
$query->with(['indexable' => function ($query) {
$query->where(config('laravel-fulltext.exclude_records_column_name'), '=', true);
}]);
} else {
$query->with('indexable');
}

return $query;
}
Expand Down

0 comments on commit 6c0e5da

Please sign in to comment.