Skip to content
This repository has been archived by the owner on Feb 16, 2018. It is now read-only.

Indexing

nath edited this page Jan 22, 2016 · 5 revisions

Just include the SearchableTrait to enable indexing of the Husband model:

class Husband extends Eloquent {

    use Menthol\Flexible\Traits\SearchableTrait;

    ....

To index this model Flexible needs to know what the paths are to the related models. A path is a dot seperated string of relation methods that are defined on the Eloquent models. It also needs to know the 'inverse' of this path to determine which Searchable model records should be reindexed when a change is made to one of its related models. To this end Flexible exposes an Artisan 'paths' command. This command will generate the paths for you and optionaly write them to your local flexible package config. The paths command needs to be rerun when a change is made to the model relationships. Also, the relationships need to be implemented properly with the relations defined in both directions.

Run the following Artisan command to generate the paths for the model:

php artisan flexible:paths Husband

To generate paths for relations you should add the --relations flag. Use the --dir=app/models option (or other path) to lookup all Models having a SearchableTrait.

Run the following Artisan command to index the model:

php artisan flexible:reindex Husband

To automatically add all related models to the indexed documents you can use the --relations flag. The --dir= parameter may also help. By default Flexible will use the database table as index name and the model name as type. There are some more options on the commands to you can inspect by reading the --help output.

TransformableTrait

Include the TransformableTrait to get more control over the indexing proces:

class Husband extends Eloquent {

    use Menthol\Flexible\Traits\TransformableTrait;

    ....

It provides a basic transform function to convert attributes to an indexable property array. You can override this function in your model to gain full control over how the property array is built.

Indexing Configuration

You can specify how certain fields are analysed by Elasticsearch during the indexing process. This is done by declaring a public static variable on your model:

class Husband extends Eloquent {

    public static $__es_config = [

    ];

    ....

This array contains analysis settings for different fields. It is possible to specify fields from related models using dot notation.

  • Autocomplete or Partial matches
class Husband extends Eloquent {

    public static $__es_config = [
        'autocomplete' => ['field1', 'field2', 'relation.field1', 'relation.field2', ...]
    ];

    ...

Instead of the generic 'autocomplete' analyzer you can also use more specific variants:

  • text_start
  • text_middle
  • text_end
  • word_start
  • word_middle
  • word_end

The text types use the keyword tokeniser and the word types use the standard tokeniser.

  • Suggestions
class Husband extends Eloquent {

    public static $__es_config = [
        'suggest' => ['field1', 'field2', 'relation.field1', 'relation.field2', ...]
    ];

    ...