laravel elasticsearch scout 多模板 多索引

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

内容简介: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"

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

查看所有标签

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

Introduction to Tornado

Introduction to Tornado

Michael Dory、Adam Parrish、Brendan Berg / O'Reilly Media / 2012-3-28 / USD 23.99

Tornado is a scalable, non-blocking web server and web application framework written in Python. It is also light-weight to deploy, fun to write for, and incredibly powerful. Tornado was written with p......一起来看看 《Introduction to Tornado》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试