On-site search

Elasticsearch is a search engine for Elasticsearch, which is a search engine for Elasticsearch. Medcl Elasticsearch (select * from medcl, select * from medCL, select * from medCL) Index type Name Document: indicates the index document field. Template: indicates the template

So once you know the basics

1. Install Elasticsearch and IK plugin

Elasticsearch integration installation package 1 at https://github.com/medcl/elasticsearch-rtf. Zip file: elasticSearch. Bat: zip file: elasticSearch. Bat: zip file: elasticSearch

The first step is to install JDK1.8 and configure the Java environment




Go to the bin directory of ElasticSearch to install elasticSearch. bat

Don’t know why here small make up the installation, after the failure of baidu many eventually failed when I tried to find a install https://pan.baidu.com/s/1pJNkrUV

Enable elasticSearch binElasticSearch -d

Go ahead and open http://localhost:9200/


If this appears then congratulations you have successfully installed if the computer memory is small only keep the IK plugin in the plugin directory

2. Install Elasticsearch’s Laravel Scout

Under the framework of https://laravel.com/docs/5.6/scout in installing laravel composer require laravel/scout

Configure the app file LaravelScoutScoutServiceProvider: : class

Install composer require Tamayo/Laravel-Scouter-elastic

After the installation is complete app. Add ScoutEnginesElasticsearchElasticsearchProvider: PHP: class


Modify the configuration file scout select ElasticSearch to add the index driver


Configure index and hosts based on your own index and port

3. Create indexes and templates for Ylaravel

PHP artisan make:command ESInit


Configure ESInit to modify the command startup script and description


I’m going to mount it in appconsolekernel. PHP and then in PHP Artisan you can see that there isa script startup command for ES which means that it was created successfully Create a template in handle

$client = new Client(); $url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp'; $client-> delete($url); $param = [ 'json'=>[ 'template' =>config('scout.elasticsearch.index'), 'mappings' =>[ '_default' => [ 'dynamic_templates'=>[ [ 'string'=>[ 'match_mapping_type' =>'string', 'mapping' => [ 'type'=> 'text', 'analyzer' => 'ik_smart', 'fields'=>[ 'keyword'=>[ 'type' =>'keyword' ] ] ] ] ] ] ] ] ], ]; $client-> put($url,$param); $this-> Info ("======= template created successfully ============="); / / create the index $url = config (' scout. Elasticsearch. Hosts') [0]. '/'. The config (' scout. Elasticsearch. Index); //$client-> delete($url); $param = [ 'json'=>[ 'settings'=>[ 'refresh_interval' =>'5s', 'number_of_shards' => 1, 'number_of_replicas' =>0, ], 'mappings'=>[ '_default_' =>[ '_all' =>[ 'enabled'=>false ] ] ] ] ]; $client-> put($url,$param); $this-> Info ("======= index created successfully =============");Copy the code

4. Import the database

Modify the model first by introducing Laravel’s Scout and defining index types and fields below are related operations under the model to modify them according to your needs

$this-> work_name, 'work_content'=> $this-> work_content, ]; }}Copy the code

The next step is to import the tables for the database you want to search


You can see that I’ve successfully imported my data and the largest ID is 20


Let’s test work 20


Later, the blogger also tested the update of the database. We can see that laravel automatically added the data to the index after the database was updated, and was also very frustrated. Why couldn’t the database be updated The Scout driver must use a new model method to automatically update data to the index


That’s when it hit me, and I rewrote the method in my controller


After creating the instance and writing the save method, I can test whether my data is updated in real time, and find that deleting and adding the data Scout automatically updates my data to the index. Then we can write the search method

5. Search page writing logic

First we create a route to get the search page and then we write the controller method


The next step is to return data to the page and populate the page with data



At the beginning of the search for the word is too sultry, resulting in fruitless search is also pit me for a long time, the IK segmentation configuration is not very understanding, resulting in IK’s strong Chinese segmentation is not reflected, but in general you can search normal words can search results. If you have done this step, congratulations on your success. If you want a more powerful search function, you have to continue to work hard to follow up !!!!!!

For a more accurate segmentation search function simply select IK_max_word instead of IK_smart ik_max_word: For example, “National anthem of the People’s Republic of China” will be split into “People’s Republic of China, People’s Republic of China, People’s Republic of China, People’s Republic of China, People’s Republic of China, People’s Republic of China, People’s Republic of China, republic of China, and Guoguo, national anthem”, exhausting all possible combinations; Ik_smart: text will be split in the coarsest granularity. For example, “National anthem of the People’s Republic of China” will be split into “National anthem of the People’s Republic of China”. Attention attention !!!!! In spite of all the problems that went online, my first installation of the JAVA environment went very well for coincidental reasons.

– There are so many pits for ElasticSearch that the blogger’s head is getting smashed. In order to prevent friends from repeating my mistakes please pay attention to the following instructions: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Weak bloggers, seeking attention




Personal blog

More front-end technology articles, art design, wordpress plug-ins, optimization tutorials, learning notes in my personal blog Meorong – describe life with you, welcome to exchange and learn together, progress together: http://panmiaorong.top

Article recommendation:

Here are some of the top tips for interviewing in 2018

Super easy to use development tools, programmer utility collation

Collect quality Chinese front-end blogs (updated irregularly)

In-game Search (laravel Scout + ElasticSearch)

Vue Axios encapsulation and API interface management

Original link:

PHP site search (laravel Scout + ElasticSearch) – Meowpanmiaorong.top