laravel elasticsearch scout 多模板 多索引

栏目: 编程语言 · PHP · 发布时间: 7年前

内容简介:laravel安装config/scout.php上面设置一下索引名

laravel安装 elasticsearch scout ,用的过程中,发现一个问题,只能存在一个模板和一个索引,用的是elasticsearch 6。

一,发现问题

config/scout.php

'elasticsearch' => [
    'index' => env('ELASTICSEARCH_QUESTION', 'question'),
    'hosts' => [
        env('ELASTICSEARCH_HOST', 'http://localhost:9200'),
    ],
],

上面设置一下索引名

<?php

namespace ScoutEngines\Elasticsearch;

use Laravel\Scout\EngineManager;
use Illuminate\Support\ServiceProvider;
use Elasticsearch\ClientBuilder as ElasticBuilder;

class ElasticsearchProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     */
    public function boot()
    {
        app(EngineManager::class)->extend('elasticsearch', function($app) {
            return new ElasticsearchEngine(ElasticBuilder::create()
                ->setHosts(config('scout.elasticsearch.hosts'))
                ->build(),
                config('scout.elasticsearch.index')  //底层这块就写死了
            );
        });
    }
}

二,解决办法

1,修改config/scout.php

'elasticsearch' => [
    'index' => env('ELASTICSEARCH_QUESTION', 'question'),
    'exam' => env('ES_EXAM', 'exam'),
    'question' => env('ES_QUESTION', 'question'),
    'hosts' => [
        env('ELASTICSEARCH_HOST', 'http://localhost:9200'),
    ],
],

2,创建es模板和索引

php artisan make:command ESQuestion
php artisan make:command ESExam

在app/console/commands目录下会产生二个文件,怎么修改,参考文章开头的连接

3,初始化es模板和索引

php artisan es:question
php artisan es:exam

4,生成models

php artisan make:model ./Models/t_es_question
php artisan make:model ./Models/t_es_exam

5,修改models

//t_es_question修改如下
public function __construct()
{
    \Config::set('scout.elasticsearch.index', config('scout.elasticsearch.question'));
    parent::__construct();
}

//t_es_exam修改如下
public function __construct()
{
    \Config::set('scout.elasticsearch.index', config('scout.elasticsearch.exam'));
    parent::__construct();
}在

在调用不同model前指定index,这样可以了,还有一种方式是,就是改底层代码,改底层花的时间就比较长了。

6,导入数据

php artisan scout:import "App\Models\t_es_question"
php artisan scout:import "App\Models\t_es_exam"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Effective Engineer

The Effective Engineer

Edmond Lau / The Effective Bookshelf, Palo Alto, CA. / 2015-3-19 / USD 39.00

Introducing The Effective Engineer — the only book designed specifically for today's software engineers, based on extensive interviews with engineering leaders at top tech companies, and packed with h......一起来看看 《The Effective Engineer》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具