# Search

# Overview

Search is configured using the Laravel Scout (opens new window) package. Out the box we have configured the GetCandy hub to use the community provided mysql driver (opens new window).

Using Scout allows us to provide search out the box but also make it easy for you as the developer to customise and tailor searching to your needs. There is no longer a requirement for Elasticsearch to be installed like it was in previous versions of GetCandy.

# Initial set up

When installing GetCandy you will need to add the base config for using the MySQL driver, as outlined in the installation steps. The MySQL driver provides basic search to get you up and running but you will likely find you want to implement something with a bit more power, such as Meilisearch (opens new window) or Algolia (opens new window).

# Configuration

By default, scout has the setting soft_delete set to false. You need to make sure this is set to true otherwise you will see soft deleted models appear in your search results.

If you are using meilisearch, there is some additional set up needed. We have a command ready to go which will set everything up for you, just run:

php artisan getcandy:meilisearch:setup

The above command will create the indexes for the models listed in the config file getcandy/search.php. If you want to use other models or your own models in the search engine, you can add the reference for them on the config file.

'models' => [
        // These models are required by the system, do not change them.
        // Below you can add your own models for indexing

# Additional drivers

If you don't plan on using MySQL, there are some other Scout drivers you can use, depending on your set up. Bear in mind these haven't tested by GetCandy and are provided as reference.

# Index records

If you installed the GetCandy package in an existing project and you like to use the database records with the search engine, or you just need to do some maintenance on the indexes, you can use the index command.

php artisan getcandy:search:index

The command will import the records of the models listed in the getcandy/indexer.php configuration file. Type --help to see the available options.

# Engine Mapping

By default, Scout will use the driver defined in your .env file as SCOUT_DRIVER. So if that's set to meilisearch, all your models will be indexed via the Meilisearch driver. This can present some issues, if you wanted to use a service like Algolia for Products, you wouldn't want all your Orders being indexed there since it will ramp up the record count and the cost.

In GetCandy we've made it possible to define what driver you would like to use per model. It's all defined in the config/getcandy/search.php config file and looks like this:

'engine_map' => [
    \GetCandy\Models\Product::class => 'algolia',
    \GetCandy\Models\Order::class => 'meilisearch',
    \GetCandy\Models\Collection::class => 'meilisearch',

It's quite self explanatory, if a model class isn't added to the config, it will take on the Scout default.