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

abkrim's avatar
Level 13

Working with Scout and elasticsearch engine with babenkoivan/elastic-scout-driver-plus

I am quite comfortable working with the babenkoivan/elastic-scout-driver-plus driver and Elasticsearch.

However to date for the subject of the tests I have used my own approach instead of using the factories because I am unable to configure it properly.

It is an application that has 3 models that manage three specific indexes, to which data is sent through queues managed with horizon.

Each one has its model that has NO respect in the database. They are templates for exclusive use with Elasticsearch

ElkAnalyzer

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Elastic\ScoutDriverPlus\Searchable;

class ElkAnalyzer extends Model
{
     use HasFactory;
     use Searchable;

     public $timestamps = false;

     public function searchableAs(): string
     {
         return 'analyzers-2021120901';
     }
}

ElkAnalyzerFactory

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
  * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ElkAnalyzer>
  */
class ElkAnalyzerFactory extends Factory
{
     /**
      * Define the model's default state.
      *
      * @return array<string, mixed>
      */
     public function definition()
     {
         return [
             'modem_id' => $this->faker->randomDigit(),
             'log_id' => $this->faker->randomNumber(5),
             'status_code' => $this->faker->randomElement(["200", "404", "500"]),
         ];
     }
}

composer.json

         "babenkoivan/elastic-scout-driver": "^3.0",
         "babenkoivan/elastic-scout-driver-plus": "^4.2",

As much as I read the documentation, I can't see how to assign the use of elasticseacrh to the model without using mysql.

Factorization error

As you can see it tries to connect to Mysql, instead of to

  ElkAnalyzer::factory()->create()
[!] Aliasing 'ElkAnalyzer' to 'App\Models\ElkAnalyzer' for this Tinker session.

    Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sitelight.elk_analyzers' doesn't exist (Connection: mysql, SQL: insert into `elk_analyzers` (`modem_id`, `log_id`, ` status_code`) values (2, 1686, 200)).

0 likes
2 replies
abkrim's avatar
Level 13

Hi.

Calling static trait member directly is deprecated. It should only be accessed on a class using the trait.

ElkAnalyzer::factory()->create()
[!] Aliasing 'ElkAnalyzer' to 'App\Models\ElkAnalyzer' for this Tinker session.

   Error  Call to undefined method Elastic\ScoutDriverPlus\Searchable::use().

Another lost day... with A.I. But thanks for your efforts.

abkrim's avatar
Level 13

I see the problem.

Implementation in the model has not worked with the Testing approach. Testing working with work-indice instead of test-indice.

Please or to participate in this conversation.