We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
When you use Laravel Scout for full-text search, you can change the way it finds the results by using a proper matching strategy. In this post, I'll show you how to do that.
I'm assuming you are using Meilisearch in as your full-text search engine. When you use a different engine, this trick will not work.
The matching strategies
Meilisearch has two matching strategies:
-
last
(the default): returns documents containing all the query terms first. If there are not enough results containing all query terms to meet the requestedlimit
, Meilisearch will remove one query term at a time, starting from the end of the query.When you for example search for "big fat liar", Meilisearch will first return documents that contain all three words. If the results don't meet the requested limit, it will also return documents containing only the first two terms, big fat, followed by documents containing only big.
-
all
: only returns documents that contain all query terms. Meilisearch will not match any more documents even if there aren't enough to meet the requestedlimit
.When you for example search for "big fat liar", it would only return documents containing all three words.
Search setup
First, let's look at how the model we're using is setup. In my use-case, I'm having a Document
class which contains
both a searchable field name
(the name of the document) and text
(the actual text contents of the file).
To achieve, this, I've setup the following model:
app/Models/Document.php
```php
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.