Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

added facetAttributes in Model #109

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,52 @@ class BookController extends Controller
}
```

#### Attributes for Faceting <!-- omit in toc -->

You can read details of faceting in the [Faceted Search](https://docs.meilisearch.com/reference/features/faceted_search.html#filters-or-facets) section of the documentation.

```php
<?php

use Laravel\Scout\Searchable;

class Book extends Model
{
use Searchable;

//...

public function facetAttributes(): array
{
return [
'category',
'tags'
];
}
}
```

And you can use it like:

```php
<?php

class BookController extends Controller
{
public function customSearch()
{
Book::search('prince', function (Indexes $meilisearch, $query, $options) {
$options['facetFilters'] = ['category:Book'];
// OR $options['facetFilters'] = ['category:book', 'tags:horror'];

return $meilisearch->search($query, $options);
})->take(3)->get();
}
}
```



## 🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the [version v0.19.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.19.0).
Expand Down
4 changes: 4 additions & 0 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function update($models)
if (!empty($objects)) {
$index->addDocuments($objects, $models->first()->getKeyName());
}

if (method_exists($models->first(), 'facetAttributes')) {
$index->updateAttributesForFaceting($models->first()->facetAttributes());
}
}

/**
Expand Down