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"

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

查看所有标签

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

Ruby元编程

Ruby元编程

[意] Paolo Perrotta / 廖志刚、陈睿杰 / 华中科技大学出版社 / 2012-1-10 / 56.00元

《Ruby元编程》以案例形式循序渐进讲解Ruby对象模型原理和高级应用技巧,堪称动态语言的设计模式。书中讲述的各种Ruby编程模式,完全可以应用于其他动态语言(甚至静态语言)。本书不仅适合Ruby程序员阅读,也适合对动态编程 语言和面向对象编程感兴趣的读者阅读。所有对程序设计理论感兴趣的人都能从中获益。Ruby之父松本行弘作序推荐。一起来看看 《Ruby元编程》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

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

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具