babenkoivan / elastic-scout-driver

Elasticsearch driver for Laravel Scout
MIT License
257 stars 33 forks source link

New record isn't added to Index #22

Closed alfee closed 3 years ago

alfee commented 3 years ago
Software Version
PHP 8.0.6
Elasticsearch 7.13.0
Laravel 8.44.0
Laravel Scout 9.1.0

Describe the bug

I'm a beginner for Laravel Scout and Elasticsearch, and I tried to introduce Elasticsearch to my project. The search function looks to work fine after php artisan scout:import 'App\Models\Post', but a record isn't added to Index when I created a new record.
Scout document says Once you have added the Laravel\Scout\Searchable trait to a model, all you need to do is save or create a model instance and it will automatically be added to your search index. https://laravel.com/docs/master/scout#adding-records I wonder why my new record isn't added to Index.
My Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use ElasticScoutDriverPlus\QueryDsl;

class Post extends Model
{
    use HasFactory;
    use Searchable;
    use QueryDsl;

    protected $with = ['user', 'tags'];

    /**
     * Get the name of the index associated with the model.
     *
     * @return string
     */
    public function searchableAs () {
        return 'posts_index';
    }

    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();

        // Customize the data array...

        return $array;
    }

  // ...
}

My Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\History;
use App\Models\Post;
use App\Models\Tag;

use Inertia\Inertia;

class PostController extends Controller
{

// ...

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // ...
        Post::create([
            'user_id' => $request->user()->id,
            'team_id' => $request->user()->currentTeam->id,
            'title' => $request->input('title'),
            'content' => $request->input('content'),
            'type' => $request->input('type'),
            'status' => $request->input('status'),
            'is_wip' => $request->input('is_wip')
        ])->tags()->attach($tagIds);

        return redirect()->route('dashboard')->with('success', 'Post is created.');
    }

// ..

}

Should I make other settings for Elasticsearch and Scout or code to add a record to Index?

babenkoivan commented 3 years ago

Hey @alfee,

First of all, please update the package and the dependencies (especially elastic-adapter). I've improved error handling recently, so it might help you to debug the issue. Second, check your Scout config: if you have queues enabled, then confirm that workers are running. Finally, you didn't mention how you created the index and what mapping you have.

alfee commented 3 years ago

Hi @babenkoivan,

Thank you for your reply. I did composer update and auto-sync is working fine.

Thanks again.

github-actions[bot] commented 3 years 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