Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

junedc's avatar
Level 1

Meilisearch activating contains filtering

i need to create a filter using contains which is still experimental please see link https://www.meilisearch.com/docs/learn/filtering_and_sorting/filter_expression_reference#contains

How can we activate that using scout (or laravel). I ended up converting an array to string using json_encode since there are no way to filter using array. Shown below are the results from meilisearch dasboard (index)

converted to json_encode

roles
["administrator","manager","broker"]

original value

arr_roles
array
[
0:"administrator"
1:"manager"
2:"broker"
]

This is my laravel code getting the search from the meilisearch index directly. Im working on the filter parameter

 $meiliSearch = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key'));
  $index = $meiliSearch->index( 'users-index');
 $searchResults = $index->search($search, [
            'limit'                => $searchResultLimit,
            'attributesToRetrieve' => ['*'],
            'filter'               => $meiliFilter
        ]);
        $users = $searchResults->getHits();
0 likes
2 replies
LaryAI's avatar
Level 58

To activate and use the contains filtering feature in Meilisearch with Laravel Scout, you need to ensure that your Meilisearch instance is updated to a version that supports this experimental feature. As of the latest updates, the contains filter is still experimental, so you should proceed with caution and test thoroughly.

Here's a step-by-step guide to implement this:

  1. Update Meilisearch: Ensure your Meilisearch instance is updated to a version that supports the contains filter. Check the Meilisearch documentation for the latest version that includes this feature.

  2. Configure Laravel Scout: Make sure your Laravel application is set up to use Meilisearch with Laravel Scout. This involves setting the correct driver in your config/scout.php file:

    'driver' => 'meilisearch',
    
  3. Indexing Data: When indexing your data, ensure that the array fields you want to filter with contains are stored as JSON strings. You can use json_encode to convert arrays to JSON strings before indexing.

  4. Using the contains Filter: When querying Meilisearch, you can use the contains filter in your search query. Here's an example of how you might construct a query using Laravel Scout:

    use App\Models\YourModel;
    
    $results = YourModel::search('your search query')
        ->where('roles', 'contains', 'administrator')
        ->get();
    

    In this example, roles is the field you want to filter, and 'administrator' is the value you are checking for within the array.

  5. Testing: Since this feature is experimental, thoroughly test your implementation to ensure it behaves as expected. Check for any edge cases or performance issues.

  6. Fallback: If you encounter issues with the contains filter, consider alternative approaches, such as restructuring your data or using different filtering strategies.

By following these steps, you should be able to implement the contains filtering feature in your Laravel application using Meilisearch. Always refer to the latest Meilisearch documentation for updates and changes to experimental features.

jlrdw's avatar

@LaryAI You could just use LIKE.

LIKE '%{$whatever}%'

I bind parameters.

Please or to participate in this conversation.