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

Laravel5.3+Scout+ElasticSearch5.0

Nov 15, 2016 pm 02:44 PM
html5

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.


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles