How using algolia/scout-extended make Faceting dynamic?

74 Views Asked by At

In Algolia docs https://www.algolia.com/ecommerce-merchandising-playbook/configuring-faceting/

I read

Faceted search is a way of classifying records, allowing users to narrow down results. You can think of facets as filterable categories.

I share with Algolia 2 tables : a) tasks(it has category_id and parent_id fields) :

Schema::create('tasks', function (Blueprint $table) {
    $table->id();
    $table->foreignId('parent_id')->nullable()->references('id')->on('tasks')->onUpdate('RESTRICT')->onDelete('CASCADE');
    $table->foreignId('creator_id')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('CASCADE');
    $table->string('title', 255);
    $table->smallInteger('category_id')->unsigned();
    $table->foreign('category_id', 'users_categories_foreign')->references('id')->on('categories')->onUpdate('RESTRICT')->onDelete('RESTRICT');
    $table->enum('priority', [ '1', '2', '3', '4', '5'])->default("1")->comment('1-Low, 2-Normal, 3-High, 4-Urgent, 5-Immediate  ');
    $table->enum('status', ['D', 'T'])->default('T')->comment('D => Done, T=>Todo');
    $table->integer('weight')->unsigned();
    $table->mediumText('description');
    $table->datetime('completed_at')->nullable();
    $table->timestamp('created_at')->useCurrent();

As I use scout-extended plugin I have config table for these 2 tables and in config/scout-tasks-index.php I have :

/*
|--------------------------------------------------------------------------
| Attributes For Faceting
|--------------------------------------------------------------------------
|
| Your index comes with no categories. By designating an attribute as a facet, this enables
| Algolia to compute a set of possible values that can later be used to create categories
| or filters. You can also get a count of records that match those values.
|
| Supported: Null, Array
| Example: ['type', 'filterOnly(country)', 'searchable(city)',]
|
*/

'attributesForFaceting' => ['category_id'],

I do not need attributesForFaceting equals 'category_id', but some dynamic value like number of child(field parent_id) uncompleted (field status = 'T') tasks. I do not know how to make condition for these rules, but I see some examples of attributesForFaceting - above - looks like it use some formulas...

b) categories (has many tasks):

Schema::create('categories', function (Blueprint $table) {
    $table->smallIncrements('id')->unsigned();
    $table->string('title', 100)->unique();
    $table->string('slug', 102)->unique();
    $table->integer('weight')->unsigned();

    $table->timestamp('created_at')->useCurrent();

I attributesForFaceting of categories I need to show number for filtering of related tasks.

How can I do it and how to use from Laravel app? Is Facetin valid option for my goal ?

UPDATED INFO : In app/Models/Category.php model I added

class Category extends Model
{
    use Searchable;

    protected $table = 'categories';
    ...
    public function searchableAs(): string
    {
        return 'categories_index';
    }

    public function toSearchableArray(): array
    {
        $datetimeFormat = ConfigValueEnum::get(ConfigValueEnum::DATETIME_ASTEXT_FORMAT);
        $data = $this->toArray();
        ...

        // Added 2 calculable fields
        $data['active_tasks_count'] = Task::getByStatus(TaskStatus::TODO->value)->count();
        $data['completed_tasks_count'] = Task::getByStatus(TaskStatus::DONE->value)->count();

        return $data;
    }

and I use these fields in config/scout-categories-index.php :

'attributesForFaceting' => ['active_tasks_count', 'completed_tasks_count'],

After I see under Algolia console :

enter image description here How can I use these values when calling method search of Category model and use these values as filter ?

"algolia/algoliasearch-client-php": "^3.4",
"algolia/scout-extended": "^3.0",
"laravel/framework": "^10.34.2",

UPDATED INFO # 2 :

On docs https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/#examples page

I see using of 'facetFilters' in searchmethod with example code :

$results = $index->search('query', [
  'facetFilters' => [
    ["category:Book", "category:Movie"],
    "author:John Doe"
  ]
]);

But as in my printrscreen above there are calculated numbers with label "active_tasks_count" and "completed_tasks_count" I need to create facetFilters like "completed_tasks_count > active_tasks_count" OR "active_tasks_count" > 10.

Can I do it in anyway ?

0

There are 0 best solutions below