babenkoivan / elastic-migrations

Elasticsearch migrations for Laravel
MIT License
189 stars 32 forks source link

Call to undefined method Modules\Document\Models\Document::matchAllSearch() #53

Closed carlin-rj closed 6 months ago

carlin-rj commented 8 months ago
Software Version
PHP PHP 8.1.25
Elasticsearch 8.9.1
Laravel 10.10

Describe the bug Execute the command php artisan elastic:migrate 2024_03_19_190722_oa_document an error message appears Call to undefined method Modules\Document\Models\Document::matchAllSearch()

Model class:

The model has introduced Elastic\ScoutDriverPlus\Searchable screenshot-20240320-092421

Migration class:

<?php
declare(strict_types=1);

use Elastic\Adapter\Indices\Mapping;
use Elastic\Adapter\Indices\Settings;
use Elastic\Migrations\Facades\Index;
use Elastic\Migrations\MigrationInterface;
use Modules\Document\Models\Document;

final class OaDocument implements MigrationInterface
{
    /**
     * Run the migration.
     */
    public function up(): void
    {
        $mapping = [
            'properties' => [
                'title' => [
                    'type' => 'text'
                ],
                'document_type' => [
                    'type' => 'text'
                ],
                'text' => [
                    'type' => 'text'
                ],
                'sort' => [
                    'type' => 'text'
                ],
                'status'=>[
                    'type'=>'text'
                ],
                'doc_system_id'=>[
                    'type'=>'text'
                ]
            ]
        ];

        $targetCount = Document::matchAllSearch()->size(0)->execute()->total();

        Index::createRaw('oa_document_v2', $mapping);

        Document::$searchableAs = 'oa_document_v2';
        Document::makeAllSearchable();

        // wait until all documents are reindexed to the new index
        while (true) {
            $currentCount = Document::matchAllSearch()->size(0)->execute()->total();

            if ($currentCount >= $targetCount) {
                break;
            }

            sleep(1);
        }
        // step 3
        Index::drop('oa_document');

        // step 4
        Index::putAlias('oa_document_v2', 'oa_document');
    }

    /**
     * Reverse the migration.
     */
    public function down(): void
    {

    }
}

Implementation steps:

❯ php artisan elastic:migrate 2024_03_19_190722_oa_document
Migrating: 2024_03_19_190722_oa_document

   BadMethodCallException 

  Call to undefined method Modules\Document\Models\Document::matchAllSearch()

  at vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:67
     63▕      * @throws \BadMethodCallException
     64▕      */
     65▕     protected static function throwBadMethodCallException($method)
     66▕     {
  ➜  67▕         throw new BadMethodCallException(sprintf(
     68▕             'Call to undefined method %s::%s()', static::class, $method
     69▕         ));
     70▕     }
     71▕ }

  i   Bad Method Call: Did you mean Modules\Document\Models\Document::makeAllSearchable() ? 

      +3 vendor frames 

  4   elastic/migrations/2024_03_19_190722_oa_document.php:76
      Illuminate\Database\Eloquent\Model::__callStatic("matchAllSearch", [])

  5   elastic/migrations/2024_03_19_190722_oa_document.php:18

Expected behavior Normal execution

dducro commented 7 months ago

matchAllSearch() might be an old method. Use searchQuery().

To count the total rows use:

Document::searchQuery()
            ->size(0)
            ->trackTotalHits(true) // Needed for indexes with more then 10000 documents.
            ->execute()
            ->total();

Hope it helps.

github-actions[bot] commented 6 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days