Home > php教程 > PHP开发 > Laravel5.3+Scout+ElasticSearch5.0

Laravel5.3+Scout+ElasticSearch5.0

高洛峰
Release: 2016-11-15 14:44:23
Original
1364 people have browsed it

System environment

Ubuntu16.04, ElasticSearch5.0, JDK1.8

ElasticSearch5.0

There is no point in installing es5.0. As long as you follow the official document process, you can usually install it successfully and run it successfully.
But it is online Environment, others are still using es2.4. Firstly, the project is larger, and secondly, different versions of jdk have different memory requirements.
And after es5.0, due to the cancellation of site-plugin, many plug-ins cannot follow the It was installed in the previous way. For example, elasticSearch-head is very commonly used, and now it needs to be run through Grunt. Or other plug-ins are put into the www directory of Nginx or Apache to run.

Small problems that may occur after installation:

$JAVA_HOME cannot be found , but it is indeed installed. You can set the /etc/default/elasticsearch file and find JAVA_HOME=/usr/local/java/jdk1.8.0_101/jre;

Don’t install it if the environment memory is too small, es5.0 takes up almost I have 2.5G of memory. Of course, most people’s computers now have quite a lot of memory;

Install and configure Laravel/Scout

Add these three lines at the bottom of the .env file

SCOUT_DRIVER=customElasticSearch
ELASTICSEARCH_INDEX=box
ELASTICSEARCH_HOST=localhost:9200
Copy after login

These three lines of configuration are used by Scout to determine your Which Engine to use, and the address of the search engine.

Readers may find that my Driver is customElasticSearch, not elasticsearch.
Because when you open ElasticSearchEngine and find performSearch Method, you will find this piece of code inside

$query = [
            'index' =>  $this->index,
            'type'  =>  $builder->model->searchableAs(),
            'body' => [
                'query' => [
                    'filtered' => [
                        'filter' => $filters,
                        'query' => [
                            'bool' => [
                                'must' => $matches
                            ]
                        ],
                    ],
                ],
            ],
        ];
Copy after login

If you run search Method directly, you will be told that filtered has been cancelled. For details, see the official website address.
But you can’t directly change the package code. Fortunately, Scout provides a custom Engine.

So we create a new customElasticSearchEngine , inherit elasticSearchEngine, and rewrite performSearch Method. In it, I modified two places,

这只是演示, 要真使用以后一定要改
$matches[] = [
            'match' => [
                '字段名' => $builder->query
            ]
        ];
Copy after login
$query = [
            'index' => $this->index,
            'type' => $builder->model->searchableAs(),
            'body' => [
                'query' => [
                    'bool' => [
                        'filter' => $filters,
                        'must' => $matches,
                    ],
                ],
            ],
        ];
Copy after login

Possible pitfalls of using Scout

If you have a field with a primary key auto-incremented and named id in the database table, but you don’t If you want elasticSearch to use the id of the data table to serve as the id of es' Document, then you need to change the $primaryKey of the model and public $incrementing = false;, so that you can specify other values ​​​​of the current data table to serve as the id of es. If es's Part of the data_id is the database id, and the other part is newly specified by you, which will affect your search and other operations.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template