如何使联接表的搜索laravel / elasticsearch?

时间:2018-11-14 07:55:40

标签: elasticsearch laravel-5

在我的laravel 5.7 / mysql应用中,我使用基于 https://michaelstivala.com/learning-elasticsearch-with-laravel/ 文章。 这对我来说很清楚如何保存/删除/搜索基于一个表的数据。 但是我有桌子:

CREATE TABLE `votes` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `description` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',

带有名称和描述字段的搜索

但我也有一张桌子

CREATE TABLE `vote_items` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `vote_id` INT(10) UNSIGNED NULL DEFAULT NULL,
    `name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',

我也想在名称字段中进行搜索。

这似乎有些棘手。在我的投票模型中,我定义了与Elasticsearch同步数据的方法:

<?php

namespace App;

use DB;

class Vote extends Model
{
    protected $table = 'votes';
    protected $elasticsearch_type  = 'vote';
    protected $primaryKey = 'id';
    ...
    public function getElasticsearchType(): string
    {
        return $this->elasticsearch_type;
    }

    protected static function boot() {
        parent::boot();

        static::saved(function ($vote) {
            $elastic = app(\App\Elastic\Elastic::class);
            $elasticsearch_root_index  = config('app.elasticsearch_root_index');
            $elasticsearch_type        = with(new Vote)->getElasticsearchType();

            $elastic->delete([
                'index' => $elasticsearch_root_index,
                'type'  => $elasticsearch_type,
                'id'    => $vote->id,
            ]);

            if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
                $elastic->index([
                    'index' => $elasticsearch_root_index,
                    'type'  => $elasticsearch_type,
                    'id'    => $vote->id,
                    'body'  => [
                        'id'          => $vote->id,
                        'slug'        => $vote->slug,
                        'name'        => $vote->name,
                        'description' => $vote->description,
                    ]
                ]);
            } // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch

        });
        static::deleted(function ($vote ) {
            $elastic = app(\App\Elastic\Elastic::class);
            $elasticsearch_root_index  = config('app.elasticsearch_root_index');
            $elasticsearch_type        = with(new Vote)->getElasticsearchType();
            $elastic->delete([
                'index' => $elasticsearch_root_index,
                'type' => $elasticsearch_type,
                'id' => $vote->id,
            ]);
        });
    }


    public static function bulkVotesToElastic()
    {
        $elastic = app(\App\Elastic\Elastic::class);

        $elasticsearch_root_index  = config('app.elasticsearch_root_index');
        $elasticsearch_type        = with(new Vote)->getElasticsearchType();

        Vote::chunk(100, function ($Votes) use ($elastic, $elasticsearch_root_index, $elasticsearch_type) {
            foreach ($Votes as $nextVote) {
                if ($nextVote->status!= 'A') continue; // only active votes must be saved in elasticsearch
                $elastic->index([
                    'index' => $elasticsearch_root_index,
                    'type'  => $elasticsearch_type,
                    'id'    => $nextVote->id,
                    'body'  => [
                        'id'          => $nextVote->id,
                        'slug'        => $nextVote->slug,
                        'name'        => $nextVote->name,
                        'description' => $nextVote->description,
                    ]
                ]);
            }
        });
    }


}

我想我可以为vote_items模型制作类似的方法,并保存具有不同类型的所有数据,但是我认为 不是有效的方法...哪种方法有效?有一些例子可以实现吗?

谢谢!

0 个答案:

没有答案