Home  >  Article  >  php教程  >  Laravel5.3+Scout+ElasticSearch5.0 Notes

Laravel5.3+Scout+ElasticSearch5.0 Notes

大家讲道理
大家讲道理Original
2016-11-11 09:11:111441browse

ElasticSearch5.0

There is no key point in installing es5.0. As long as you follow the official document process, you can usually install it successfully and run it successfully.
However, in the online environment, others are still using es2.4. First, the project is large. Second, different versions of jdk have different memory requirements.
And after es5.0, due to the cancellation of site-plugin, many plug-ins cannot be installed in the previous way. For example, elasticSearch-head is very commonly used, and it needs to be installed now. Run through Grunt. Or put other plug-ins 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 /etc/default/elasticsearch In this file, find JAVA_HOME=/usr/local/java/jdk1.8.0_101/jre;

  • If the environment memory is too small, don’t install it. es5.0 takes up almost 2.5G of my memory. Of course, most people’s computers now There is quite a lot of memory;

Install and configure Laravel/Scout

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

SCOUT_DRIVER=customElasticSearchELASTICSEARCH_INDEX=boxELASTICSEARCH_HOST=localhost:9200

The configuration of these three lines is Sc for out use To determine what Engine you are using 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
                            ]
                        ],
                    ],
                ],
            ],
        ];

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
            ]
        ];
$query = [            'index' => $this->index,            'type' => $builder->model->searchableAs(),            'body' => [                'query' => [                    'bool' => [                        'filter' => $filters,                        'must' => $matches,
                    ],
                ],
            ],
        ];

Possible pitfalls of using Scout

If you have a primary key in the database table that is auto-incremented and named id field, but you don’t want elasticSearch to use the id of the data table as the id of es’ Document, then you need to change the $primaryKey of the model and public $incrementing = false;, so you You can specify other values ​​​​in the current data table to serve as the id of es. If part of the es data _id is the id of the database, and the other part is newly specified by you, then it will cause your search or other Operation and operation are all affected.


Statement:
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